throw if napi function returns Err variant of Result<T>

This commit is contained in:
forehalo 2021-09-24 14:45:27 +08:00 committed by LongYinan
parent ee7a146ea1
commit f66f79e587
12 changed files with 53 additions and 37 deletions

View file

@ -16,7 +16,7 @@ Generated by [AVA](https://avajs.dev).
export enum Kind { Dog = 0, Cat = 1, Duck = 2 }␊
export enum CustomNumEnum { One = 1, Two = 2, Three = 3, Four = 4, Six = 6, Eight = 8, Nine = 9, Ten = 10 }␊
export function enumToI32(e: CustomNumEnum): number␊
export function getError(): Error | undefined␊
export function throwError(): void␊
export function mapOption(val: number | null): number | null␊
export function add(a: number, b: number): number␊
export function fibonacci(n: number): number␊

View file

@ -19,7 +19,7 @@ import {
createObj,
mapOption,
readFile,
getError,
throwError,
} from '../'
test('number', (t) => {
@ -98,7 +98,5 @@ test('Option', (t) => {
})
test('Result', (t) => {
const error = getError()
t.not(error, undefined)
t.is(error!.message, 'Manual Error')
t.throws(() => throwError(), null, 'Manual Error')
})

View file

@ -6,7 +6,7 @@ export function readFile(callback: (arg0: Error | undefined, arg1: string | null
export enum Kind { Dog = 0, Cat = 1, Duck = 2 }
export enum CustomNumEnum { One = 1, Two = 2, Three = 3, Four = 4, Six = 6, Eight = 8, Nine = 9, Ten = 10 }
export function enumToI32(e: CustomNumEnum): number
export function getError(): Error | undefined
export function throwError(): void
export function mapOption(val: number | null): number | null
export function add(a: number, b: number): number
export function fibonacci(n: number): number

View file

@ -1,6 +1,6 @@
use napi::bindgen_prelude::*;
#[napi]
fn get_error() -> Result<()> {
fn throw_error() -> Result<()> {
Err(Error::new(Status::InvalidArg, "Manual Error".to_owned()))
}