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:

src/api/example.ts
1// non-compliant2throw new Error("Something went wrong")

Instead, throw a MedusaError with an appropriate type:

src/api/example.ts
1// compliant2import { MedusaError } from "@medusajs/framework/utils"3
4throw new MedusaError(5  MedusaError.Types.NOT_FOUND,6  "Something went wrong"7)

Why it's Important#

MedusaError carries a type 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:

eslint.config.ts
1import { defineConfig } from "eslint/config"2import medusa from "@medusajs/eslint-plugin"3
4export default defineConfig([5  ...medusa.configs.recommended,6  {7    rules: {8      "@medusajs/use-medusa-error-not-generic-error": "off",9    },10  },11])

Or disable it for a single line using an inline comment:

Code
1// eslint-disable-next-line @medusajs/use-medusa-error-not-generic-error2throw new Error("Something went wrong")
Was this page helpful?
Ask Bloom
For assistance in your development, use Claude Code Plugins or Docs MCP server in Cursor, VSCode, etc...FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break
⇧↵