Skip to content

Commit 09034bc

Browse files
authored
fix: improve error message when handler not found from worker (#117)
1 parent 8c3370c commit 09034bc

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/entry/process.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ async function onMessage(message: IncomingMessage & { source: 'port' }) {
6969
try {
7070
const handler = await getHandler(filename, name)
7171
if (handler === null) {
72-
throw new Error(`No handler function exported from ${filename}`)
72+
throw new Error(
73+
`No handler function "${name}" exported from "${filename}"`
74+
)
7375
}
7476
const result = await handler(task)
7577
response = {

src/entry/worker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ function onMessage(
100100
try {
101101
const handler = await getHandler(filename, name)
102102
if (handler === null) {
103-
throw new Error(`No handler function exported from ${filename}`)
103+
throw new Error(
104+
`No handler function "${name}" exported from "${filename}"`
105+
)
104106
}
105107
let result = await handler(task)
106108
if (isMovable(result)) {

test/uncaught-exception-from-handler.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dirname, resolve } from 'node:path'
1+
import { dirname, resolve, sep } from 'node:path'
22
import { Tinypool } from 'tinypool'
33
import { fileURLToPath } from 'node:url'
44
import { once } from 'node:events'
@@ -67,3 +67,23 @@ test('using parentPort is treated as an error', async () => {
6767
`)
6868
).rejects.toThrow(/Unexpected message on Worker: 'some message'/)
6969
})
70+
71+
test('no named handler found from worker', async () => {
72+
const pool = new Tinypool({
73+
filename: resolve(__dirname, 'fixtures/eval.js'),
74+
})
75+
76+
let errorMessage = 'Worker did not throw error'
77+
78+
try {
79+
await pool.run('', { name: 'someHandler' })
80+
} catch (error) {
81+
errorMessage = error instanceof Error ? error.message : String(error)
82+
}
83+
84+
expect(
85+
errorMessage.replace(process.cwd(), '<process-cwd>').replaceAll(sep, '/')
86+
).toMatchInlineSnapshot(
87+
`"No handler function "someHandler" exported from "<process-cwd>/test/fixtures/eval.js""`
88+
)
89+
})

0 commit comments

Comments
 (0)