update
This commit is contained in:
@ -182,25 +182,41 @@ export class PlatformError extends Error {
|
||||
): PlatformError {
|
||||
const nextRetryAt = Date.now() + retryDelay;
|
||||
|
||||
// Create a new instance with the same parameters but updated context
|
||||
// Clone the error with updated context
|
||||
const newContext = {
|
||||
...this.context,
|
||||
retry: {
|
||||
maxRetries,
|
||||
currentRetry,
|
||||
nextRetryAt,
|
||||
retryDelay
|
||||
}
|
||||
};
|
||||
|
||||
// Create a new instance using the protected method that subclasses can override
|
||||
const newError = this.createWithContext(newContext);
|
||||
|
||||
// Update recoverability if we can retry
|
||||
if (currentRetry < maxRetries && newError.recoverability === ErrorRecoverability.NON_RECOVERABLE) {
|
||||
(newError as any).recoverability = ErrorRecoverability.MAYBE_RECOVERABLE;
|
||||
}
|
||||
|
||||
return newError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected method to create a new instance with updated context
|
||||
* Subclasses can override this to handle their own constructor signatures
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
// Default implementation for PlatformError
|
||||
return new (this.constructor as typeof PlatformError)(
|
||||
this.message,
|
||||
this.code,
|
||||
this.severity,
|
||||
this.category,
|
||||
// If we can retry, the error is at least maybe recoverable
|
||||
currentRetry < maxRetries
|
||||
? ErrorRecoverability.MAYBE_RECOVERABLE
|
||||
: this.recoverability,
|
||||
{
|
||||
...this.context,
|
||||
retry: {
|
||||
maxRetries,
|
||||
currentRetry,
|
||||
nextRetryAt,
|
||||
retryDelay
|
||||
}
|
||||
}
|
||||
this.recoverability,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
@ -247,6 +263,18 @@ export class ValidationError extends PlatformError {
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
* Overrides the base implementation to handle ValidationError's constructor signature
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof ValidationError)(
|
||||
this.message,
|
||||
this.code,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -274,6 +302,18 @@ export class ConfigurationError extends PlatformError {
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
* Overrides the base implementation to handle ConfigurationError's constructor signature
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof ConfigurationError)(
|
||||
this.message,
|
||||
this.code,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -301,6 +341,18 @@ export class NetworkError extends PlatformError {
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
* Overrides the base implementation to handle NetworkError's constructor signature
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof NetworkError)(
|
||||
this.message,
|
||||
this.code,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -328,6 +380,18 @@ export class ResourceError extends PlatformError {
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
* Overrides the base implementation to handle ResourceError's constructor signature
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof ResourceError)(
|
||||
this.message,
|
||||
this.code,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -355,6 +419,18 @@ export class AuthenticationError extends PlatformError {
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
* Overrides the base implementation to handle AuthenticationError's constructor signature
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof AuthenticationError)(
|
||||
this.message,
|
||||
this.code,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -382,6 +458,18 @@ export class OperationError extends PlatformError {
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
* Overrides the base implementation to handle OperationError's constructor signature
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof OperationError)(
|
||||
this.message,
|
||||
this.code,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -34,6 +34,16 @@ export class EmailServiceError extends OperationError {
|
||||
) {
|
||||
super(message, EMAIL_SERVICE_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof EmailServiceError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -52,6 +62,16 @@ export class EmailTemplateError extends OperationError {
|
||||
) {
|
||||
super(message, EMAIL_TEMPLATE_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof EmailTemplateError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,6 +90,16 @@ export class EmailValidationError extends ValidationError {
|
||||
) {
|
||||
super(message, EMAIL_VALIDATION_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof EmailValidationError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -88,6 +118,16 @@ export class EmailSendError extends OperationError {
|
||||
) {
|
||||
super(message, EMAIL_SEND_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof EmailSendError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for a permanently failed send
|
||||
@ -161,6 +201,16 @@ export class EmailReceiveError extends OperationError {
|
||||
) {
|
||||
super(message, EMAIL_RECEIVE_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof EmailReceiveError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -256,6 +306,16 @@ export class EmailParseError extends OperationError {
|
||||
) {
|
||||
super(message, EMAIL_PARSE_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof EmailParseError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -274,6 +334,16 @@ export class EmailRateLimitError extends ResourceError {
|
||||
) {
|
||||
super(message, EMAIL_RATE_LIMIT_EXCEEDED, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof EmailRateLimitError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance with rate limit information
|
||||
|
@ -16,10 +16,17 @@ export * from './email.errors.js';
|
||||
export * from './mta.errors.js';
|
||||
export * from './reputation.errors.js';
|
||||
|
||||
// Export error handler
|
||||
export * from './error-handler.js';
|
||||
|
||||
// Export utility function to create specific error types based on the error category
|
||||
import { getErrorClassForCategory } from './base.errors.js';
|
||||
export { getErrorClassForCategory };
|
||||
|
||||
// Import needed classes for utility functions
|
||||
import { PlatformError } from './base.errors.js';
|
||||
import { ErrorSeverity, ErrorCategory, ErrorRecoverability } from './error.codes.js';
|
||||
|
||||
/**
|
||||
* Create a typed error from a standard Error
|
||||
* Useful for converting errors from external libraries or APIs
|
||||
@ -33,11 +40,7 @@ export function fromError(
|
||||
error: Error,
|
||||
code: string,
|
||||
contextData: Record<string, any> = {}
|
||||
) {
|
||||
// Import and use PlatformError
|
||||
const { PlatformError } = require('./base.errors.js');
|
||||
const { ErrorSeverity, ErrorCategory, ErrorRecoverability } = require('./error.codes.js');
|
||||
|
||||
): PlatformError {
|
||||
return new PlatformError(
|
||||
error.message,
|
||||
code,
|
||||
@ -66,7 +69,6 @@ export function fromError(
|
||||
export function isRetryable(error: any): boolean {
|
||||
// If it's our platform error, use its recoverability property
|
||||
if (error && typeof error === 'object' && 'recoverability' in error) {
|
||||
const { ErrorRecoverability } = require('./error.codes.js');
|
||||
return error.recoverability === ErrorRecoverability.RECOVERABLE ||
|
||||
error.recoverability === ErrorRecoverability.MAYBE_RECOVERABLE ||
|
||||
error.recoverability === ErrorRecoverability.TRANSIENT;
|
||||
|
@ -33,6 +33,16 @@ export class MtaConnectionError extends NetworkError {
|
||||
) {
|
||||
super(message, MTA_CONNECTION_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof MtaConnectionError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for a DNS resolution error
|
||||
@ -136,6 +146,16 @@ export class MtaAuthenticationError extends AuthenticationError {
|
||||
) {
|
||||
super(message, MTA_AUTHENTICATION_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof MtaAuthenticationError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for invalid credentials
|
||||
@ -209,6 +229,16 @@ export class MtaDeliveryError extends OperationError {
|
||||
) {
|
||||
super(message, MTA_DELIVERY_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof MtaDeliveryError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for a permanent delivery failure
|
||||
@ -322,6 +352,16 @@ export class MtaConfigurationError extends ConfigurationError {
|
||||
) {
|
||||
super(message, MTA_CONFIGURATION_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof MtaConfigurationError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for a missing configuration value
|
||||
@ -392,6 +432,16 @@ export class MtaDnsError extends NetworkError {
|
||||
) {
|
||||
super(message, MTA_DNS_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof MtaDnsError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for an MX record lookup failure
|
||||
@ -477,6 +527,16 @@ export class MtaTimeoutError extends NetworkError {
|
||||
) {
|
||||
super(message, MTA_TIMEOUT_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof MtaTimeoutError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for an SMTP command timeout
|
||||
@ -550,6 +610,16 @@ export class MtaProtocolError extends OperationError {
|
||||
) {
|
||||
super(message, MTA_PROTOCOL_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof MtaProtocolError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for an unexpected server response
|
||||
|
@ -51,6 +51,16 @@ export class ReputationCheckError extends ReputationError {
|
||||
) {
|
||||
super(message, REPUTATION_CHECK_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof ReputationCheckError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for an IP reputation check error
|
||||
@ -133,6 +143,16 @@ export class ReputationDataError extends ReputationError {
|
||||
) {
|
||||
super(message, REPUTATION_DATA_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof ReputationDataError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for a data access error
|
||||
@ -186,6 +206,16 @@ export class BlocklistError extends ReputationError {
|
||||
) {
|
||||
super(message, REPUTATION_BLOCKLIST_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof BlocklistError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for an entity found on a blocklist
|
||||
@ -237,6 +267,16 @@ export class ReputationUpdateError extends ReputationError {
|
||||
) {
|
||||
super(message, REPUTATION_UPDATE_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof ReputationUpdateError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -255,6 +295,16 @@ export class WarmupAllocationError extends ReputationError {
|
||||
) {
|
||||
super(message, WARMUP_ALLOCATION_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof WarmupAllocationError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for no available IPs
|
||||
@ -299,6 +349,16 @@ export class WarmupLimitError extends ResourceError {
|
||||
) {
|
||||
super(message, WARMUP_LIMIT_EXCEEDED, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof WarmupLimitError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance for daily sending limit exceeded
|
||||
@ -349,4 +409,14 @@ export class WarmupScheduleError extends ReputationError {
|
||||
) {
|
||||
super(message, WARMUP_SCHEDULE_ERROR, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with updated context
|
||||
*/
|
||||
protected createWithContext(context: IErrorContext): PlatformError {
|
||||
return new (this.constructor as typeof WarmupScheduleError)(
|
||||
this.message,
|
||||
context
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user