From de4a749788aa88b6f23f50eccb7a6752f6bb59b7 Mon Sep 17 00:00:00 2001 From: Sebastian Van Sande Date: Fri, 10 Feb 2017 21:23:52 +0100 Subject: [PATCH] internal/util: use internal/errors.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/11301 Refs: https://github.com/nodejs/node/issues/11273 Reviewed-By: James M Snell Reviewed-By: Franziska Hinkelmann Reviewed-By: Rich Trott Reviewed-By: Tobias Nießen Reviewed-By: Yuta Hiroto Reviewed-By: Joyee Cheung --- doc/api/errors.md | 6 ++++++ lib/internal/errors.js | 1 + lib/internal/util.js | 4 ++-- test/parallel/test-internal-util-assertCrypto.js | 12 +++++++----- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 9138da3115b0d8..aef6580822859e 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -748,6 +748,12 @@ Used when data cannot be sent on a socket. Used when a call is made and the UDP subsystem is not running. + +### ERR_NO_CRYPTO + +Used when an attempt is made to use crypto features while Node.js is not +compiled with OpenSSL crypto support. + ### ERR_STDERR_CLOSE diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 570ed6d5ba6b42..fcfbdd24bf3925 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -155,6 +155,7 @@ E('ERR_IPC_SYNC_FORK', 'IPC cannot be used with synchronous forks'); E('ERR_MISSING_ARGS', missingArgs); E('ERR_PARSE_HISTORY_DATA', (oldHistoryPath) => `Could not parse history data in ${oldHistoryPath}`); +E('ERR_NO_CRYPTO', 'Node.js is not compiled with OpenSSL crypto support'); E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed'); E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed'); E('ERR_TRANSFORM_ALREADY_TRANSFORMING', diff --git a/lib/internal/util.js b/lib/internal/util.js index a77089b6833cd1..efdd7e0fd277b3 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -34,7 +34,7 @@ function deprecate(fn, msg, code) { } if (code !== undefined && typeof code !== 'string') - throw new TypeError('`code` argument must be a string'); + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'string'); var warned = false; function deprecated(...args) { @@ -79,7 +79,7 @@ function decorateErrorStack(err) { function assertCrypto() { if (noCrypto) - throw new Error('Node.js is not compiled with openssl crypto support'); + throw new errors.Error('ERR_NO_CRYPTO'); } // The loop should only run at most twice, retrying with lowercased enc diff --git a/test/parallel/test-internal-util-assertCrypto.js b/test/parallel/test-internal-util-assertCrypto.js index 9d1c923255a0e8..d1387e489e1dfc 100644 --- a/test/parallel/test-internal-util-assertCrypto.js +++ b/test/parallel/test-internal-util-assertCrypto.js @@ -1,14 +1,16 @@ // Flags: --expose-internals 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const util = require('internal/util'); +const expectedError = common.expectsError({ + code: 'ERR_NO_CRYPTO', + type: Error +}); + if (!process.versions.openssl) { - assert.throws( - () => util.assertCrypto(), - /^Error: Node\.js is not compiled with openssl crypto support$/ - ); + assert.throws(() => util.assertCrypto(), expectedError); } else { assert.doesNotThrow(() => util.assertCrypto()); }