From 19f6ef67d4bc9d9c6932b802985899c35b3e15e0 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Sat, 16 Sep 2023 08:13:50 +0800 Subject: [PATCH] dev(pkg::compiler): start error handling (#357) --- packages/compiler/src/lib.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/compiler/src/lib.rs b/packages/compiler/src/lib.rs index 2b4b640f..0db3e98b 100644 --- a/packages/compiler/src/lib.rs +++ b/packages/compiler/src/lib.rs @@ -65,11 +65,15 @@ impl FontLoader for SnapshotFontLoader { // todo: remove this unsafe impl Send for SnapshotFontLoader {} +// todo: design error handling +// todo: we return a string for now which is better than nothing #[wasm_bindgen] impl TypstCompiler { - pub fn reset(&mut self) { + pub fn reset(&mut self) -> Result<(), JsValue> { // reset the world caches - self.compiler.reset().unwrap(); + self.compiler.reset().map_err(|e| format!("{e:?}"))?; + + Ok(()) } pub fn add_source(&mut self, path: &str, content: &str, is_main: bool) -> bool { @@ -155,13 +159,17 @@ impl TypstCompiler { ); // compile and export document - let doc = self.compiler.compile().unwrap(); + let doc = self.compiler.compile().map_err(|e| format!("{e:?}"))?; let data = ast_exporter .export(self.compiler.world(), Arc::new(doc)) - .unwrap(); + .map_err(|e| format!("{e:?}"))?; - let converted = - ansi_to_html::convert_escaped(String::from_utf8(data).unwrap().as_str()).unwrap(); + let converted = ansi_to_html::convert_escaped( + String::from_utf8(data) + .map_err(|e| format!("{e:?}"))? + .as_str(), + ) + .map_err(|e| format!("{e:?}"))?; Ok(converted) } @@ -174,10 +182,10 @@ impl TypstCompiler { typst_ts_svg_exporter::SvgModuleExporter::default(), ); - let doc = self.compiler.compile().unwrap(); + let doc = self.compiler.compile().map_err(|e| format!("{e:?}"))?; let artifact_bytes = ir_exporter .export(self.compiler.world(), Arc::new(doc)) - .unwrap(); + .map_err(|e| format!("{e:?}"))?; Ok(artifact_bytes) }