Skip to content

Commit

Permalink
mutate error stack with context
Browse files Browse the repository at this point in the history
  • Loading branch information
darky committed Jan 17, 2025
1 parent 02ea038 commit 5b913db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
26 changes: 10 additions & 16 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4863,14 +4863,11 @@ export function klubok(...fns: KeyedFunction<string, Function>[]) {
: (async () => {
try {
return await Promise.resolve(fn(ctx)).then(resp => ({ ...ctx, [fn.key]: resp }))
} catch (error: any) {
throw inspect(
{
error: error.stack ?? error.message ?? error,
ctx,
},
{ depth: 3 }
)
} catch (error) {
if (error instanceof Error) {
error.stack += '\ncontext: ' + inspect(ctx)
}
throw error
}
})()
)
Expand All @@ -4890,14 +4887,11 @@ export function klubok(...fns: KeyedFunction<string, Function>[]) {
? Reflect.get(mock, fn.key)(ctx)
: fn(ctx)
).then(resp => ({ ...ctx, [fn.key]: resp }))
} catch (error: any) {
throw inspect(
{
error: error.stack ?? error.message ?? error,
ctx,
},
{ depth: 3 }
)
} catch (error) {
if (error instanceof Error) {
error.stack += '\ncontext: ' + inspect(ctx)
}
throw error
}
})()
),
Expand Down
8 changes: 4 additions & 4 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ test('throwable error with context for production fn', async () => {
})
)
const err = await fn({ number: 1 }).catch(e => e)
assert.match(err, /error: 'Error: test/)
assert.match(err, /ctx: { number: 1, incNumber: 2, strNumber: '2' }/)
assert.deepStrictEqual(err, new Error('test'))
assert.match(err.stack ?? '', /context: { number: 1, incNumber: 2, strNumber: '2' }/)
})

test('throwable error with context for test fn', async () => {
Expand All @@ -350,6 +350,6 @@ test('throwable error with context for test fn', async () => {
})
)
const err = await fn({ number: 1 }, {}, []).catch(e => e)
assert.match(err, /error: 'Error: test/)
assert.match(err, /ctx: { number: 1, incNumber: 2, strNumber: '2' }/)
assert.deepStrictEqual(err, new Error('test'))
assert.match(err.stack ?? '', /context: { number: 1, incNumber: 2, strNumber: '2' }/)
})

0 comments on commit 5b913db

Please sign in to comment.