Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change serializer errors to use error codes #4464

Merged
merged 2 commits into from Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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