Skip to content

Commit

Permalink
Change serializer errors to use error codes (#4464)
Browse files Browse the repository at this point in the history
* Change serializer errors to use error codes
  • Loading branch information
evaline-ju committed Oct 9, 2020
1 parent 6ceca82 commit fd9fe95
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
12 changes: 9 additions & 3 deletions lib/nodejs/serializer.js
Expand Up @@ -131,7 +131,11 @@ class SerializableEvent {
*/
constructor(eventName, originalValue, originalError) {
if (!eventName) {
throw new Error('expected a non-empty `eventName` string argument');
throw createInvalidArgumentTypeError(
'Empty `eventName` string argument',
'eventName',
'string'
);
}
/**
* The event name.
Expand All @@ -140,8 +144,10 @@ class SerializableEvent {
this.eventName = eventName;
const originalValueType = type(originalValue);
if (originalValueType !== 'object' && originalValueType !== 'undefined') {
throw new Error(
`expected object, received [${originalValueType}]: ${originalValue}`
throw createInvalidArgumentTypeError(
`Expected object but received ${originalValueType}`,
'originalValue',
'object'
);
}
/**
Expand Down
20 changes: 8 additions & 12 deletions test/node-unit/serializer.spec.js
Expand Up @@ -88,22 +88,18 @@ describe('serializer', function() {
describe('SerializableEvent', function() {
describe('constructor', function() {
describe('when called without `eventName`', function() {
it('should throw', function() {
expect(
() => new SerializableEvent(),
'to throw',
/expected a non-empty `eventName`/
);
it('should throw "invalid arg value" error', function() {
expect(() => new SerializableEvent(), 'to throw', {
code: 'ERR_MOCHA_INVALID_ARG_TYPE'
});
});
});

describe('when called with a non-object `rawObject`', function() {
it('should throw', function() {
expect(
() => new SerializableEvent('blub', 'glug'),
'to throw',
/expected object, received \[string\]/
);
it('should throw "invalid arg type" error', function() {
expect(() => new SerializableEvent('blub', 'glug'), 'to throw', {
code: 'ERR_MOCHA_INVALID_ARG_TYPE'
});
});
});
});
Expand Down

0 comments on commit fd9fe95

Please sign in to comment.