Skip to content

Commit fd9fe95

Browse files
authored
Change serializer errors to use error codes (#4464)
* Change serializer errors to use error codes
1 parent 6ceca82 commit fd9fe95

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

lib/nodejs/serializer.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ class SerializableEvent {
131131
*/
132132
constructor(eventName, originalValue, originalError) {
133133
if (!eventName) {
134-
throw new Error('expected a non-empty `eventName` string argument');
134+
throw createInvalidArgumentTypeError(
135+
'Empty `eventName` string argument',
136+
'eventName',
137+
'string'
138+
);
135139
}
136140
/**
137141
* The event name.
@@ -140,8 +144,10 @@ class SerializableEvent {
140144
this.eventName = eventName;
141145
const originalValueType = type(originalValue);
142146
if (originalValueType !== 'object' && originalValueType !== 'undefined') {
143-
throw new Error(
144-
`expected object, received [${originalValueType}]: ${originalValue}`
147+
throw createInvalidArgumentTypeError(
148+
`Expected object but received ${originalValueType}`,
149+
'originalValue',
150+
'object'
145151
);
146152
}
147153
/**

test/node-unit/serializer.spec.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,18 @@ describe('serializer', function() {
8888
describe('SerializableEvent', function() {
8989
describe('constructor', function() {
9090
describe('when called without `eventName`', function() {
91-
it('should throw', function() {
92-
expect(
93-
() => new SerializableEvent(),
94-
'to throw',
95-
/expected a non-empty `eventName`/
96-
);
91+
it('should throw "invalid arg value" error', function() {
92+
expect(() => new SerializableEvent(), 'to throw', {
93+
code: 'ERR_MOCHA_INVALID_ARG_TYPE'
94+
});
9795
});
9896
});
9997

10098
describe('when called with a non-object `rawObject`', function() {
101-
it('should throw', function() {
102-
expect(
103-
() => new SerializableEvent('blub', 'glug'),
104-
'to throw',
105-
/expected object, received \[string\]/
106-
);
99+
it('should throw "invalid arg type" error', function() {
100+
expect(() => new SerializableEvent('blub', 'glug'), 'to throw', {
101+
code: 'ERR_MOCHA_INVALID_ARG_TYPE'
102+
});
107103
});
108104
});
109105
});

0 commit comments

Comments
 (0)