use-medusa-error-not-generic-error - ESLint plugin rules
This rule requires throwing MedusaError rather than a generic Error. MedusaError carries a type that Medusa maps to the right HTTP status and a consistent client response.
Severity#
warn. This rule is enabled in the recommended preset.
What it Targets#
This rule targets all files in your project. It reports throw statements that throw a built-in error constructor, such as Error or TypeError.
The following code is reported by the rule:
Instead, throw a MedusaError with an appropriate type:
Why it's Important#
carries a that Medusa maps to the correct HTTP status and a consistent client response. Throwing a generic error loses that structured information.
Learn more in the API route errors documentation.
Fixable#
This rule isn't auto-fixable with the --fix option, since you must choose the appropriate error type. However, it provides an editor suggestion that you can apply manually from your editor's quick-fix menu.
The suggestion rewrites the generic error to new MedusaError(MedusaError.Types.UNEXPECTED_STATE, ...), adds the MedusaError import if needed, and expects you to replace UNEXPECTED_STATE with a more specific error type, such as NOT_FOUND or INVALID_DATA.
Turn it Off#
To turn off this rule, set it to off in your ESLint configuration:
Or disable it for a single line using an inline comment: