Skip to content

Commit

Permalink
fix: remove panic from init_log_level in acvm_js
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jan 29, 2024
1 parent 09bad2c commit f77c877
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 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,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(|| {
Expand All @@ -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;
}

0 comments on commit f77c877

Please sign in to comment.