fix(napi): propagation error in function call (#1315)
This commit is contained in:
parent
5ba70b0e1a
commit
ea18170779
9 changed files with 79 additions and 8 deletions
examples/napi
|
@ -43,6 +43,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void␊
|
||||
export function returnJsFunction(): (...args: any[]) => any␊
|
||||
export function callbackReturnPromise<T>(functionInput: () => T | Promise<T>, callback: (err: Error | null, result: T) => void): T | Promise<T>␊
|
||||
export function captureErrorInCallback(cb1: () => void, cb2: (arg0: Error) => void): void␊
|
||||
export interface ObjectFieldClassInstance {␊
|
||||
bird: Bird␊
|
||||
}␊
|
||||
|
|
Binary file not shown.
|
@ -112,6 +112,7 @@ import {
|
|||
CustomFinalize,
|
||||
plusOne,
|
||||
Width,
|
||||
captureErrorInCallback,
|
||||
} from '../'
|
||||
|
||||
test('export const', (t) => {
|
||||
|
@ -290,6 +291,15 @@ test('callback', (t) => {
|
|||
t.is(err, undefined)
|
||||
t.is(content, 'hello world')
|
||||
})
|
||||
|
||||
captureErrorInCallback(
|
||||
() => {
|
||||
throw new Error('Testing')
|
||||
},
|
||||
(err) => {
|
||||
t.is((err as Error).message, 'Testing')
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
test('return function', (t) => {
|
||||
|
|
1
examples/napi/index.d.ts
vendored
1
examples/napi/index.d.ts
vendored
|
@ -33,6 +33,7 @@ export function optionOnly(callback: (arg0?: string | undefined | null) => void)
|
|||
export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void
|
||||
export function returnJsFunction(): (...args: any[]) => any
|
||||
export function callbackReturnPromise<T>(functionInput: () => T | Promise<T>, callback: (err: Error | null, result: T) => void): T | Promise<T>
|
||||
export function captureErrorInCallback(cb1: () => void, cb2: (arg0: Error) => void): void
|
||||
export interface ObjectFieldClassInstance {
|
||||
bird: Bird
|
||||
}
|
||||
|
|
|
@ -80,3 +80,15 @@ fn callback_return_promise<T: Fn() -> Result<JsUnknown>>(
|
|||
Ok(ret)
|
||||
}
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn capture_error_in_callback<C: Fn() -> Result<()>, E: Fn(Error) -> Result<()>>(
|
||||
cb1: C,
|
||||
cb2: E,
|
||||
) -> Result<()> {
|
||||
if let Err(e) = cb1() {
|
||||
cb2(e)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue