lib: port errors to new system

This is a first batch of updates that touches non-underscored modules in
lib.

PR-URL: https://github.com/nodejs/node/pull/19034
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
Michaël Zasso 2018-02-27 14:55:32 +01:00
parent 023f49c5a9
commit 1e8d110e64
31 changed files with 377 additions and 418 deletions

View file

@ -1,12 +1,19 @@
'use strict';
const EventEmitter = require('events');
const errors = require('internal/errors');
const {
ERR_INSPECTOR_ALREADY_CONNECTED,
ERR_INSPECTOR_CLOSED,
ERR_INSPECTOR_NOT_AVAILABLE,
ERR_INSPECTOR_NOT_CONNECTED,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK
} = require('internal/errors').codes;
const util = require('util');
const { Connection, open, url } = process.binding('inspector');
if (!Connection)
throw new errors.Error('ERR_INSPECTOR_NOT_AVAILABLE');
throw new ERR_INSPECTOR_NOT_AVAILABLE();
const connectionSymbol = Symbol('connectionProperty');
const messageCallbacksSymbol = Symbol('messageCallbacks');
@ -23,7 +30,7 @@ class Session extends EventEmitter {
connect() {
if (this[connectionSymbol])
throw new errors.Error('ERR_INSPECTOR_ALREADY_CONNECTED');
throw new ERR_INSPECTOR_ALREADY_CONNECTED();
this[connectionSymbol] =
new Connection((message) => this[onMessageSymbol](message));
}
@ -47,23 +54,21 @@ class Session extends EventEmitter {
post(method, params, callback) {
if (typeof method !== 'string') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'method', 'string', method);
throw new ERR_INVALID_ARG_TYPE('method', 'string', method);
}
if (!callback && util.isFunction(params)) {
callback = params;
params = null;
}
if (params && typeof params !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'params', 'Object', params);
throw new ERR_INVALID_ARG_TYPE('params', 'Object', params);
}
if (callback && typeof callback !== 'function') {
throw new errors.TypeError('ERR_INVALID_CALLBACK');
throw new ERR_INVALID_CALLBACK();
}
if (!this[connectionSymbol]) {
throw new errors.Error('ERR_INSPECTOR_NOT_CONNECTED');
throw new ERR_INSPECTOR_NOT_CONNECTED();
}
const id = this[nextIdSymbol]++;
const message = { id, method };
@ -83,7 +88,7 @@ class Session extends EventEmitter {
this[connectionSymbol] = null;
const remainingCallbacks = this[messageCallbacksSymbol].values();
for (const callback of remainingCallbacks) {
process.nextTick(callback, new errors.Error('ERR_INSPECTOR_CLOSED'));
process.nextTick(callback, new ERR_INSPECTOR_CLOSED());
}
this[messageCallbacksSymbol].clear();
this[nextIdSymbol] = 1;