string_decoder: Migrate to use internal/errors
PR-URL: https://github.com/nodejs/node/pull/14682 Refs: https://github.com/nodejs/node/issues/11273 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
468110b327
commit
eb4940e2d2
@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
const Buffer = require('buffer').Buffer;
|
const Buffer = require('buffer').Buffer;
|
||||||
const internalUtil = require('internal/util');
|
const internalUtil = require('internal/util');
|
||||||
|
const errors = require('internal/errors');
|
||||||
const isEncoding = Buffer[internalUtil.kIsEncodingSymbol];
|
const isEncoding = Buffer[internalUtil.kIsEncodingSymbol];
|
||||||
|
|
||||||
// Do not cache `Buffer.isEncoding` when checking encoding names as some
|
// Do not cache `Buffer.isEncoding` when checking encoding names as some
|
||||||
@ -31,7 +32,7 @@ function normalizeEncoding(enc) {
|
|||||||
const nenc = internalUtil.normalizeEncoding(enc);
|
const nenc = internalUtil.normalizeEncoding(enc);
|
||||||
if (typeof nenc !== 'string' &&
|
if (typeof nenc !== 'string' &&
|
||||||
(Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc)))
|
(Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc)))
|
||||||
throw new Error(`Unknown encoding: ${enc}`);
|
throw new errors.TypeError('ERR_UNKNOWN_ENCODING', enc);
|
||||||
return nenc || enc;
|
return nenc || enc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const inspect = require('util').inspect;
|
const inspect = require('util').inspect;
|
||||||
const StringDecoder = require('string_decoder').StringDecoder;
|
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.write(Buffer.from('4D', 'hex')), '');
|
||||||
assert.strictEqual(decoder.end(), '\ud83d');
|
assert.strictEqual(decoder.end(), '\ud83d');
|
||||||
|
|
||||||
assert.throws(() => {
|
common.expectsError(
|
||||||
new StringDecoder(1);
|
() => new StringDecoder(1),
|
||||||
}, /^Error: Unknown encoding: 1$/);
|
{
|
||||||
|
code: 'ERR_UNKNOWN_ENCODING',
|
||||||
|
type: TypeError,
|
||||||
|
message: 'Unknown encoding: 1'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
assert.throws(() => {
|
common.expectsError(
|
||||||
new StringDecoder('test');
|
() => new StringDecoder('test'),
|
||||||
}, /^Error: Unknown encoding: test$/);
|
{
|
||||||
|
code: 'ERR_UNKNOWN_ENCODING',
|
||||||
|
type: TypeError,
|
||||||
|
message: 'Unknown encoding: test'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// test verifies that StringDecoder will correctly decode the given input
|
// test verifies that StringDecoder will correctly decode the given input
|
||||||
// buffer with the given encoding to the expected output. It will attempt all
|
// buffer with the given encoding to the expected output. It will attempt all
|
||||||
|
Loading…
x
Reference in New Issue
Block a user