71 releases (36 breaking)

0.36.0 Nov 9, 2024
0.34.0 Oct 26, 2024
0.22.1 Jul 28, 2024
0.11.0 Mar 30, 2024
0.0.1 Mar 30, 2023

#1212 in Programming languages

Download history 71047/week @ 2024-07-30 54455/week @ 2024-08-06 47740/week @ 2024-08-13 46803/week @ 2024-08-20 38445/week @ 2024-08-27 43389/week @ 2024-09-03 37323/week @ 2024-09-10 41413/week @ 2024-09-17 53275/week @ 2024-09-24 58897/week @ 2024-10-01 44067/week @ 2024-10-08 35045/week @ 2024-10-15 17445/week @ 2024-10-22 14901/week @ 2024-10-29 15182/week @ 2024-11-05 16339/week @ 2024-11-12

66,866 downloads per month
Used in 25 crates (6 directly)

MIT license

40KB
648 lines

Error data types and utilities for handling/reporting them.

The main type in this module is OxcDiagnostic, which is used by all other oxc tools to report problems. It implements [miette]'s Diagnostic trait, making it compatible with other tooling you may be using.

use oxc_diagnostics::{OxcDiagnostic, Result};
fn my_tool() -> Result<()> {
    try_something().map_err(|e| OxcDiagnostic::error(e.to_string()))?;
    Ok(())
}

See the [miette] documentation for more information on how to interact with diagnostics.

Reporting

If you are writing your own tools that may produce their own errors, you can use DiagnosticService to format and render them to a string or a stream. It can receive Errors over a multi-producer, single consumer

use std::{sync::Arc, thread};
use oxc_diagnostics::{DiagnosticService, Error, OxcDiagnostic};

fn my_tool() -> Result<()> {
    try_something().map_err(|e| OxcDiagnostic::error(e.to_string()))?;
    Ok(())
}

let mut service = DiagnosticService::default();
let mut sender = service.sender().clone();

thread::spawn(move || {
    let file_path_being_processed = PathBuf::from("file.txt");
    let file_being_processed = Arc::new(NamedSource::new(file_path_being_processed.clone()));

    for _ in 0..10 {
        if let Err(diagnostic) = my_tool() {
            let report = diagnostic.with_source_code(Arc::clone(&file_being_processed));
            sender.send(Some(file_path_being_processed, vec![Error::new(e)]));
        }
        // send None to stop the service
        sender.send(None);
    }
});

service.run();

Dependencies

~2.5MB
~44K SLoC