From f8b89664f181dab66849bcab2f3abc93e4db9ad3 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 11 Dec 2017 03:56:41 -0200 Subject: [PATCH 1/3] test: fix wrong error classes passed in as type --- test/parallel/test-crypto-sign-verify.js | 4 ++-- test/parallel/test-fs-timestamp-parsing-error.js | 4 ++-- test/parallel/test-http2-client-http1-server.js | 7 +++++-- test/parallel/test-http2-client-onconnect-errors.js | 5 ++++- test/parallel/test-http2-info-headers-errors.js | 5 ++++- test/parallel/test-http2-respond-errors.js | 5 ++++- test/parallel/test-http2-respond-with-fd-errors.js | 5 ++++- test/parallel/test-http2-server-http1-client.js | 4 +++- test/parallel/test-http2-server-push-stream-errors.js | 5 ++++- test/parallel/test-internal-errors.js | 6 +++++- test/parallel/test-ttywrap-invalid-fd.js | 7 +++++-- 11 files changed, 42 insertions(+), 15 deletions(-) diff --git a/test/parallel/test-crypto-sign-verify.js b/test/parallel/test-crypto-sign-verify.js index abdbcd3a1e5350..48b21d0d85ac82 100644 --- a/test/parallel/test-crypto-sign-verify.js +++ b/test/parallel/test-crypto-sign-verify.js @@ -36,7 +36,7 @@ common.expectsError( }, ''), { code: 'ERR_INVALID_OPT_VALUE', - type: Error, + type: TypeError, message: 'The value "undefined" is invalid for option "padding"' }); @@ -47,7 +47,7 @@ common.expectsError( }, ''), { code: 'ERR_INVALID_OPT_VALUE', - type: Error, + type: TypeError, message: 'The value "undefined" is invalid for option "saltLength"' }); diff --git a/test/parallel/test-fs-timestamp-parsing-error.js b/test/parallel/test-fs-timestamp-parsing-error.js index 3680b5fece13c9..a0dac1bf6d3671 100644 --- a/test/parallel/test-fs-timestamp-parsing-error.js +++ b/test/parallel/test-fs-timestamp-parsing-error.js @@ -10,7 +10,7 @@ const assert = require('assert'); }, { code: 'ERR_INVALID_ARG_TYPE', - type: Error + type: TypeError }); }); @@ -20,7 +20,7 @@ common.expectsError( }, { code: 'ERR_INVALID_ARG_TYPE', - type: Error + type: TypeError }); const okInputs = [1, -1, '1', '-1', Date.now()]; diff --git a/test/parallel/test-http2-client-http1-server.js b/test/parallel/test-http2-client-http1-server.js index 616427b3904e16..c7535adcef2381 100644 --- a/test/parallel/test-http2-client-http1-server.js +++ b/test/parallel/test-http2-client-http1-server.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --expose-internals const common = require('../common'); if (!common.hasCrypto) @@ -6,6 +7,7 @@ if (!common.hasCrypto) 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()); @@ -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' })); diff --git a/test/parallel/test-http2-client-onconnect-errors.js b/test/parallel/test-http2-client-onconnect-errors.js index 44fe6875602187..901caf18d9177f 100644 --- a/test/parallel/test-http2-client-onconnect-errors.js +++ b/test/parallel/test-http2-client-onconnect-errors.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --expose-internals const common = require('../common'); if (!common.hasCrypto) @@ -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) @@ -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' diff --git a/test/parallel/test-http2-info-headers-errors.js b/test/parallel/test-http2-info-headers-errors.js index 1df76334558529..230a1328ed1550 100644 --- a/test/parallel/test-http2-info-headers-errors.js +++ b/test/parallel/test-http2-info-headers-errors.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --expose-internals const common = require('../common'); if (!common.hasCrypto) @@ -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) @@ -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' diff --git a/test/parallel/test-http2-respond-errors.js b/test/parallel/test-http2-respond-errors.js index 2a48456c9394a4..5ec953c5442360 100644 --- a/test/parallel/test-http2-respond-errors.js +++ b/test/parallel/test-http2-respond-errors.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --expose-internals const common = require('../common'); if (!common.hasCrypto) @@ -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) @@ -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' diff --git a/test/parallel/test-http2-respond-with-fd-errors.js b/test/parallel/test-http2-respond-with-fd-errors.js index 0b215134663bda..99a5273df3c0d9 100644 --- a/test/parallel/test-http2-respond-with-fd-errors.js +++ b/test/parallel/test-http2-respond-with-fd-errors.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --expose-internals const common = require('../common'); @@ -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) @@ -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' diff --git a/test/parallel/test-http2-server-http1-client.js b/test/parallel/test-http2-server-http1-client.js index 34a8f48b5e130d..394993d4d72088 100644 --- a/test/parallel/test-http2-server-http1-client.js +++ b/test/parallel/test-http2-server-http1-client.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --expose-internals const common = require('../common'); @@ -7,6 +8,7 @@ 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()); @@ -14,7 +16,7 @@ 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' })); })); diff --git a/test/parallel/test-http2-server-push-stream-errors.js b/test/parallel/test-http2-server-push-stream-errors.js index 7eaf4dc94d15e2..a6d2fe127827a8 100644 --- a/test/parallel/test-http2-server-push-stream-errors.js +++ b/test/parallel/test-http2-server-push-stream-errors.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --expose-internals const common = require('../common'); if (!common.hasCrypto) @@ -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) @@ -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' diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index 3673749224e248..e2d297eaf78fb9 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -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(() => { diff --git a/test/parallel/test-ttywrap-invalid-fd.js b/test/parallel/test-ttywrap-invalid-fd.js index ad7a98da3460dd..6564d47fd938a6 100644 --- a/test/parallel/test-ttywrap-invalid-fd.js +++ b/test/parallel/test-ttywrap-invalid-fd.js @@ -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), @@ -27,7 +30,7 @@ common.expectsError( new tty.WriteStream(fd); }, { code: 'ERR_SYSTEM_ERROR', - type: Error, + type: SystemError, message } ); @@ -42,7 +45,7 @@ common.expectsError( new tty.ReadStream(fd); }, { code: 'ERR_SYSTEM_ERROR', - type: Error, + type: SystemError, message }); } From adebaeb0ee2bad5bc4f9d5948b571fa689ea57bf Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Wed, 14 Jun 2017 15:47:19 -0400 Subject: [PATCH 2/3] test: fix common.expectsError The function should strictly test for the error class and only accept the correct one. Any other error class should fail. --- test/common/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/common/index.js b/test/common/index.js index 7a2cebac5f182d..e6236b1be69371 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -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; From c0606e0626222c7d2269c5599c410f57529fb711 Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Mon, 19 Jun 2017 15:24:04 -0400 Subject: [PATCH 3/3] test: add more asserts to `test-internal-errors` --- test/parallel/test-internal-errors.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index e2d297eaf78fb9..e8904bba0ec014 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -187,6 +187,7 @@ common.expectsError(() => { message: /^Error for testing 2/ }); }, { code: 'ERR_ASSERTION', + type: assert.AssertionError, message: /.+ does not match \S/ }); @@ -237,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$/ }); @@ -251,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$/ });