81 releases (45 breaking)

new 0.45.0 Jan 11, 2025
0.44.0 Dec 25, 2024
0.43.0 Dec 21, 2024
0.38.0 Nov 26, 2024
0.0.1 Mar 30, 2023

#1236 in Programming languages

Download history 50229/week @ 2024-09-21 58251/week @ 2024-09-28 47210/week @ 2024-10-05 41086/week @ 2024-10-12 17998/week @ 2024-10-19 15068/week @ 2024-10-26 14649/week @ 2024-11-02 16630/week @ 2024-11-09 17475/week @ 2024-11-16 11946/week @ 2024-11-23 15756/week @ 2024-11-30 15421/week @ 2024-12-07 14692/week @ 2024-12-14 7378/week @ 2024-12-21 9758/week @ 2024-12-28 12345/week @ 2025-01-04

46,633 downloads per month
Used in 28 crates (7 directly)

MIT license

41KB
648 lines

Feature gating napi in other crates will lead to symbol not found when compiling, use this crate for common napi interfaces.


lib.rs:

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
~43K SLoC