Files
medusa-store/packages/medusa-core-utils/src/errors.ts
Adrien de Peretti aaebb38eae feat(medusa): Convert IdempotencyKeyService to TypeScript (#1995)
* feat(medusa): Migrate the idempotency key service to ts + fix

* feat(medusa): Finalise idempotency migration

* Create late-owls-pump.md

* feat(medusa): Polish

* feat(medusa): Add case to the error handler

* feat(medusa): Add case to the error handler

Co-authored-by: olivermrbl <oliver@mrbltech.com>
2022-08-09 08:08:07 +02:00

56 lines
1.4 KiB
TypeScript

/**
* @typedef MedusaErrorType
*
*/
export const MedusaErrorTypes = {
/** Errors stemming from the database */
DB_ERROR: "database_error",
DUPLICATE_ERROR: "duplicate_error",
INVALID_ARGUMENT: "invalid_argument",
INVALID_DATA: "invalid_data",
NOT_FOUND: "not_found",
NOT_ALLOWED: "not_allowed",
UNEXPECTED_STATE: "unexpected_state",
CONFLICT: "conflict",
}
export const MedusaErrorCodes = {
INSUFFICIENT_INVENTORY: "insufficient_inventory",
CART_INCOMPATIBLE_STATE: "cart_incompatible_state",
}
/**
* Standardized error to be used across Medusa project.
* @extends Error
*/
class MedusaError extends Error {
public type: string
public message: string
public code?: string
public date: Date
public static Types = MedusaErrorTypes
public static Codes = MedusaErrorCodes
/**
* Creates a standardized error to be used across Medusa project.
* @param {string} type - type of error
* @param {string} message - message to go along with error
* @param {string} code - code of error
* @param {Array} params - params
*/
constructor(type: string, message: string, code?: string, ...params: any) {
super(...params)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, MedusaError)
}
this.type = type
this.code = code
this.message = message
this.date = new Date()
}
}
export default MedusaError