From eb4940e2d29e98572c69e59aaf447f1dd68f7425 Mon Sep 17 00:00:00 2001 From: Weijia Wang <381152119@qq.com> Date: Tue, 8 Aug 2017 10:24:08 +0800 Subject: [PATCH] string_decoder: Migrate to use internal/errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/14682 Refs: https://github.com/nodejs/node/issues/11273 Reviewed-By: Refael Ackermann Reviewed-By: Michaƫl Zasso Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/string_decoder.js | 3 ++- test/parallel/test-string-decoder.js | 24 +++++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/string_decoder.js b/lib/string_decoder.js index eee9a7d9273ebf..f0bca5705098a8 100644 --- a/lib/string_decoder.js +++ b/lib/string_decoder.js @@ -23,6 +23,7 @@ const Buffer = require('buffer').Buffer; const internalUtil = require('internal/util'); +const errors = require('internal/errors'); const isEncoding = Buffer[internalUtil.kIsEncodingSymbol]; // Do not cache `Buffer.isEncoding` when checking encoding names as some @@ -31,7 +32,7 @@ function normalizeEncoding(enc) { const nenc = internalUtil.normalizeEncoding(enc); if (typeof nenc !== 'string' && (Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc))) - throw new Error(`Unknown encoding: ${enc}`); + throw new errors.TypeError('ERR_UNKNOWN_ENCODING', enc); return nenc || enc; } diff --git a/test/parallel/test-string-decoder.js b/test/parallel/test-string-decoder.js index 87ab5e8d37bd43..a13741180689b6 100644 --- a/test/parallel/test-string-decoder.js +++ b/test/parallel/test-string-decoder.js @@ -20,7 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const inspect = require('util').inspect; const StringDecoder = require('string_decoder').StringDecoder; @@ -124,13 +124,23 @@ assert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), ''); assert.strictEqual(decoder.write(Buffer.from('4D', 'hex')), ''); assert.strictEqual(decoder.end(), '\ud83d'); -assert.throws(() => { - new StringDecoder(1); -}, /^Error: Unknown encoding: 1$/); +common.expectsError( + () => new StringDecoder(1), + { + code: 'ERR_UNKNOWN_ENCODING', + type: TypeError, + message: 'Unknown encoding: 1' + } +); -assert.throws(() => { - new StringDecoder('test'); -}, /^Error: Unknown encoding: test$/); +common.expectsError( + () => new StringDecoder('test'), + { + code: 'ERR_UNKNOWN_ENCODING', + type: TypeError, + message: 'Unknown encoding: test' + } +); // test verifies that StringDecoder will correctly decode the given input // buffer with the given encoding to the expected output. It will attempt all