Skip to content

Commit

Permalink
fix: add BaseError and JobError classes to handle custom errors
Browse files Browse the repository at this point in the history
  • Loading branch information
code-xhyun committed May 3, 2024
1 parent 313f929 commit b2f7092
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/job/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import createDebugger from 'debug';
import { Job } from '.';
import { JobError } from '../utils';

const debug = createDebugger('pulse:job');

Expand Down Expand Up @@ -77,7 +78,7 @@ export const run: RunMethod = async function (this: Job) {
debug('[%s:%s] starting job', this.attrs.name, this.attrs._id);
if (!definition) {
debug('[%s:%s] has no definition, can not run', this.attrs.name, this.attrs._id);
throw new Error('Undefined job');
throw new JobError('Undefined job');
}

if (definition.fn.length === 2) {
Expand Down
3 changes: 2 additions & 1 deletion src/pulse/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import createDebugger from 'debug';
import { AnyError, Collection, MongoClient, MongoClientOptions } from 'mongodb';
import { Pulse } from '.';
import { BaseError } from '../utils';
import { hasMongoProtocol } from './has-mongo-protocol';

const debug = createDebugger('pulse:database');
Expand Down Expand Up @@ -46,7 +47,7 @@ export const database: DatabaseMethod = async function (this: Pulse, url, collec
});

if (!client) {
throw new Error('Mongo Client is undefined');
throw new BaseError('Mongo Client is undefined');
}

this._db = client;
Expand Down
6 changes: 5 additions & 1 deletion src/pulse/save-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import createDebugger from 'debug';
import { ObjectId } from 'mongodb';
import { Pulse } from '.';
import { Job } from '../job';
import { processJobs } from '../utils';
import { JobError, processJobs } from '../utils';

const debug = createDebugger('pulse:saveJob');

Expand Down Expand Up @@ -72,6 +72,10 @@ export type SaveJobMethod = (job: Job) => Promise<Job>;
*/
export const saveJob: SaveJobMethod = async function (this: Pulse, job) {
try {
if (!this._collection) {
throw new JobError('A db must be set up before you can save a job');
}

debug('attempting to save a job into Pulse instance');

// Grab information needed to save job but that we don't want to persist in MongoDB
Expand Down
15 changes: 15 additions & 0 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class BaseError extends Error {
constructor(public message: string) {
super(message);
this.name = this.constructor.name;
Object.setPrototypeOf(this, new.target.prototype);
}
}

export class JobError extends BaseError {
constructor(public message: string) {
super(message);
this.name = this.constructor.name;
Object.setPrototypeOf(this, new.target.prototype);
}
}
7 changes: 4 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./create-job";
export * from "./parse-priority";
export * from "./process-jobs";
export * from './create-job';
export * from './error';
export * from './parse-priority';
export * from './process-jobs';
3 changes: 2 additions & 1 deletion src/utils/process-jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import createDebugger from 'debug';
import { Job } from '../job';
import { Pulse } from '../pulse';
import { createJob } from './create-job';
import { JobError } from './error';

const debug = createDebugger('pulse:internal:processJobs');

Expand Down Expand Up @@ -317,7 +318,7 @@ export const processJobs = async function (this: Pulse, extraJob: Job): Promise<
// Job isn't in running jobs so throw an error
if (!self._runningJobs.includes(job)) {
debug('[%s] callback was called, job must have been marked as complete already', job.attrs._id);
throw new Error(`callback already called - job ${name} already marked complete`);
throw new JobError(`callback already called - job ${name} already marked complete`);
}

// Remove the job from the running queue
Expand Down

0 comments on commit b2f7092

Please sign in to comment.