UNPKG

@api-buddy/sendgrid

Version:

API Buddy integration for SendGrid - Email delivery service for transactional and marketing emails

115 lines 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SendGridApiError = void 0; exports.handleSendGridError = handleSendGridError; exports.isSendGridError = isSendGridError; exports.isRateLimitError = isRateLimitError; exports.isAuthError = isAuthError; exports.isValidationError = isValidationError; /** * Custom error class for SendGrid API errors */ class SendGridApiError extends Error { constructor(error) { const message = error.response?.body?.errors?.[0]?.message || error.message; super(message); this.name = 'SendGridApiError'; this.code = error.code; this.status = error.code; this.headers = error.response?.headers || {}; this.body = error.response?.body || {}; this.errors = error.response?.body?.errors || []; // Maintains proper stack trace for where our error was thrown if (Error.captureStackTrace) { Error.captureStackTrace(this, SendGridApiError); } } /** * Check if the error is a rate limit error */ get isRateLimit() { return this.status === 429; } /** * Check if the error is an authentication error */ get isAuthError() { return this.status === 401 || this.status === 403; } /** * Check if the error is a validation error */ get isValidationError() { return this.status === 400; } /** * Get the first error message */ get firstErrorMessage() { return this.errors[0]?.message || this.message; } } exports.SendGridApiError = SendGridApiError; /** * Handle SendGrid API errors * @param error The error to handle * @returns A SendGridApiError instance */ function handleSendGridError(error) { if (error instanceof SendGridApiError) { return error; } const sendGridError = error; if (sendGridError.response) { return new SendGridApiError(sendGridError); } // Handle network errors if (error instanceof Error) { const networkError = new SendGridApiError({ message: error.message, name: 'NetworkError', code: 0, }); networkError.stack = error.stack; return networkError; } // Handle unknown errors return new SendGridApiError({ message: 'An unknown error occurred', name: 'UnknownError', code: 0, }); } /** * Check if an error is a SendGrid API error * @param error The error to check * @returns True if the error is a SendGrid API error */ function isSendGridError(error) { return error instanceof SendGridApiError; } /** * Check if an error is a rate limit error * @param error The error to check * @returns True if the error is a rate limit error */ function isRateLimitError(error) { return isSendGridError(error) && error.isRateLimit; } /** * Check if an error is an authentication error * @param error The error to check * @returns True if the error is an authentication error */ function isAuthError(error) { return isSendGridError(error) && error.isAuthError; } /** * Check if an error is a validation error * @param error The error to check * @returns True if the error is a validation error */ function isValidationError(error) { return isSendGridError(error) && error.isValidationError; } //# sourceMappingURL=errors.js.map