diff --git a/src/job/run.ts b/src/job/run.ts index c05278b..c837d75 100644 --- a/src/job/run.ts +++ b/src/job/run.ts @@ -1,5 +1,6 @@ import createDebugger from 'debug'; import { Job } from '.'; +import { JobError } from '../utils'; const debug = createDebugger('pulse:job'); @@ -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) { diff --git a/src/pulse/database.ts b/src/pulse/database.ts index 9b5e5a7..6b7d93b 100644 --- a/src/pulse/database.ts +++ b/src/pulse/database.ts @@ -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'); @@ -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; diff --git a/src/pulse/save-job.ts b/src/pulse/save-job.ts index a64ed44..efe605d 100644 --- a/src/pulse/save-job.ts +++ b/src/pulse/save-job.ts @@ -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'); @@ -72,6 +72,10 @@ export type SaveJobMethod = (job: Job) => Promise; */ 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 diff --git a/src/utils/error.ts b/src/utils/error.ts new file mode 100644 index 0000000..723c0e8 --- /dev/null +++ b/src/utils/error.ts @@ -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); + } +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 54c0efb..3cf45d4 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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'; diff --git a/src/utils/process-jobs.ts b/src/utils/process-jobs.ts index e301f4a..269cbe0 100644 --- a/src/utils/process-jobs.ts +++ b/src/utils/process-jobs.ts @@ -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'); @@ -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