Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove panic from init_log_level in acvm_js #4195

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions acvm-repo/acvm_js/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use js_sys::{Error, JsString};
use tracing_subscriber::prelude::*;
use tracing_subscriber::EnvFilter;
use tracing_web::MakeWebConsoleWriter;
Expand All @@ -7,12 +8,15 @@ 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(|| {
Expand All @@ -23,4 +27,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;
}
Loading