Skip to content

Commit fe3b9f5

Browse files
Pass rootScope to error serializer
1 parent 7bd8379 commit fe3b9f5

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

src/sandbox/handleToNative/handleToNative.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export const handleToNative = (ctx: QuickJSContext | QuickJSAsyncContext, handle
168168
if (typeof errorType === 'string') {
169169
const s = getSerializer(errorType)
170170
if (s) {
171-
const ret = s(ctx, handle)
171+
const ret = s(ctx, handle, rootScope)
172172
if (ret) {
173173
return ret
174174
}
@@ -204,9 +204,9 @@ export const handleToNative = (ctx: QuickJSContext | QuickJSAsyncContext, handle
204204
handle,
205205
).consume(r => ctx.dump(r))
206206

207-
const serializer = getSerializer(constructorName)
208-
if (serializer) {
209-
const ret = serializer(ctx, handle, rootScope)
207+
const serializer = getSerializer(constructorName)
208+
if (serializer) {
209+
const ret = serializer(ctx, handle, rootScope)
210210
if (ret) {
211211
return ret
212212
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import { loadQuickJs } from '../loadQuickJs.js'
3+
import type { ErrorResponse } from '../types/ErrorResponse.js'
4+
import type { SandboxOptions } from '../types/SandboxOptions.js'
5+
6+
describe('bugfix - #82', () => {
7+
it('handles async env errors without crashing', async () => {
8+
const runtime = await loadQuickJs()
9+
10+
const thrower = async (_input: string) => {
11+
throw new Error('test')
12+
}
13+
14+
const options: SandboxOptions = {
15+
allowFetch: false,
16+
env: {
17+
thrower,
18+
},
19+
}
20+
21+
const code = `
22+
const fn = async ()=>{
23+
await env.thrower('some-id')
24+
return true
25+
}
26+
27+
export default await fn()
28+
`
29+
30+
const result = await runtime.runSandboxed(async ({ evalCode }) => evalCode(code), options)
31+
32+
expect(result.ok).toBeFalse()
33+
expect((result as ErrorResponse).error.name).toBe('Error')
34+
expect((result as ErrorResponse).error.message).toBe('test')
35+
})
36+
})

0 commit comments

Comments
 (0)