-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(abi): throw errors rather than returning string from `noirc_abi_…
…wasm`
- Loading branch information
1 parent
0490549
commit a537479
Showing
7 changed files
with
168 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use js_sys::{Error, JsString}; | ||
use noirc_abi::errors::{AbiError, InputParserError}; | ||
use wasm_bindgen::prelude::wasm_bindgen; | ||
|
||
#[wasm_bindgen(typescript_custom_section)] | ||
const ABI_ERROR: &'static str = r#" | ||
export type ABIError = Error; | ||
"#; | ||
|
||
/// JsAbiError is a raw js error. | ||
/// It'd be ideal that ABI error was a subclass of Error, but for that we'd need to use JS snippets or a js module. | ||
/// Currently JS snippets don't work with a nodejs target. And a module would be too much for just a custom error type. | ||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(extends = Error, js_name = "AbiError", typescript_type = "AbiError")] | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub type JsAbiError; | ||
|
||
#[wasm_bindgen(constructor, js_class = "Error")] | ||
fn constructor(message: JsString) -> JsAbiError; | ||
} | ||
|
||
impl JsAbiError { | ||
/// Creates a new execution error with the given call stack. | ||
/// Call stacks won't be optional in the future, after removing ErrorLocation in ACVM. | ||
pub fn new(message: String) -> Self { | ||
JsAbiError::constructor(JsString::from(message)) | ||
} | ||
} | ||
|
||
impl From<String> for JsAbiError { | ||
fn from(value: String) -> Self { | ||
JsAbiError::new(value) | ||
} | ||
} | ||
|
||
impl From<AbiError> for JsAbiError { | ||
fn from(value: AbiError) -> Self { | ||
JsAbiError::new(value.to_string()) | ||
} | ||
} | ||
|
||
impl From<InputParserError> for JsAbiError { | ||
fn from(value: InputParserError) -> Self { | ||
JsAbiError::new(value.to_string()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { expect } from "@esm-bundle/chai"; | ||
import initNoirAbi, { abiEncode } from "@noir-lang/noirc_abi"; | ||
|
||
beforeEach(async () => { | ||
await initNoirAbi(); | ||
}); | ||
|
||
it("errors when an integer input overflows", async () => { | ||
const { abi, inputs } = await import("../shared/uint_overflow"); | ||
|
||
expect(() => abiEncode(abi, inputs, null)).to.throw( | ||
"The parameter foo is expected to be a Integer { sign: Unsigned, width: 32 } but found incompatible value Field(2³⁸)", | ||
); | ||
}); | ||
|
||
it("errors when passing a field in place of an array", async () => { | ||
const { abi, inputs } = await import("../shared/field_as_array"); | ||
|
||
expect(() => abiEncode(abi, inputs, null)).to.throw( | ||
"cannot parse value into Array { length: 2, typ: Field }", | ||
); | ||
}); | ||
|
||
it("errors when passing an array in place of a field", async () => { | ||
const { abi, inputs } = await import("../shared/array_as_field"); | ||
|
||
expect(() => abiEncode(abi, inputs, null)).to.throw( | ||
"cannot parse value into Field", | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { expect } from "chai"; | ||
import { abiEncode } from "@noir-lang/noirc_abi"; | ||
|
||
it("errors when an integer input overflows", async () => { | ||
const { abi, inputs } = await import("../shared/uint_overflow"); | ||
|
||
expect(() => abiEncode(abi, inputs, null)).to.throw( | ||
"The parameter foo is expected to be a Integer { sign: Unsigned, width: 32 } but found incompatible value Field(2³⁸)", | ||
); | ||
}); | ||
|
||
it("errors when passing a field in place of an array", async () => { | ||
const { abi, inputs } = await import("../shared/field_as_array"); | ||
|
||
expect(() => abiEncode(abi, inputs, null)).to.throw( | ||
"cannot parse value into Array { length: 2, typ: Field }", | ||
); | ||
}); | ||
|
||
it("errors when passing an array in place of a field", async () => { | ||
const { abi, inputs } = await import("../shared/array_as_field"); | ||
|
||
expect(() => abiEncode(abi, inputs, null)).to.throw( | ||
"cannot parse value into Field", | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const abi = { | ||
parameters: [ | ||
{ | ||
name: "foo", | ||
type: { kind: "field" }, | ||
visibility: "private", | ||
}, | ||
], | ||
param_witnesses: { foo: [1, 2] }, | ||
return_type: null, | ||
return_witnesses: [], | ||
}; | ||
|
||
export const inputs = { | ||
foo: ["1", "2"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const abi = { | ||
parameters: [ | ||
{ | ||
name: "foo", | ||
type: { kind: "array", length: 2, type: { kind: "field" } }, | ||
visibility: "private", | ||
}, | ||
], | ||
param_witnesses: { foo: [1, 2] }, | ||
return_type: null, | ||
return_witnesses: [], | ||
}; | ||
|
||
export const inputs = { | ||
foo: "1", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const abi = { | ||
parameters: [ | ||
{ | ||
name: "foo", | ||
type: { kind: "integer", sign: "unsigned", width: 32 }, | ||
visibility: "private", | ||
}, | ||
], | ||
param_witnesses: { foo: [1] }, | ||
return_type: null, | ||
return_witnesses: [], | ||
}; | ||
|
||
export const inputs = { | ||
foo: `0x${(1n << 38n).toString(16)}`, | ||
}; |