oxc-project/oxc · error · OxcDiagnostic

Exported error name should match error class

Error message

Exported error name should match error class

What it means

Lint diagnostic from unicorn/custom-error-definition (invalid_export_diagnostic): a module exports an error class under a name that does not match the class name (e.g. export { FooError as BarError } or export default). Mismatched names make stack traces and instanceof checks harder to follow.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/custom_error_definition.rs:33

fn invalid_class_name_diagnostic(span: Span, expected: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Invalid class name, use `{expected}`.")).with_label(span)
}

fn missing_super_call_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Missing call to `super()` in constructor.").with_label(span)
}

fn invalid_name_property_diagnostic(span: Span, name: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("The `name` property should be set to `{name}`.")).with_label(span)
}

fn pass_message_to_super_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Pass the error message to `super()` instead of setting `this.message`.")
        .with_label(span)
}

fn invalid_export_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Exported error name should match error class").with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct CustomErrorDefinition;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Enforces the only valid way of Error subclassing. It works with any super class that ends in Error.
    ///
    /// ### Why is this bad?
    ///
    /// Incorrectly defined custom errors can lead to unexpected behavior when
    /// catching and identifying errors. Missing `super()` calls, wrong `name`
    /// property values, or non-standard class names make error handling unreliable.
    ///
    /// ### Examples
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Export the class under its own name (export class FooError / export { FooError })
  2. Rename the class itself to the intended exported name
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/custom_error_definition.rs:33 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/9a6d92bce3709a4e. Report an issue: GitHub.