1 ---
2 sidebar_label: "use-medusa-error-not-generic-error"
3 ---
4
5 export const metadata = {
6 title: `use-medusa-error-not-generic-error - ESLint plugin rules`,
7 }
8
9 # {metadata.title}
10
11 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.
12
13 ## Severity
14
15 `warn`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset.
16
17 ## What it Targets
18
19 This rule targets all files in your project. It reports `throw` statements that throw a built-in error constructor, such as `Error` or `TypeError`.
20
21 The following code is reported by the rule:
22
23 ```ts title="src/api/example.ts"
24 // non-compliant
25 throw new Error("Something went wrong")
26 ```
27
28 Instead, throw a `MedusaError` with an appropriate type:
29
30 ```ts title="src/api/example.ts"
31 // compliant
32 import { MedusaError } from "@medusajs/framework/utils"
33
34 throw new MedusaError(
35 MedusaError.Types.NOT_FOUND,
36 "Something went wrong"
37 )
38 ```
39
40 ## Why it's Important
41
42 `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.
43
44 Learn more in the [API route errors documentation](!docs!/learn/fundamentals/api-routes/errors).
45
46 ## Fixable
47
48 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.
49
50 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](!docs!/learn/fundamentals/api-routes/errors), such as `NOT_FOUND` or `INVALID_DATA`.
51
52 ## Turn it Off
53
54 To turn off this rule, set it to `off` in your ESLint configuration:
55
56 ```ts title="eslint.config.ts"
57 import { defineConfig } from "eslint/config"
58 import medusa from "@medusajs/eslint-plugin"
59
60 export default defineConfig([
61 ...medusa.configs.recommended,
62 {
63 rules: {
64 "@medusajs/use-medusa-error-not-generic-error": "off",
65 },
66 },
67 ])
68 ```
69
70 Or disable it for a single line using an inline comment:
71
72 ```ts
73 // eslint-disable-next-line @medusajs/use-medusa-error-not-generic-error
74 throw new Error("Something went wrong")
75 ```