fix: enhance error handling (#1758)

This commit is contained in:
Hong Jiarong 2025-05-14 13:43:40 +08:00 committed by GitHub
parent 5bf177637a
commit 28d416b852
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 4 deletions

View file

@ -237,7 +237,10 @@ impl Typlite {
let mut world = world.task(task_inputs).html_task().into_owned();
let markdown_id = FileId::new(
Some(typst_syntax::package::PackageSpec::from_str("@local/markdown:0.1.0").unwrap()),
Some(
typst_syntax::package::PackageSpec::from_str("@local/markdown:0.1.0")
.context_ut("failed to import markdown package")?,
),
VirtualPath::new("lib.typ"),
);
@ -261,7 +264,10 @@ impl Typlite {
r#"#import "@local/markdown:0.1.0": md-doc, example
#show: md-doc
{}"#,
world.source(current).unwrap().text()
world
.source(current)
.context_ut("failed to get main file content")?
.text()
)),
)
.context_ut("cannot map source for main file")?;

View file

@ -90,7 +90,9 @@ fn run(args: CompileArgs, world: Arc<LspWorld>) -> tinymist_std::Result<()> {
};
if is_stdout {
std::io::stdout().write_all(result.as_slice()).unwrap();
std::io::stdout()
.write_all(result.as_slice())
.context("failed to write to stdout")?;
} else if let Err(err) = std::fs::write(&output_path, result.as_slice()) {
bail!("failed to write file {output_path:?}: {err}");
}

View file

@ -20,7 +20,9 @@ impl MarkdownWriter {
impl FormatWriter for MarkdownWriter {
fn write_eco(&mut self, document: &Node, output: &mut EcoString) -> Result<()> {
let mut writer = CommonMarkWriter::new();
writer.write(document).expect("Failed to write document");
writer
.write(document)
.map_err(|e| format!("failed to write document: {}", e))?;
output.push_str(&writer.into_string());
Ok(())
}