Skip to content

Commit eb4940e

Browse files
starkwangBridgeAR
authored andcommitted
string_decoder: Migrate to use internal/errors
PR-URL: #14682 Refs: #11273 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 468110b commit eb4940e

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

lib/string_decoder.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const Buffer = require('buffer').Buffer;
2525
const internalUtil = require('internal/util');
26+
const errors = require('internal/errors');
2627
const isEncoding = Buffer[internalUtil.kIsEncodingSymbol];
2728

2829
// Do not cache `Buffer.isEncoding` when checking encoding names as some
@@ -31,7 +32,7 @@ function normalizeEncoding(enc) {
3132
const nenc = internalUtil.normalizeEncoding(enc);
3233
if (typeof nenc !== 'string' &&
3334
(Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc)))
34-
throw new Error(`Unknown encoding: ${enc}`);
35+
throw new errors.TypeError('ERR_UNKNOWN_ENCODING', enc);
3536
return nenc || enc;
3637
}
3738

test/parallel/test-string-decoder.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525
const inspect = require('util').inspect;
2626
const StringDecoder = require('string_decoder').StringDecoder;
@@ -124,13 +124,23 @@ assert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), '');
124124
assert.strictEqual(decoder.write(Buffer.from('4D', 'hex')), '');
125125
assert.strictEqual(decoder.end(), '\ud83d');
126126

127-
assert.throws(() => {
128-
new StringDecoder(1);
129-
}, /^Error: Unknown encoding: 1$/);
127+
common.expectsError(
128+
() => new StringDecoder(1),
129+
{
130+
code: 'ERR_UNKNOWN_ENCODING',
131+
type: TypeError,
132+
message: 'Unknown encoding: 1'
133+
}
134+
);
130135

131-
assert.throws(() => {
132-
new StringDecoder('test');
133-
}, /^Error: Unknown encoding: test$/);
136+
common.expectsError(
137+
() => new StringDecoder('test'),
138+
{
139+
code: 'ERR_UNKNOWN_ENCODING',
140+
type: TypeError,
141+
message: 'Unknown encoding: test'
142+
}
143+
);
134144

135145
// test verifies that StringDecoder will correctly decode the given input
136146
// buffer with the given encoding to the expected output. It will attempt all

0 commit comments

Comments
 (0)