oxc-project/oxc · error · OxcDiagnostic

Pass the error message to `super()` instead of setting `this

Error message

Pass the error message to `super()` instead of setting `this.message`.

What it means

Lint diagnostic from unicorn/custom-error-definition: a custom error subclass sets this.message in its constructor body. That field is not enumerable and skips Error's normal initialization path; the message must be passed to super() for the error to behave like a native Error.

Source

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

use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};

use crate::{AstNode, ast_util, context::LintContext, rule::Rule};

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

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Call super(message) in the constructor and delete the this.message assignment
  2. Forward a default message when none is provided, e.g. super(message ?? 'MyError')
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/custom_error_definition.rs:28 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/1ce34d10ebf14fa4. Report an issue: GitHub.