Skip to content

Commit

Permalink
feat: cleaner recordHook error messages (#327)
Browse files Browse the repository at this point in the history
* feat: cleaner recordHook error messages

* Create few-yaks-fetch.md

* feat: add test for error message

* add reset script

* feat: restructure test & console.log -> console.error
  • Loading branch information
carlbrugger authored Nov 27, 2023
1 parent 7835dcc commit f18a889
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-yaks-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@flatfile/plugin-record-hook': patch
---

Human-readable errors from recordHook
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"clean": "find ./ '(' -name 'node_modules' -o -name 'dist' -o -name '.turbo' -o -name '.parcel-cache' ')' -type d -exec rm -rf {} +",
"test": "turbo test --concurrency=1",
"build": "turbo build",
"reset": "npm run clean && npm i && npm run build",
"lint": "prettier --check **/*.ts",
"changeset": "changeset",
"changeset-apply": "changeset version",
Expand Down
10 changes: 8 additions & 2 deletions plugins/record-hook/src/RecordHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ export const RecordHook = async (
) => any | Promise<any>,
options: RecordHookOptions = {}
) => {
const { concurrency } = { concurrency: 10, ...options }
const { concurrency = 10 } = options
return BulkRecordHook(
event,
async (records, event) => {
const handlers = await records.map((record: FlatfileRecord) =>
Effect.promise(async () => {
await handler(record, event)
try {
await handler(record, event)
} catch (e) {
console.error(
`An error occurred while running the handler: ${e.message}`
)
}
})
)
return Effect.runPromise(
Expand Down
27 changes: 27 additions & 0 deletions plugins/record-hook/src/record.hook.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,31 @@ describe('recordHook() e2e', () => {
expect(records[1].values['name']).toMatchObject({ value: 'daddy' })
})
})

describe('recordHook errors', () => {
const logErrorSpy = jest.spyOn(global.console, 'error')
beforeEach(async () => {
listener.use(
recordHook('test', (record) => {
throw new Error('oops')
})
)
await createRecords(sheetId, [
{
name: 'John Doe',
email: '[email protected]',
notes: 'foobar',
},
])
})
it('returns readable error', async () => {
await listener.waitFor('commit:created')
expect(logErrorSpy).toHaveBeenCalledWith(
'An error occurred while running the handler: oops'
)
})
afterEach(() => {
logErrorSpy.mockClear()
})
})
})

0 comments on commit f18a889

Please sign in to comment.