From 0379dd942329dc1cae0b76ea01491a4fb8cceb81 Mon Sep 17 00:00:00 2001 From: Rogger Valverde Date: Sun, 3 Dec 2023 20:59:53 -0600 Subject: [PATCH] refactor(erros): convert rate limit error into a new class definition (#2311) --- src/classes/{ => errors}/delayed-error.ts | 0 src/classes/errors/index.ts | 4 ++++ src/classes/errors/rate-limit-error.ts | 15 +++++++++++++++ src/classes/{ => errors}/unrecoverable-error.ts | 0 .../{ => errors}/waiting-children-error.ts | 0 src/classes/index.ts | 4 +--- src/classes/job.ts | 2 +- src/classes/worker.ts | 12 +++++++----- tests/fixtures/fixture_processor_unrecoverable.js | 4 +--- 9 files changed, 29 insertions(+), 12 deletions(-) rename src/classes/{ => errors}/delayed-error.ts (100%) create mode 100644 src/classes/errors/index.ts create mode 100644 src/classes/errors/rate-limit-error.ts rename src/classes/{ => errors}/unrecoverable-error.ts (100%) rename src/classes/{ => errors}/waiting-children-error.ts (100%) diff --git a/src/classes/delayed-error.ts b/src/classes/errors/delayed-error.ts similarity index 100% rename from src/classes/delayed-error.ts rename to src/classes/errors/delayed-error.ts diff --git a/src/classes/errors/index.ts b/src/classes/errors/index.ts new file mode 100644 index 0000000000..dde044104f --- /dev/null +++ b/src/classes/errors/index.ts @@ -0,0 +1,4 @@ +export * from './delayed-error'; +export * from './unrecoverable-error'; +export * from './rate-limit-error'; +export * from './waiting-children-error'; diff --git a/src/classes/errors/rate-limit-error.ts b/src/classes/errors/rate-limit-error.ts new file mode 100644 index 0000000000..4d2a8fbf0c --- /dev/null +++ b/src/classes/errors/rate-limit-error.ts @@ -0,0 +1,15 @@ +export const RATE_LIMIT_ERROR = 'bullmq:rateLimitExceeded'; + +/** + * RateLimitError + * + * Error to be thrown when queue reaches a rate limit. + * + */ +export class RateLimitError extends Error { + constructor(message: string = RATE_LIMIT_ERROR) { + super(message); + this.name = this.constructor.name; + Object.setPrototypeOf(this, new.target.prototype); + } +} diff --git a/src/classes/unrecoverable-error.ts b/src/classes/errors/unrecoverable-error.ts similarity index 100% rename from src/classes/unrecoverable-error.ts rename to src/classes/errors/unrecoverable-error.ts diff --git a/src/classes/waiting-children-error.ts b/src/classes/errors/waiting-children-error.ts similarity index 100% rename from src/classes/waiting-children-error.ts rename to src/classes/errors/waiting-children-error.ts diff --git a/src/classes/index.ts b/src/classes/index.ts index edd36aebcc..314bb8f0b8 100644 --- a/src/classes/index.ts +++ b/src/classes/index.ts @@ -2,7 +2,7 @@ export * from './async-fifo-queue'; export * from './backoffs'; export * from './child-pool'; export * from './child-processor'; -export * from './delayed-error'; +export * from './errors'; export * from './flow-producer'; export * from './job'; // export * from './main'; this file must not be exported @@ -16,6 +16,4 @@ export * from './redis-connection'; export * from './repeat'; export * from './sandbox'; export * from './scripts'; -export * from './unrecoverable-error'; -export * from './waiting-children-error'; export * from './worker'; diff --git a/src/classes/job.ts b/src/classes/job.ts index 6f30b23de8..dfd6207039 100644 --- a/src/classes/job.ts +++ b/src/classes/job.ts @@ -32,7 +32,7 @@ import { } from '../utils'; import { Backoffs } from './backoffs'; import { Scripts } from './scripts'; -import { UnrecoverableError } from './unrecoverable-error'; +import { UnrecoverableError } from './errors/unrecoverable-error'; import type { QueueEvents } from './queue-events'; const logger = debuglog('bull'); diff --git a/src/classes/worker.ts b/src/classes/worker.ts index c2ff6b6658..908b31ec71 100644 --- a/src/classes/worker.ts +++ b/src/classes/worker.ts @@ -29,8 +29,12 @@ import { Job } from './job'; import { RedisConnection } from './redis-connection'; import sandbox from './sandbox'; import { AsyncFifoQueue } from './async-fifo-queue'; -import { DelayedError } from './delayed-error'; -import { WaitingChildrenError } from './waiting-children-error'; +import { + DelayedError, + RateLimitError, + RATE_LIMIT_ERROR, + WaitingChildrenError, +} from './errors'; // 10 seconds is the maximum time a BRPOPLPUSH can block. const maximumBlockTimeout = 10; @@ -147,8 +151,6 @@ export interface WorkerListener< stalled: (jobId: string, prev: string) => void; } -const RATE_LIMIT_ERROR = 'bullmq:rateLimitExceeded'; - /** * * This class represents a worker that is able to process jobs from the queue. @@ -186,7 +188,7 @@ export class Worker< protected running = false; static RateLimitError(): Error { - return new Error(RATE_LIMIT_ERROR); + return new RateLimitError(); } constructor( diff --git a/tests/fixtures/fixture_processor_unrecoverable.js b/tests/fixtures/fixture_processor_unrecoverable.js index 27a63e7b4b..3d6b2ece0b 100644 --- a/tests/fixtures/fixture_processor_unrecoverable.js +++ b/tests/fixtures/fixture_processor_unrecoverable.js @@ -4,9 +4,7 @@ */ 'use strict'; -const { - UnrecoverableError, -} = require('../../dist/cjs/classes/unrecoverable-error'); +const { UnrecoverableError } = require('../../dist/cjs/classes'); const delay = require('./delay'); module.exports = function (job) {