replace typed errors with custom errors

This commit is contained in:
2017-02-28 15:44:28 -07:00
parent 1e8340f214
commit 9cff4e4b04
2 changed files with 68 additions and 16 deletions

View File

@@ -1,19 +1,20 @@
import typedError from 'error/typed'; export function WorkerTimeoutError(message, props = {}) {
this.name = 'WorkerTimeoutError';
this.message = message;
this.timeout = props.timeout;
this.jobId = props.jobId;
const errors = {}; if ("captureStackTrace" in Error) Error.captureStackTrace(this, WorkerTimeoutError);
else this.stack = (new Error()).stack;
}
WorkerTimeoutError.prototype = Object.create(Error.prototype);
errors.WorkerTimeoutError = typedError({ export function UnspecifiedWorkerError(message, props = {}) {
type: 'WorkerTimeoutError', this.name = 'UnspecifiedWorkerError';
message: 'worker timed out, timeout={timeout}', this.message = message;
timeout: null, this.jobId = props.jobId;
jobId: null
});
errors.UnspecifiedWorkerError = typedError({ if ("captureStackTrace" in Error) Error.captureStackTrace(this, UnspecifiedWorkerError);
type: 'UnspecifiedWorkerError', else this.stack = (new Error()).stack;
message: 'Unspecified worker error', }
timeout: null, UnspecifiedWorkerError.prototype = Object.create(Error.prototype);
jobId: null
});
export default errors;

View File

@@ -0,0 +1,51 @@
import expect from 'expect.js';
import { WorkerTimeoutError, UnspecifiedWorkerError } from '../../../lib/helpers/errors';
describe('custom errors', function () {
describe('WorkerTimeoutError', function () {
it('should be function', () => {
expect(WorkerTimeoutError).to.be.a('function');
});
it('should have a name', function () {
const err = new WorkerTimeoutError('timeout error');
expect(err).to.have.property('name', 'WorkerTimeoutError');
});
it('should take a jobId property', function () {
const err = new WorkerTimeoutError('timeout error', { jobId: 'il7hl34rqlo8ro' });
expect(err).to.have.property('jobId', 'il7hl34rqlo8ro');
});
it('should take a timeout property', function () {
const err = new WorkerTimeoutError('timeout error', { timeout: 15000 });
expect(err).to.have.property('timeout', 15000);
});
it('should be stringifyable', function () {
const err = new WorkerTimeoutError('timeout error');
expect(`${err}`).to.equal('WorkerTimeoutError: timeout error');
});
});
describe('UnspecifiedWorkerError', function () {
it('should be function', () => {
expect(UnspecifiedWorkerError).to.be.a('function');
});
it('should have a name', function () {
const err = new UnspecifiedWorkerError('unspecified error');
expect(err).to.have.property('name', 'UnspecifiedWorkerError');
});
it('should take a jobId property', function () {
const err = new UnspecifiedWorkerError('unspecified error', { jobId: 'il7hl34rqlo8ro' });
expect(err).to.have.property('jobId', 'il7hl34rqlo8ro');
});
it('should be stringifyable', function () {
const err = new UnspecifiedWorkerError('unspecified error');
expect(`${err}`).to.equal('UnspecifiedWorkerError: unspecified error');
});
});
});