1 /**
2 * @typedef MedusaErrorType
3 *
4 */
5 export const MedusaErrorTypes = {
6 /** Errors stemming from the database */
7 DB_ERROR: "database_error",
8 DUPLICATE_ERROR: "duplicate_error",
9 INVALID_ARGUMENT: "invalid_argument",
10 INVALID_DATA: "invalid_data",
11 UNAUTHORIZED: "unauthorized",
12 FORBIDDEN: "forbidden",
13 NOT_FOUND: "not_found",
14 NOT_ALLOWED: "not_allowed",
15 UNEXPECTED_STATE: "unexpected_state",
16 CONFLICT: "conflict",
17 UNKNOWN_MODULES: "unknown_modules",
18 PAYMENT_AUTHORIZATION_ERROR: "payment_authorization_error",
19 PAYMENT_REQUIRES_MORE_ERROR: "payment_requires_more_error",
20 }
21
22 export const MedusaErrorCodes = {
23 INSUFFICIENT_INVENTORY: "insufficient_inventory",
24 CART_INCOMPATIBLE_STATE: "cart_incompatible_state",
25 UNKNOWN_MODULES: "unknown_modules",
26 }
27
28 /**
29 * Standardized error to be used across Medusa project.
30 * @extends Error
31 */
32 export class MedusaError extends Error {
33 __isMedusaError = true
34
35 public type: string
36 public message: string
37 public code?: string
38 public date: Date
39 public static Types = MedusaErrorTypes
40 public static Codes = MedusaErrorCodes
41
42 /**
43 * Creates a standardized error to be used across Medusa project.
44 * @param {string} type - type of error
45 * @param {string} message - message to go along with error
46 * @param {string} code - code of error
47 * @param {Array} params - params
48 */
49 constructor(type: string, message: string, code?: string, ...params: any) {
50 super(...params)
51
52 if (Error.captureStackTrace) {
53 Error.captureStackTrace(this, MedusaError)
54 }
55
56 this.type = type
57 this.code = code
58 this.message = message
59 this.date = new Date()
60 }
61
62 /**
63 * Checks the object for the MedusaError type.
64 */
65 static isMedusaError(error: any): error is MedusaError {
66 return !!error.__isMedusaError
67 }
68 }