-
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 aa370e8
Showing
5 changed files
with
97 additions
and
12 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,14 @@ | ||
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³⁸)", | ||
); | ||
}); |
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,10 @@ | ||
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³⁸)", | ||
); | ||
}); |
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)}`, | ||
}; |