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

fix common.expectsError type checking #13686

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,11 @@ exports.expectsError = function expectsError(fn, settings, exact) {
}
assert(error instanceof type,
`${error.name} is not instance of ${type.name}`);
let typeName = error.constructor.name;
if (typeName === 'NodeError' && type.name !== 'NodeError') {
typeName = Object.getPrototypeOf(error.constructor).name;
}
assert.strictEqual(typeName, type.name);
}
if ('message' in settings) {
const message = settings.message;
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-crypto-sign-verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ common.expectsError(
}, ''),
{
code: 'ERR_INVALID_OPT_VALUE',
type: Error,
type: TypeError,
message: 'The value "undefined" is invalid for option "padding"'
});

Expand All @@ -47,7 +47,7 @@ common.expectsError(
}, ''),
{
code: 'ERR_INVALID_OPT_VALUE',
type: Error,
type: TypeError,
message: 'The value "undefined" is invalid for option "saltLength"'
});

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-timestamp-parsing-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const assert = require('assert');
},
{
code: 'ERR_INVALID_ARG_TYPE',
type: Error
type: TypeError
});
});

Expand All @@ -20,7 +20,7 @@ common.expectsError(
},
{
code: 'ERR_INVALID_ARG_TYPE',
type: Error
type: TypeError
});

const okInputs = [1, -1, '1', '-1', Date.now()];
Expand Down
7 changes: 5 additions & 2 deletions test/parallel/test-http2-client-http1-server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const http = require('http');
const http2 = require('http2');
const { NghttpError } = require('internal/http2/util');

// Creating an http1 server here...
const server = http.createServer(common.mustNotCall());
Expand All @@ -18,13 +20,14 @@ server.listen(0, common.mustCall(() => {

req.on('error', common.expectsError({
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
message: 'Protocol error'
}));

client.on('error', common.expectsError({
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
name: 'Error [ERR_HTTP2_ERROR]',
message: 'Protocol error'
}));

Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-http2-client-onconnect-errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
if (!common.hasCrypto)
Expand All @@ -10,6 +11,7 @@ const {
nghttp2ErrorString
} = process.binding('http2');
const http2 = require('http2');
const { NghttpError } = require('internal/http2/util');

// tests error handling within requestOnConnect
// - NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE (should emit session error)
Expand Down Expand Up @@ -51,7 +53,8 @@ const genericTests = Object.getOwnPropertyNames(constants)
ngError: constants[key],
error: {
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
name: 'Error [ERR_HTTP2_ERROR]',
message: nghttp2ErrorString(constants[key])
},
type: 'session'
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-http2-info-headers-errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
if (!common.hasCrypto)
Expand All @@ -9,6 +10,7 @@ const {
Http2Stream,
nghttp2ErrorString
} = process.binding('http2');
const { NghttpError } = require('internal/http2/util');

// tests error handling within additionalHeaders
// - every other NGHTTP2 error from binding (should emit stream error)
Expand All @@ -24,7 +26,8 @@ const genericTests = Object.getOwnPropertyNames(constants)
ngError: constants[key],
error: {
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
name: 'Error [ERR_HTTP2_ERROR]',
message: nghttp2ErrorString(constants[key])
},
type: 'stream'
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-http2-respond-errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
if (!common.hasCrypto)
Expand All @@ -9,6 +10,7 @@ const {
Http2Stream,
nghttp2ErrorString
} = process.binding('http2');
const { NghttpError } = require('internal/http2/util');

// tests error handling within respond
// - every other NGHTTP2 error from binding (should emit stream error)
Expand All @@ -25,7 +27,8 @@ const genericTests = Object.getOwnPropertyNames(constants)
ngError: constants[key],
error: {
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
name: 'Error [ERR_HTTP2_ERROR]',
message: nghttp2ErrorString(constants[key])
},
type: 'stream'
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-http2-respond-with-fd-errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');

Expand All @@ -14,6 +15,7 @@ const {
Http2Stream,
nghttp2ErrorString
} = process.binding('http2');
const { NghttpError } = require('internal/http2/util');

// tests error handling within processRespondWithFD
// (called by respondWithFD & respondWithFile)
Expand All @@ -32,7 +34,8 @@ const genericTests = Object.getOwnPropertyNames(constants)
ngError: constants[key],
error: {
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
name: 'Error [ERR_HTTP2_ERROR]',
message: nghttp2ErrorString(constants[key])
},
type: 'stream'
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http2-server-http1-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');

Expand All @@ -7,14 +8,15 @@ if (!common.hasCrypto)

const http = require('http');
const http2 = require('http2');
const { NghttpError } = require('internal/http2/util');

const server = http2.createServer();
server.on('stream', common.mustNotCall());
server.on('session', common.mustCall((session) => {
session.on('close', common.mustCall());
session.on('error', common.expectsError({
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
message: 'Received bad client magic byte string'
}));
}));
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-http2-server-push-stream-errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
if (!common.hasCrypto)
Expand All @@ -9,6 +10,7 @@ const {
Http2Stream,
nghttp2ErrorString
} = process.binding('http2');
const { NghttpError } = require('internal/http2/util');

// tests error handling within pushStream
// - NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE (should emit session error)
Expand Down Expand Up @@ -49,7 +51,8 @@ const genericTests = Object.getOwnPropertyNames(constants)
ngError: constants[key],
error: {
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
name: 'Error [ERR_HTTP2_ERROR]',
message: nghttp2ErrorString(constants[key])
},
type: 'stream'
Expand Down
9 changes: 8 additions & 1 deletion test/parallel/test-internal-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ assert.doesNotThrow(() => {
assert.doesNotThrow(() => {
common.expectsError(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a');
}, { code: 'TEST_ERROR_1', type: Error });
}, {
code: 'TEST_ERROR_1',
type: TypeError,
message: 'Error for testing purposes: a'
});
});

common.expectsError(() => {
Expand All @@ -183,6 +187,7 @@ common.expectsError(() => {
message: /^Error for testing 2/ });
}, {
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: /.+ does not match \S/
});

Expand Down Expand Up @@ -233,6 +238,7 @@ common.expectsError(
() => errors.message('ERR_INVALID_URL_SCHEME', [[]]),
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: /^At least one expected value needs to be specified$/
});

Expand All @@ -247,6 +253,7 @@ common.expectsError(
() => errors.message('ERR_MISSING_ARGS'),
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: /^At least one arg needs to be specified$/
});

Expand Down
7 changes: 5 additions & 2 deletions test/parallel/test-ttywrap-invalid-fd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
const fs = require('fs');
const tty = require('tty');
const { SystemError } = require('internal/errors');

common.expectsError(
() => new tty.WriteStream(-1),
Expand All @@ -27,7 +30,7 @@ common.expectsError(
new tty.WriteStream(fd);
}, {
code: 'ERR_SYSTEM_ERROR',
type: Error,
type: SystemError,
message
}
);
Expand All @@ -42,7 +45,7 @@ common.expectsError(
new tty.ReadStream(fd);
}, {
code: 'ERR_SYSTEM_ERROR',
type: Error,
type: SystemError,
message
});
}
Expand Down