UNPKG

chargebee

Version:

A library for integrating with Chargebee.

95 lines (94 loc) 2.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebhookPayloadParseError = exports.WebhookPayloadValidationError = exports.WebhookAuthenticationError = exports.WebhookError = void 0; /** * Base class for all webhook-related errors. * Extends the standard Error class with proper stack trace support. */ class WebhookError extends Error { constructor(message) { var _a; super(message); this.name = 'WebhookError'; // Maintains proper stack trace for where error was thrown (V8 only) (_a = Error.captureStackTrace) === null || _a === void 0 ? void 0 : _a.call(Error, this, this.constructor); } } exports.WebhookError = WebhookError; /** * Authentication error thrown when webhook request authentication fails. * * Common scenarios: * - Missing authorization header * - Invalid authorization header format * - Invalid credentials * * Typically maps to HTTP 401 Unauthorized. * * @example * ```typescript * handler.on('error', (error, { response }) => { * if (error instanceof WebhookAuthenticationError) { * response?.status(401).json({ error: 'Unauthorized' }); * } * }); * ``` */ class WebhookAuthenticationError extends WebhookError { constructor(message) { super(message); this.name = 'WebhookAuthenticationError'; } } exports.WebhookAuthenticationError = WebhookAuthenticationError; /** * Payload validation error thrown when the webhook payload structure is invalid. * * Common scenarios: * - Missing required fields (event_type, id) * - Invalid field types * - Malformed payload structure * * Typically maps to HTTP 400 Bad Request. * * @example * ```typescript * handler.on('error', (error, { response }) => { * if (error instanceof WebhookPayloadValidationError) { * response?.status(400).json({ error: 'Bad Request', message: error.message }); * } * }); * ``` */ class WebhookPayloadValidationError extends WebhookError { constructor(message) { super(message); this.name = 'WebhookPayloadValidationError'; } } exports.WebhookPayloadValidationError = WebhookPayloadValidationError; /** * JSON parsing error thrown when the webhook body cannot be parsed as JSON. * * Includes the raw body that failed to parse (if available) for debugging. * * Typically maps to HTTP 400 Bad Request. * * @example * ```typescript * handler.on('error', (error, { response }) => { * if (error instanceof WebhookPayloadParseError) { * console.error('Failed to parse:', error.rawBody); * response?.status(400).json({ error: 'Invalid JSON' }); * } * }); * ``` */ class WebhookPayloadParseError extends WebhookError { constructor(message, rawBody) { super(message); this.rawBody = rawBody; this.name = 'WebhookPayloadParseError'; } } exports.WebhookPayloadParseError = WebhookPayloadParseError;