Skip to content

Commit

Permalink
refactor(erros): convert rate limit error into a new class definition (
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Dec 4, 2023
1 parent 9896b8a commit 0379dd9
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 12 deletions.
File renamed without changes.
4 changes: 4 additions & 0 deletions src/classes/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './delayed-error';
export * from './unrecoverable-error';
export * from './rate-limit-error';
export * from './waiting-children-error';
15 changes: 15 additions & 0 deletions src/classes/errors/rate-limit-error.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
File renamed without changes.
File renamed without changes.
4 changes: 1 addition & 3 deletions src/classes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
2 changes: 1 addition & 1 deletion src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 7 additions & 5 deletions src/classes/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -186,7 +188,7 @@ export class Worker<
protected running = false;

static RateLimitError(): Error {
return new Error(RATE_LIMIT_ERROR);
return new RateLimitError();
}

constructor(
Expand Down
4 changes: 1 addition & 3 deletions tests/fixtures/fixture_processor_unrecoverable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0379dd9

Please # to comment.