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 1 commit
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
18 changes: 14 additions & 4 deletions lib/nodejs/serializer.js
Expand Up @@ -7,7 +7,10 @@
'use strict';

const {type} = require('../utils');
const {createInvalidArgumentTypeError} = require('../errors');
const {
createInvalidArgumentTypeError,
createInvalidArgumentValueError
} = require('../errors');
// this is not named `mocha:parallel:serializer` because it's noisy and it's
// helpful to be able to write `DEBUG=mocha:parallel*` and get everything else.
const debug = require('debug')('mocha:serializer');
Expand Down Expand Up @@ -131,7 +134,12 @@ class SerializableEvent {
*/
constructor(eventName, originalValue, originalError) {
if (!eventName) {
throw new Error('expected a non-empty `eventName` string argument');
throw createInvalidArgumentValueError(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if eventName is undefined (e.g., someone wrote new SerializableEvent()) then one could argue this is an invalid type (it should be a string; not undefined) instead of an invalid value. contrast that with passing '' for eventName--the type is correct but the value is invalid. in this case, we're checking for falsy, which is both a type and a value (because JavaScript)--and is intended--so I don't feel strongly that we need two different errors here. just wanted to mention it

further, these two types of errors are unlikely to be caused by an end-user--it's an error only Mocha contributors should ever encounter. we don't usually do this sort of exhaustive type-checking unless it's user input

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. I wasn't tied to the two different error types, so I've gone ahead and changed this one to throw a type error too.

'Empty `eventName` string argument',
'eventName',
eventName,
'is empty'
);
}
/**
* The event name.
Expand All @@ -140,8 +148,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_VALUE'
});
});
});

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