Skip to content

Commit 55dbf94

Browse files
committed
Don't log routine errors as out-of-the-ordinary
To benefit users in debug mode we log any unexpected exceptions to help diagnose any issues that might arise. It turns out, though, we log this for *every* exception happening for *every* import, including imports like `__wbindgen_throw` which are explicitly intended to throw an exception. This can cause distracting debug logs to get emitted to the console, so let's squelch the debug logging for known imports that we shouldn't log for, such as intrinsics. Closes #1785
1 parent 8b4fd2a commit 55dbf94

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

crates/cli-support/src/js/binding.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub struct Builder<'a, 'b> {
4646
/// Whether or not we're catching exceptions from the main function
4747
/// invocation. Currently only used for imports.
4848
catch: bool,
49+
/// Whether or not we're logging the error coming out of this intrinsic
50+
log_error: bool,
4951
}
5052

5153
/// Helper struct used in incoming/outgoing to generate JS.
@@ -66,6 +68,7 @@ pub struct TypescriptArg {
6668
impl<'a, 'b> Builder<'a, 'b> {
6769
pub fn new(cx: &'a mut Context<'b>) -> Builder<'a, 'b> {
6870
Builder {
71+
log_error: cx.config.debug,
6972
cx,
7073
args_prelude: String::new(),
7174
finally: String::new(),
@@ -98,6 +101,12 @@ impl<'a, 'b> Builder<'a, 'b> {
98101
Ok(())
99102
}
100103

104+
pub fn disable_log_error(&mut self, disable: bool) {
105+
if disable {
106+
self.log_error = false;
107+
}
108+
}
109+
101110
pub fn process(
102111
&mut self,
103112
binding: &Binding,
@@ -107,7 +116,7 @@ impl<'a, 'b> Builder<'a, 'b> {
107116
invoke: &mut dyn FnMut(&mut Context, &mut String, &[String]) -> Result<String, Error>,
108117
) -> Result<String, Error> {
109118
// used in `finalize` below
110-
if self.cx.config.debug {
119+
if self.log_error {
111120
self.cx.expose_log_error();
112121
}
113122

@@ -378,7 +387,7 @@ impl<'a, 'b> Builder<'a, 'b> {
378387
// unhandled exceptions, typically used on imports. This currently just
379388
// logs what happened, but keeps the exception being thrown to propagate
380389
// elsewhere.
381-
if self.cx.config.debug {
390+
if self.log_error {
382391
call = format!("try {{\n{}}} catch (e) {{\n logError(e)\n}}\n", call);
383392
}
384393

crates/cli-support/src/js/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1937,6 +1937,7 @@ impl<'a> Context<'a> {
19371937
.unwrap();
19381938
self.export_function_table()?;
19391939
let mut builder = binding::Builder::new(self);
1940+
builder.disable_log_error(true);
19401941
let js = builder.process(&binding, &webidl, true, &None, &mut |_, _, args| {
19411942
Ok(format!(
19421943
"wasm.__wbg_function_table.get({})({})",
@@ -1965,6 +1966,7 @@ impl<'a> Context<'a> {
19651966
// Construct a JS shim builder, and configure it based on the kind of
19661967
// export that we're generating.
19671968
let mut builder = binding::Builder::new(self);
1969+
builder.disable_log_error(true);
19681970
match &export.kind {
19691971
AuxExportKind::Function(_) => {}
19701972
AuxExportKind::StaticFunction { .. } => {}
@@ -2058,8 +2060,10 @@ impl<'a> Context<'a> {
20582060
);
20592061
}
20602062

2063+
let disable_log_error = self.import_never_log_error(import);
20612064
let mut builder = binding::Builder::new(self);
20622065
builder.catch(catch)?;
2066+
builder.disable_log_error(disable_log_error);
20632067
let js = builder.process(
20642068
&binding,
20652069
&webidl,
@@ -2076,6 +2080,22 @@ impl<'a> Context<'a> {
20762080
}
20772081
}
20782082

2083+
/// Returns whether we should disable the logic, in debug mode, to catch an
2084+
/// error, log it, and rethrow it. This is only intended for user-defined
2085+
/// imports, not all imports of everything.
2086+
fn import_never_log_error(&self, import: &AuxImport) -> bool {
2087+
match import {
2088+
// Some intrinsics are intended to exactly throw errors, and in
2089+
// general we shouldn't have exceptions in our intrinsics to debug,
2090+
// so skip these.
2091+
AuxImport::Intrinsic(_) => true,
2092+
2093+
// Otherwise assume everything else gets a debug log of errors
2094+
// thrown in debug mode.
2095+
_ => false,
2096+
}
2097+
}
2098+
20792099
fn import_does_not_require_glue(
20802100
&self,
20812101
binding: &Binding,

0 commit comments

Comments
 (0)