From f77c87723ede0ba20de1710b0da20bf570a5f8ba Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 29 Jan 2024 15:30:22 +0000 Subject: [PATCH 1/3] fix: remove panic from `init_log_level` in `acvm_js` --- acvm-repo/acvm_js/src/logging.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/acvm-repo/acvm_js/src/logging.rs b/acvm-repo/acvm_js/src/logging.rs index f5d71fae067..0613920a4f2 100644 --- a/acvm-repo/acvm_js/src/logging.rs +++ b/acvm-repo/acvm_js/src/logging.rs @@ -1,3 +1,4 @@ +use js_sys::{Error, JsString}; use tracing_subscriber::prelude::*; use tracing_subscriber::EnvFilter; use tracing_web::MakeWebConsoleWriter; @@ -7,12 +8,13 @@ use wasm_bindgen::prelude::*; /// /// @param {LogLevel} level - The maximum level of logging to be emitted. #[wasm_bindgen(js_name = initLogLevel, skip_jsdoc)] -pub fn init_log_level(filter: String) { +pub fn init_log_level(filter: String) -> Result<(), JsLogInitError> { // Set the static variable from Rust use std::sync::Once; - let filter: EnvFilter = - filter.parse().expect("Could not parse log filter while initializing logger"); + let filter: EnvFilter = filter.parse().map_err(|err| { + JsLogInitError::constructor(format!("Could not parse log filter while initializing logger: {err}").into()) + })?; static SET_HOOK: Once = Once::new(); SET_HOOK.call_once(|| { @@ -23,4 +25,19 @@ pub fn init_log_level(filter: String) { tracing_subscriber::registry().with(fmt_layer.with_filter(filter)).init(); }); + + Ok(()) +} + +/// `LogInitError` is a raw js error. +/// It'd be ideal that `LogInitError` 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 = "LogInitError", typescript_type = "LogInitError")] + #[derive(Clone, Debug, PartialEq, Eq)] + pub type JsLogInitError; + + #[wasm_bindgen(constructor, js_class = "Error")] + fn constructor(message: JsString) -> JsLogInitError; } From fa45346b7b2fb34a3bf296cf720ea294e5bba8a5 Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 29 Jan 2024 15:36:18 +0000 Subject: [PATCH 2/3] chore: cargo fmt --- acvm-repo/acvm_js/src/logging.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/acvm-repo/acvm_js/src/logging.rs b/acvm-repo/acvm_js/src/logging.rs index 0613920a4f2..a44bbe92242 100644 --- a/acvm-repo/acvm_js/src/logging.rs +++ b/acvm-repo/acvm_js/src/logging.rs @@ -13,7 +13,9 @@ pub fn init_log_level(filter: String) -> Result<(), JsLogInitError> { use std::sync::Once; let filter: EnvFilter = filter.parse().map_err(|err| { - JsLogInitError::constructor(format!("Could not parse log filter while initializing logger: {err}").into()) + JsLogInitError::constructor( + format!("Could not parse log filter while initializing logger: {err}").into() + ) })?; static SET_HOOK: Once = Once::new(); From c9c54f0c116c9b4801e8a0298bf8bc943cb91339 Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 29 Jan 2024 15:42:52 +0000 Subject: [PATCH 3/3] chore: cargo fmt --- acvm-repo/acvm_js/src/logging.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acvm-repo/acvm_js/src/logging.rs b/acvm-repo/acvm_js/src/logging.rs index a44bbe92242..483c23ab624 100644 --- a/acvm-repo/acvm_js/src/logging.rs +++ b/acvm-repo/acvm_js/src/logging.rs @@ -14,7 +14,7 @@ pub fn init_log_level(filter: String) -> Result<(), JsLogInitError> { let filter: EnvFilter = filter.parse().map_err(|err| { JsLogInitError::constructor( - format!("Could not parse log filter while initializing logger: {err}").into() + format!("Could not parse log filter while initializing logger: {err}").into(), ) })?;