Files
xo-cash-utils/source/exponential-backoff/errors.ts
T

93 lines
3.7 KiB
TypeScript

/* eslint-disable max-classes-per-file */
/**
* Error thrown when the maximum number of retries is hit in an exponential backoff
*/
export class ExponentialBackoffMaxRetriesHitError extends Error {
constructor(errors: Array<Error>) {
super('Exponential backoff: Max retries hit', { cause: errors });
this.name = 'ExponentialBackoffMaxRetriesHitError';
}
}
/**
* Error thrown when the exponential backoff retries are stopped
*/
export class ExponentialBackoffStoppedRetriesError extends Error {
constructor(reason: unknown) {
// Convert the reason to an error if it is not an error
const reasonError = reason instanceof Error ? reason : new Error(`${reason}`);
super(`Exponential backoff was aborted: "${reasonError.message}"`, { cause: reasonError });
this.name = 'ExponentialBackoffStoppedRetriesError';
}
}
/**
* Error thrown when an exponential backoff option is too small
*/
export class ExponentialBackoffNumberTooSmallError extends Error {
constructor(option: string, value: number, min: number) {
super(`Exponential backoff option "${option}" is too small. Must be at least ${min}. Received value: ${value}`);
this.name = 'ExponentialBackoffNumberTooSmallError';
}
}
/**
* Error thrown when an exponential backoff option is out of bounds
*/
export class ExponentialBackoffNumberOutOfBoundsError extends Error {
constructor(option: string, value: number, min: number, max: number) {
super(`Exponential backoff option "${option}" is out of bounds. Must be between ${min} and ${max}. Received value: ${value}`);
this.name = 'ExponentialBackoffNumberOutOfBoundsError';
}
}
/**
* Error thrown when an exponential backoff option is an invalid infinite integer
*/
export class ExponentialBackoffNumberNotFiniteError extends Error {
constructor(option: string, value: number) {
super(`Exponential backoff option "${option}" is invalid. Must be a finite number. Received value: ${value}`);
this.name = 'ExponentialBackoffNumberNotFiniteError';
}
}
/**
* Error thrown when an exponential backoff option is not an integer
*/
export class ExponentialBackoffNonIntegerError extends Error {
constructor(option: string, value: number) {
super(`Exponential backoff option "${option}" is invalid. Must be an integer. Received value: ${value}`);
this.name = 'ExponentialBackoffNonIntegerError';
}
}
/**
* Error thrown when an externally aborted exponential backoff is aborted
* due to the external abort signal that was passed in to the constructor being aborted by an upstream consumer
*/
export class ExternallyAbortedExponentialBackoffExternalSignalAbortedError extends Error {
constructor(reason: unknown) {
// Convert the reason to an error if it is not an error
const reasonError = reason instanceof Error ? reason : new Error(`${reason}`);
super(`Externally aborted exponential backoff: "${reasonError.message}"`, { cause: reasonError });
this.name = 'ExternallyAbortedExponentialBackoffExternalSignalAbortedError';
}
}
/**
* Error thrown when an externally aborted exponential backoff is aborted
* due to the internal abort signal being aborted using the .abort() method
*/
export class ExternallyAbortedExponentialBackoffInternalSignalAbortedError extends Error {
constructor(reason: unknown) {
// Convert the reason to an error if it is not an error
const reasonError = reason instanceof Error ? reason : new Error(`${reason}`);
super(`Externally aborted exponential backoff: "${reasonError.message}"`, { cause: reasonError });
this.name = 'ExternallyAbortedExponentialBackoffInternalSignalAbortedError';
}
}