Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string_decoder: Migrate to use internal/errors #14682

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/string_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
24 changes: 17 additions & 7 deletions test/parallel/test-string-decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down