UNPKG

mercadopago

Version:
126 lines (125 loc) 5.14 kB
"use strict"; /** * Typed exception hierarchy for MercadoPago API errors. * * All exceptions extend {@link MercadoPagoError}, which itself extends {@link Error}. * Existing code that checks `error.status`, `error.message`, `error.error`, or * `error.causes` continues to work unchanged. * * CWE-209: The `Authorization` header value is never stored in error objects; * errors are built from the API *response* body, not the outgoing request. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MPConnectionError = exports.MPServerError = exports.MPRateLimitError = exports.MPDependencyError = exports.MPResourceLockedError = exports.MPValidationError = exports.MPIdempotencyError = exports.MPNotFoundError = exports.MPForbiddenError = exports.MPPaymentError = exports.MPAuthenticationError = exports.MPBadRequestError = exports.MercadoPagoError = void 0; exports.buildError = buildError; /** * Base class for all MercadoPago API errors. * * Preserves backward-compatible properties (`status`, `message`, `error`, `causes`) * that existing catch blocks may already reference. */ class MercadoPagoError extends Error { constructor(body) { var _a, _b, _c; const msg = body.message || body.error || 'MercadoPago API error'; super(msg); this.name = this.constructor.name; this.status = (_a = body.status) !== null && _a !== void 0 ? _a : 0; this.error = (_b = body.error) !== null && _b !== void 0 ? _b : ''; this.causes = (_c = body.cause) !== null && _c !== void 0 ? _c : []; // Maintain prototype chain in compiled JavaScript Object.setPrototypeOf(this, new.target.prototype); } } exports.MercadoPagoError = MercadoPagoError; /** HTTP 400 Bad Request — validation or syntax error. */ class MPBadRequestError extends MercadoPagoError { } exports.MPBadRequestError = MPBadRequestError; /** HTTP 401 Unauthorized — missing or invalid credentials. */ class MPAuthenticationError extends MercadoPagoError { } exports.MPAuthenticationError = MPAuthenticationError; /** HTTP 402 Payment Required — transaction processing error (AP/Orders). */ class MPPaymentError extends MercadoPagoError { } exports.MPPaymentError = MPPaymentError; /** HTTP 403 Forbidden. */ class MPForbiddenError extends MercadoPagoError { } exports.MPForbiddenError = MPForbiddenError; /** HTTP 404 Not Found. */ class MPNotFoundError extends MercadoPagoError { } exports.MPNotFoundError = MPNotFoundError; /** * HTTP 409 Conflict — idempotency-key conflict or state-machine conflict. * Use `MPOrderErrors` constants to distinguish sub-cases via `error.error`. */ class MPIdempotencyError extends MercadoPagoError { } exports.MPIdempotencyError = MPIdempotencyError; /** HTTP 422 Unprocessable Entity — business-rule violation. */ class MPValidationError extends MercadoPagoError { } exports.MPValidationError = MPValidationError; /** HTTP 423 Locked — idempotency key temporarily locked (retryable). */ class MPResourceLockedError extends MercadoPagoError { } exports.MPResourceLockedError = MPResourceLockedError; /** HTTP 424 Failed Dependency — internal dependency failure (retryable). */ class MPDependencyError extends MercadoPagoError { } exports.MPDependencyError = MPDependencyError; /** * HTTP 429 Too Many Requests. * Exposes `retryAfter` (seconds) from the `Retry-After` response header. */ class MPRateLimitError extends MercadoPagoError { constructor(body, retryAfter = null) { super(body); this.retryAfter = retryAfter; } } exports.MPRateLimitError = MPRateLimitError; /** HTTP 5xx Server Error. */ class MPServerError extends MercadoPagoError { } exports.MPServerError = MPServerError; /** Transport-level or network error (timeout, DNS failure, etc.). */ class MPConnectionError extends MercadoPagoError { constructor(cause) { const msg = cause instanceof Error ? cause.message : String(cause); super({ message: msg, error: 'connection_error' }); this.__cause__ = cause; } } exports.MPConnectionError = MPConnectionError; const STATUS_MAP = { 400: MPBadRequestError, 401: MPAuthenticationError, 402: MPPaymentError, 403: MPForbiddenError, 404: MPNotFoundError, 409: MPIdempotencyError, 422: MPValidationError, 423: MPResourceLockedError, 424: MPDependencyError, }; /** * Factory: maps an HTTP status code to the most specific error subtype. * * @param status HTTP status code from the API response. * @param body Parsed API error body. * @param retryAfter Seconds from the `Retry-After` header (only for 429). */ function buildError(status, body, retryAfter = null) { if (status === 429) return new MPRateLimitError(Object.assign(Object.assign({}, body), { status }), retryAfter); const Cls = STATUS_MAP[status]; if (Cls) return new Cls(Object.assign(Object.assign({}, body), { status })); if (status >= 500) return new MPServerError(Object.assign(Object.assign({}, body), { status })); return new MercadoPagoError(Object.assign(Object.assign({}, body), { status })); }