Skip to content

Commit 1e8d110

Browse files
committedMar 5, 2018
lib: port errors to new system
This is a first batch of updates that touches non-underscored modules in lib. PR-URL: nodejs#19034 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 023f49c commit 1e8d110

31 files changed

+377
-418
lines changed
 

Diff for: ‎lib/assert.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ const {
2525
isDeepEqual,
2626
isDeepStrictEqual
2727
} = require('internal/util/comparisons');
28-
const { AssertionError, TypeError, errorCache } = require('internal/errors');
28+
const {
29+
AssertionError,
30+
errorCache,
31+
codes: {
32+
ERR_INVALID_ARG_TYPE
33+
}
34+
} = require('internal/errors');
2935
const { openSync, closeSync, readSync } = require('fs');
3036
const { parseExpressionAt } = require('internal/deps/acorn/dist/acorn');
3137
const { inspect } = require('util');
@@ -380,8 +386,9 @@ function expectedException(actual, expected, msg) {
380386
return expected.test(actual);
381387
// assert.doesNotThrow does not accept objects.
382388
if (arguments.length === 2) {
383-
throw new TypeError('ERR_INVALID_ARG_TYPE', 'expected',
384-
['Function', 'RegExp'], expected);
389+
throw new ERR_INVALID_ARG_TYPE(
390+
'expected', ['Function', 'RegExp'], expected
391+
);
385392
}
386393
// The name and message could be non enumerable. Therefore test them
387394
// explicitly.
@@ -408,8 +415,7 @@ function expectedException(actual, expected, msg) {
408415

409416
function getActual(block) {
410417
if (typeof block !== 'function') {
411-
throw new TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
412-
block);
418+
throw new ERR_INVALID_ARG_TYPE('block', 'Function', block);
413419
}
414420
try {
415421
block();
@@ -425,10 +431,7 @@ assert.throws = function throws(block, error, message) {
425431

426432
if (typeof error === 'string') {
427433
if (arguments.length === 3)
428-
throw new TypeError('ERR_INVALID_ARG_TYPE',
429-
'error',
430-
['Function', 'RegExp'],
431-
error);
434+
throw new ERR_INVALID_ARG_TYPE('error', ['Function', 'RegExp'], error);
432435

433436
message = error;
434437
error = null;

Diff for: ‎lib/async_hooks.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict';
22

3-
const errors = require('internal/errors');
3+
const {
4+
ERR_ASYNC_CALLBACK,
5+
ERR_INVALID_ARG_TYPE,
6+
ERR_INVALID_ASYNC_ID
7+
} = require('internal/errors').codes;
48
const async_wrap = process.binding('async_wrap');
59
const internal_async_hooks = require('internal/async_hooks');
610

@@ -41,15 +45,15 @@ const {
4145
class AsyncHook {
4246
constructor({ init, before, after, destroy, promiseResolve }) {
4347
if (init !== undefined && typeof init !== 'function')
44-
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.init');
48+
throw new ERR_ASYNC_CALLBACK('hook.init');
4549
if (before !== undefined && typeof before !== 'function')
46-
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.before');
50+
throw new ERR_ASYNC_CALLBACK('hook.before');
4751
if (after !== undefined && typeof after !== 'function')
48-
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.after');
52+
throw new ERR_ASYNC_CALLBACK('hook.after');
4953
if (destroy !== undefined && typeof destroy !== 'function')
50-
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.destroy');
54+
throw new ERR_ASYNC_CALLBACK('hook.destroy');
5155
if (promiseResolve !== undefined && typeof promiseResolve !== 'function')
52-
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.promiseResolve');
56+
throw new ERR_ASYNC_CALLBACK('hook.promiseResolve');
5357

5458
this[init_symbol] = init;
5559
this[before_symbol] = before;
@@ -140,7 +144,7 @@ function showEmitBeforeAfterWarning() {
140144
class AsyncResource {
141145
constructor(type, opts = {}) {
142146
if (typeof type !== 'string')
143-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'type', 'string');
147+
throw new ERR_INVALID_ARG_TYPE('type', 'string');
144148

145149
if (typeof opts === 'number') {
146150
opts = { triggerAsyncId: opts, requireManualDestroy: false };
@@ -152,9 +156,7 @@ class AsyncResource {
152156
// triggerAsyncId.
153157
const triggerAsyncId = opts.triggerAsyncId;
154158
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
155-
throw new errors.RangeError('ERR_INVALID_ASYNC_ID',
156-
'triggerAsyncId',
157-
triggerAsyncId);
159+
throw new ERR_INVALID_ASYNC_ID('triggerAsyncId', triggerAsyncId);
158160
}
159161

160162
this[async_id_symbol] = newAsyncId();

0 commit comments

Comments
 (0)
Please sign in to comment.