From 4c2f041f023ac355a4ea8668ea23f26ccf450fd3 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 4 Feb 2017 11:21:37 -0800 Subject: [PATCH 1/2] test: throw Error objects instead of literals test-tls-econnreset and test-http-response-status-message throw literals instead of Error objects. Use common.fail() which throws an AssertionError. --- test/parallel/test-http-response-status-message.js | 4 ++-- test/parallel/test-tls-econnreset.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-http-response-status-message.js b/test/parallel/test-http-response-status-message.js index 95859634ac394a..14fcb43027a599 100644 --- a/test/parallel/test-http-response-status-message.js +++ b/test/parallel/test-http-response-status-message.js @@ -1,5 +1,5 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const http = require('http'); const net = require('net'); @@ -23,7 +23,7 @@ testCases.findByPath = function(path) { return testCase.path === path; }); if (matching.length === 0) { - throw 'failed to find test case with path ' + path; + common.fail(`failed to find test case with path ${path}`); } return matching[0]; }; diff --git a/test/parallel/test-tls-econnreset.js b/test/parallel/test-tls-econnreset.js index c0df9948f489ea..6bf84a2d0ea726 100644 --- a/test/parallel/test-tls-econnreset.js +++ b/test/parallel/test-tls-econnreset.js @@ -49,8 +49,8 @@ const ca = [ cert, cacert ]; let clientError = null; let connectError = null; -const server = tls.createServer({ ca: ca, cert: cert, key: key }, (conn) => { - throw 'unreachable'; +const server = tls.createServer({ ca: ca, cert: cert, key: key }, () => { + common.fail('should be unreachable'); }).on('tlsClientError', function(err, conn) { assert(!clientError && conn); clientError = err; From bbd1af3549d7d4282458cf7bed789b97e499c8db Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 4 Feb 2017 11:26:44 -0800 Subject: [PATCH 2/2] tools: enable no-throw-literal ESLint rule Only throw the Error object itself or an object using the Error object as base objects for user-defined exceptions. --- .eslintrc.yaml | 1 + test/message/throw_custom_error.js | 1 + test/message/throw_custom_error.out | 2 +- test/message/throw_in_line_with_tabs.js | 1 + test/message/throw_in_line_with_tabs.out | 2 +- test/message/throw_non_error.js | 1 + test/message/throw_non_error.out | 2 +- test/message/throw_null.js | 1 + test/message/throw_null.out | 2 +- test/message/throw_undefined.js | 1 + test/message/throw_undefined.out | 2 +- test/parallel/test-assert.js | 3 ++- 12 files changed, 13 insertions(+), 6 deletions(-) diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 4f691caefd3a78..81c93e533b9f8b 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -40,6 +40,7 @@ rules: no-octal: 2 no-redeclare: 2 no-self-assign: 2 + no-throw-literal: 2 no-unused-labels: 2 no-useless-call: 2 no-useless-escape: 2 diff --git a/test/message/throw_custom_error.js b/test/message/throw_custom_error.js index 8866ca8514de7d..97df4fd390ded8 100644 --- a/test/message/throw_custom_error.js +++ b/test/message/throw_custom_error.js @@ -2,4 +2,5 @@ require('../common'); // custom error throwing +// eslint-disable-next-line no-throw-literal throw ({ name: 'MyCustomError', message: 'This is a custom message' }); diff --git a/test/message/throw_custom_error.out b/test/message/throw_custom_error.out index 401581f31d5caf..ef73c52c889dff 100644 --- a/test/message/throw_custom_error.out +++ b/test/message/throw_custom_error.out @@ -1,4 +1,4 @@ -*test*message*throw_custom_error.js:5 +*test*message*throw_custom_error.js:6 throw ({ name: 'MyCustomError', message: 'This is a custom message' }); ^ MyCustomError: This is a custom message diff --git a/test/message/throw_in_line_with_tabs.js b/test/message/throw_in_line_with_tabs.js index ad95d66e97ec55..f02e5479d6fe7d 100644 --- a/test/message/throw_in_line_with_tabs.js +++ b/test/message/throw_in_line_with_tabs.js @@ -6,6 +6,7 @@ console.error('before'); (function() { // these lines should contain tab! + // eslint-disable-next-line no-throw-literal throw ({ foo: 'bar' }); })(); diff --git a/test/message/throw_in_line_with_tabs.out b/test/message/throw_in_line_with_tabs.out index d245cca4941ab5..e83b05768433b8 100644 --- a/test/message/throw_in_line_with_tabs.out +++ b/test/message/throw_in_line_with_tabs.out @@ -1,5 +1,5 @@ before -*test*message*throw_in_line_with_tabs.js:9 +*test*message*throw_in_line_with_tabs.js:10 throw ({ foo: 'bar' }); ^ [object Object] diff --git a/test/message/throw_non_error.js b/test/message/throw_non_error.js index d4dae642af882b..7a23908eece894 100644 --- a/test/message/throw_non_error.js +++ b/test/message/throw_non_error.js @@ -2,4 +2,5 @@ require('../common'); // custom error throwing +// eslint-disable-next-line no-throw-literal throw ({ foo: 'bar' }); diff --git a/test/message/throw_non_error.out b/test/message/throw_non_error.out index b98edc46ae2a6e..15f95fcc11699a 100644 --- a/test/message/throw_non_error.out +++ b/test/message/throw_non_error.out @@ -1,4 +1,4 @@ -*test*message*throw_non_error.js:5 +*test*message*throw_non_error.js:6 throw ({ foo: 'bar' }); ^ [object Object] diff --git a/test/message/throw_null.js b/test/message/throw_null.js index 9b17aa68f186e4..2c5a8f47ccaf18 100644 --- a/test/message/throw_null.js +++ b/test/message/throw_null.js @@ -1,4 +1,5 @@ 'use strict'; require('../common'); +// eslint-disable-next-line no-throw-literal throw null; diff --git a/test/message/throw_null.out b/test/message/throw_null.out index 5d2c677240d1a7..eb3eeb1294e727 100644 --- a/test/message/throw_null.out +++ b/test/message/throw_null.out @@ -1,5 +1,5 @@ -*test*message*throw_null.js:4 +*test*message*throw_null.js:5 throw null; ^ null diff --git a/test/message/throw_undefined.js b/test/message/throw_undefined.js index 18c27dbac87799..7d0fef0dbdc3a6 100644 --- a/test/message/throw_undefined.js +++ b/test/message/throw_undefined.js @@ -1,4 +1,5 @@ 'use strict'; require('../common'); +// eslint-disable-next-line no-throw-literal throw undefined; diff --git a/test/message/throw_undefined.out b/test/message/throw_undefined.out index 32a71f0486e47e..c23dac051fa5f1 100644 --- a/test/message/throw_undefined.out +++ b/test/message/throw_undefined.out @@ -1,5 +1,5 @@ -*test*message*throw_undefined.js:4 +*test*message*throw_undefined.js:5 throw undefined; ^ undefined diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index f9e82c758a6509..ba3f8ad352aeb5 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -394,7 +394,7 @@ threw = false; try { assert.throws( function() { - throw ({}); + throw ({}); // eslint-disable-line no-throw-literal }, Array ); @@ -576,6 +576,7 @@ testBlockTypeError(assert.throws, undefined); testBlockTypeError(assert.doesNotThrow, undefined); // https://github.com/nodejs/node/issues/3275 +// eslint-disable-next-line no-throw-literal assert.throws(() => { throw 'error'; }, (err) => err === 'error'); assert.throws(() => { throw new Error(); }, (err) => err instanceof Error);