Skip to content

Commit

Permalink
lib: port errors to new system
Browse files Browse the repository at this point in the history
This is a first batch of updates that touches non-underscored modules in
lib.

PR-URL: #19034
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
targos committed Mar 5, 2018
1 parent 023f49c commit 1e8d110
Show file tree
Hide file tree
Showing 31 changed files with 377 additions and 418 deletions.
21 changes: 12 additions & 9 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ const {
isDeepEqual,
isDeepStrictEqual
} = require('internal/util/comparisons');
const { AssertionError, TypeError, errorCache } = require('internal/errors');
const {
AssertionError,
errorCache,
codes: {
ERR_INVALID_ARG_TYPE
}
} = require('internal/errors');
const { openSync, closeSync, readSync } = require('fs');
const { parseExpressionAt } = require('internal/deps/acorn/dist/acorn');
const { inspect } = require('util');
Expand Down Expand Up @@ -380,8 +386,9 @@ function expectedException(actual, expected, msg) {
return expected.test(actual);
// assert.doesNotThrow does not accept objects.
if (arguments.length === 2) {
throw new TypeError('ERR_INVALID_ARG_TYPE', 'expected',
['Function', 'RegExp'], expected);
throw new ERR_INVALID_ARG_TYPE(
'expected', ['Function', 'RegExp'], expected
);
}
// The name and message could be non enumerable. Therefore test them
// explicitly.
Expand All @@ -408,8 +415,7 @@ function expectedException(actual, expected, msg) {

function getActual(block) {
if (typeof block !== 'function') {
throw new TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
block);
throw new ERR_INVALID_ARG_TYPE('block', 'Function', block);
}
try {
block();
Expand All @@ -425,10 +431,7 @@ assert.throws = function throws(block, error, message) {

if (typeof error === 'string') {
if (arguments.length === 3)
throw new TypeError('ERR_INVALID_ARG_TYPE',
'error',
['Function', 'RegExp'],
error);
throw new ERR_INVALID_ARG_TYPE('error', ['Function', 'RegExp'], error);

message = error;
error = null;
Expand Down
22 changes: 12 additions & 10 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict';

const errors = require('internal/errors');
const {
ERR_ASYNC_CALLBACK,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ASYNC_ID
} = require('internal/errors').codes;
const async_wrap = process.binding('async_wrap');
const internal_async_hooks = require('internal/async_hooks');

Expand Down Expand Up @@ -41,15 +45,15 @@ const {
class AsyncHook {
constructor({ init, before, after, destroy, promiseResolve }) {
if (init !== undefined && typeof init !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.init');
throw new ERR_ASYNC_CALLBACK('hook.init');
if (before !== undefined && typeof before !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.before');
throw new ERR_ASYNC_CALLBACK('hook.before');
if (after !== undefined && typeof after !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.after');
throw new ERR_ASYNC_CALLBACK('hook.after');
if (destroy !== undefined && typeof destroy !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.destroy');
throw new ERR_ASYNC_CALLBACK('hook.destroy');
if (promiseResolve !== undefined && typeof promiseResolve !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.promiseResolve');
throw new ERR_ASYNC_CALLBACK('hook.promiseResolve');

this[init_symbol] = init;
this[before_symbol] = before;
Expand Down Expand Up @@ -140,7 +144,7 @@ function showEmitBeforeAfterWarning() {
class AsyncResource {
constructor(type, opts = {}) {
if (typeof type !== 'string')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'type', 'string');
throw new ERR_INVALID_ARG_TYPE('type', 'string');

if (typeof opts === 'number') {
opts = { triggerAsyncId: opts, requireManualDestroy: false };
Expand All @@ -152,9 +156,7 @@ class AsyncResource {
// triggerAsyncId.
const triggerAsyncId = opts.triggerAsyncId;
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
throw new errors.RangeError('ERR_INVALID_ASYNC_ID',
'triggerAsyncId',
triggerAsyncId);
throw new ERR_INVALID_ASYNC_ID('triggerAsyncId', triggerAsyncId);
}

this[async_id_symbol] = newAsyncId();
Expand Down
Loading

0 comments on commit 1e8d110

Please sign in to comment.