@api-buddy/sendgrid
Version:
API Buddy integration for SendGrid - Email delivery service for transactional and marketing emails
129 lines (111 loc) • 3.35 kB
text/typescript
import { SendGridError } from '../types';
/**
* Custom error class for SendGrid API errors
*/
export class SendGridApiError extends Error {
code: number;
status: number;
headers: Record<string, string>;
body: any;
errors: Array<{ message: string; field?: string; help?: string }>;
constructor(error: SendGridError) {
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;
}
}
/**
* Handle SendGrid API errors
* @param error The error to handle
* @returns A SendGridApiError instance
*/
export function handleSendGridError(error: unknown): SendGridApiError {
if (error instanceof SendGridApiError) {
return error;
}
const sendGridError = error as SendGridError;
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,
} as any);
networkError.stack = error.stack;
return networkError;
}
// Handle unknown errors
return new SendGridApiError({
message: 'An unknown error occurred',
name: 'UnknownError',
code: 0,
} as any);
}
/**
* 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
*/
export function isSendGridError(error: unknown): error is SendGridApiError {
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
*/
export function isRateLimitError(error: unknown): boolean {
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
*/
export function isAuthError(error: unknown): boolean {
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
*/
export function isValidationError(error: unknown): boolean {
return isSendGridError(error) && error.isValidationError;
}