CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-medusa-core-utils

Core utilities for Medusa e-commerce platform including error handling, DI container, amount calculations, and configuration parsing

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

error-handling.mddocs/

Error Handling

Standardized error handling system for Medusa applications, providing consistent error types, codes, and structured error information across the platform.

Capabilities

MedusaError Class

A standardized error class that extends the native JavaScript Error with additional metadata including error types, codes, and timestamps.

/**
 * Standardized error to be used across Medusa project
 * @extends Error
 */
class MedusaError extends Error {
  /**
   * Creates a standardized error to be used across Medusa project
   * @param type - type of error from MedusaErrorTypes
   * @param message - message to go along with error
   * @param code - optional error code from MedusaErrorCodes
   * @param params - additional parameters passed to Error constructor
   */
  constructor(type: string, message: string, code?: string, ...params: any);
  
  /** The type of error, should be one of MedusaErrorTypes */
  public type: string;
  /** The error message */
  public message: string;
  /** Optional error code for more specific error identification */
  public code?: string;
  /** Timestamp when the error was created */
  public date: Date;
  /** Static reference to error types */
  public static Types: typeof MedusaErrorTypes;
  /** Static reference to error codes */
  public static Codes: typeof MedusaErrorCodes;
}

Usage Examples:

import { MedusaError } from "medusa-core-utils";

// Basic error with type and message
throw new MedusaError(
  MedusaError.Types.NOT_FOUND,
  "User not found"
);

// Error with type, message, and code
throw new MedusaError(
  MedusaError.Types.INVALID_DATA,
  "Insufficient inventory",
  MedusaError.Codes.INSUFFICIENT_INVENTORY
);

// Catching and handling Medusa errors
try {
  // some operation
} catch (error) {
  if (error instanceof MedusaError) {
    console.log(`Error type: ${error.type}`);
    console.log(`Error code: ${error.code}`);
    console.log(`Occurred at: ${error.date}`);
  }
}

Error Types

Standard error type constants accessible through MedusaError.Types for consistent error categorization.

// Access through MedusaError.Types static property
MedusaError.Types: {
  /** Errors stemming from the database */
  DB_ERROR: "database_error";
  /** Duplicate entry or resource conflict errors */
  DUPLICATE_ERROR: "duplicate_error";
  /** Invalid argument passed to a function */
  INVALID_ARGUMENT: "invalid_argument";
  /** Invalid data format or content */
  INVALID_DATA: "invalid_data";
  /** Authentication or authorization errors */
  UNAUTHORIZED: "unauthorized";
  /** Resource not found errors */
  NOT_FOUND: "not_found";
  /** Operation not allowed in current context */
  NOT_ALLOWED: "not_allowed";
  /** System in unexpected state */
  UNEXPECTED_STATE: "unexpected_state";
  /** Resource conflict errors */
  CONFLICT: "conflict";
  /** Payment authorization failures */
  PAYMENT_AUTHORIZATION_ERROR: "payment_authorization_error";
};

Error Codes

Specific error codes accessible through MedusaError.Codes for more granular error identification within error types.

// Access through MedusaError.Codes static property
MedusaError.Codes: {
  /** Insufficient inventory for requested quantity */
  INSUFFICIENT_INVENTORY: "insufficient_inventory";
  /** Shopping cart is in incompatible state for operation */
  CART_INCOMPATIBLE_STATE: "cart_incompatible_state";
};

Usage Examples:

import { MedusaError } from "medusa-core-utils";

// Using error types and codes together
if (inventory < requestedQuantity) {
  throw new MedusaError(
    MedusaError.Types.INVALID_DATA,
    "Not enough items in stock",
    MedusaError.Codes.INSUFFICIENT_INVENTORY
  );
}

// Database operation error
try {
  await database.save(entity);
} catch (dbError) {
  throw new MedusaError(
    MedusaError.Types.DB_ERROR,
    "Failed to save entity to database",
    undefined,
    dbError
  );
}

// Authorization error
if (!user.hasPermission("admin")) {
  throw new MedusaError(
    MedusaError.Types.UNAUTHORIZED,
    "Admin access required"
  );
}

docs

amount-utilities.md

container-di.md

data-utilities.md

error-handling.md

index.md

server-utilities.md

utility-functions.md

tile.json