Browse Source

inspector: migrate to internal/errors

PR-URL: https://github.com/nodejs/node/pull/15619
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
v9.x-staging
James M Snell 7 years ago
parent
commit
4cf56ad6f2
  1. 25
      doc/api/errors.md
  2. 20
      lib/inspector.js
  3. 4
      lib/internal/errors.js
  4. 62
      test/parallel/test-inspector-module.js

25
doc/api/errors.md

@ -885,6 +885,31 @@ Used when `http2.connect()` is passed a URL that uses any protocol other than
Used when a given index is out of the accepted range (e.g. negative offsets). Used when a given index is out of the accepted range (e.g. negative offsets).
<a id="ERR_INSPECTOR_ALREADY_CONNECTED"></a>
### ERR_INSPECTOR_ALREADY_CONNECTED
When using the `inspector` module, the `ERR_INSPECTOR_ALREADY_CONNECTED` error
code is used when an attempt is made to connect when the inspector is already
connected.
<a id="ERR_INSPECTOR_CLOSED"></a>
### ERR_INSPECTOR_CLOSED
When using the `inspector` module, the `ERR_INSPECTOR_CLOSED` error code is
used when an attempt is made to use the inspector after the session has
already closed.
<a id="ERR_INSPECTOR_NOT_AVAILABLE"></a>
### ERR_INSPECTOR_NOT_AVAILABLE
Used to identify when the `inspector` module is not available for use.
<a id="ERR_INSPECTOR_NOT_CONNECTED"></a>
### ERR_INSPECTOR_NOT_CONNECTED
When using the `inspector` module, the `ERR_INSPECTOR_NOT_CONNECTED` error code
is used when an attempt is made to use the inspector before it is connected.
<a id="ERR_INVALID_ARG_TYPE"></a> <a id="ERR_INVALID_ARG_TYPE"></a>
### ERR_INVALID_ARG_TYPE ### ERR_INVALID_ARG_TYPE

20
lib/inspector.js

@ -1,11 +1,12 @@
'use strict'; 'use strict';
const EventEmitter = require('events'); const EventEmitter = require('events');
const errors = require('internal/errors');
const util = require('util'); const util = require('util');
const { Connection, open, url } = process.binding('inspector'); const { Connection, open, url } = process.binding('inspector');
if (!Connection) if (!Connection)
throw new Error('Inspector is not available'); throw new errors.Error('ERR_INSPECTOR_NOT_AVAILABLE');
const connectionSymbol = Symbol('connectionProperty'); const connectionSymbol = Symbol('connectionProperty');
const messageCallbacksSymbol = Symbol('messageCallbacks'); const messageCallbacksSymbol = Symbol('messageCallbacks');
@ -22,7 +23,7 @@ class Session extends EventEmitter {
connect() { connect() {
if (this[connectionSymbol]) if (this[connectionSymbol])
throw new Error('Already connected'); throw new errors.Error('ERR_INSPECTOR_ALREADY_CONNECTED');
this[connectionSymbol] = this[connectionSymbol] =
new Connection((message) => this[onMessageSymbol](message)); new Connection((message) => this[onMessageSymbol](message));
} }
@ -46,24 +47,23 @@ class Session extends EventEmitter {
post(method, params, callback) { post(method, params, callback) {
if (typeof method !== 'string') { if (typeof method !== 'string') {
throw new TypeError( throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
`"method" must be a string, got ${typeof method} instead`); 'method', 'string', method);
} }
if (!callback && util.isFunction(params)) { if (!callback && util.isFunction(params)) {
callback = params; callback = params;
params = null; params = null;
} }
if (params && typeof params !== 'object') { if (params && typeof params !== 'object') {
throw new TypeError( throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
`"params" must be an object, got ${typeof params} instead`); 'params', 'object', params);
} }
if (callback && typeof callback !== 'function') { if (callback && typeof callback !== 'function') {
throw new TypeError( throw new errors.TypeError('ERR_INVALID_CALLBACK');
`"callback" must be a function, got ${typeof callback} instead`);
} }
if (!this[connectionSymbol]) { if (!this[connectionSymbol]) {
throw new Error('Session is not connected'); throw new errors.Error('ERR_INSPECTOR_NOT_CONNECTED');
} }
const id = this[nextIdSymbol]++; const id = this[nextIdSymbol]++;
const message = { id, method }; const message = { id, method };
@ -83,7 +83,7 @@ class Session extends EventEmitter {
this[connectionSymbol] = null; this[connectionSymbol] = null;
const remainingCallbacks = this[messageCallbacksSymbol].values(); const remainingCallbacks = this[messageCallbacksSymbol].values();
for (const callback of remainingCallbacks) { for (const callback of remainingCallbacks) {
process.nextTick(callback, new Error('Session was closed')); process.nextTick(callback, new errors.Error('ERR_INSPECTOR_CLOSED'));
} }
this[messageCallbacksSymbol].clear(); this[messageCallbacksSymbol].clear();
this[nextIdSymbol] = 1; this[nextIdSymbol] = 1;

4
lib/internal/errors.js

@ -236,6 +236,10 @@ E('ERR_HTTP_INVALID_STATUS_CODE',
E('ERR_HTTP_TRAILER_INVALID', E('ERR_HTTP_TRAILER_INVALID',
'Trailers are invalid with this transfer encoding'); 'Trailers are invalid with this transfer encoding');
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range'); E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
E('ERR_INSPECTOR_ALREADY_CONNECTED', 'The inspector is already connected');
E('ERR_INSPECTOR_CLOSED', 'Session was closed');
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available');
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected');
E('ERR_INVALID_ARG_TYPE', invalidArgType); E('ERR_INVALID_ARG_TYPE', invalidArgType);
E('ERR_INVALID_ARG_VALUE', E('ERR_INVALID_ARG_VALUE',
(name, value) => { (name, value) => {

62
test/parallel/test-inspector-module.js

@ -0,0 +1,62 @@
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const assert = require('assert');
const { Session } = require('inspector');
const session = new Session();
common.expectsError(
() => session.post('Runtime.evaluate', { expression: '2 + 2' }),
{
code: 'ERR_INSPECTOR_NOT_CONNECTED',
type: Error,
message: 'Session is not connected'
}
);
assert.doesNotThrow(() => session.connect());
assert.doesNotThrow(
() => session.post('Runtime.evaluate', { expression: '2 + 2' }));
[1, {}, [], true, Infinity, undefined].forEach((i) => {
common.expectsError(
() => session.post(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message:
'The "method" argument must be of type string. ' +
`Received type ${typeof i}`
}
);
});
[1, true, Infinity].forEach((i) => {
common.expectsError(
() => session.post('test', i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message:
'The "params" argument must be of type object. ' +
`Received type ${typeof i}`
}
);
});
common.expectsError(
() => session.connect(),
{
code: 'ERR_INSPECTOR_ALREADY_CONNECTED',
type: Error,
message: 'The inspector is already connected'
}
);
assert.doesNotThrow(() => session.disconnect());
assert.doesNotThrow(() => session.disconnect());
Loading…
Cancel
Save