Skip to content

Commit 4cf56ad

Browse files
committed
inspector: migrate to internal/errors
PR-URL: #15619 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent b932854 commit 4cf56ad

File tree

4 files changed

+101
-10
lines changed

4 files changed

+101
-10
lines changed

doc/api/errors.md

+25
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,31 @@ Used when `http2.connect()` is passed a URL that uses any protocol other than
885885

886886
Used when a given index is out of the accepted range (e.g. negative offsets).
887887

888+
<a id="ERR_INSPECTOR_ALREADY_CONNECTED"></a>
889+
### ERR_INSPECTOR_ALREADY_CONNECTED
890+
891+
When using the `inspector` module, the `ERR_INSPECTOR_ALREADY_CONNECTED` error
892+
code is used when an attempt is made to connect when the inspector is already
893+
connected.
894+
895+
<a id="ERR_INSPECTOR_CLOSED"></a>
896+
### ERR_INSPECTOR_CLOSED
897+
898+
When using the `inspector` module, the `ERR_INSPECTOR_CLOSED` error code is
899+
used when an attempt is made to use the inspector after the session has
900+
already closed.
901+
902+
<a id="ERR_INSPECTOR_NOT_AVAILABLE"></a>
903+
### ERR_INSPECTOR_NOT_AVAILABLE
904+
905+
Used to identify when the `inspector` module is not available for use.
906+
907+
<a id="ERR_INSPECTOR_NOT_CONNECTED"></a>
908+
### ERR_INSPECTOR_NOT_CONNECTED
909+
910+
When using the `inspector` module, the `ERR_INSPECTOR_NOT_CONNECTED` error code
911+
is used when an attempt is made to use the inspector before it is connected.
912+
888913
<a id="ERR_INVALID_ARG_TYPE"></a>
889914
### ERR_INVALID_ARG_TYPE
890915

lib/inspector.js

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

33
const EventEmitter = require('events');
4+
const errors = require('internal/errors');
45
const util = require('util');
56
const { Connection, open, url } = process.binding('inspector');
67

78
if (!Connection)
8-
throw new Error('Inspector is not available');
9+
throw new errors.Error('ERR_INSPECTOR_NOT_AVAILABLE');
910

1011
const connectionSymbol = Symbol('connectionProperty');
1112
const messageCallbacksSymbol = Symbol('messageCallbacks');
@@ -22,7 +23,7 @@ class Session extends EventEmitter {
2223

2324
connect() {
2425
if (this[connectionSymbol])
25-
throw new Error('Already connected');
26+
throw new errors.Error('ERR_INSPECTOR_ALREADY_CONNECTED');
2627
this[connectionSymbol] =
2728
new Connection((message) => this[onMessageSymbol](message));
2829
}
@@ -46,24 +47,23 @@ class Session extends EventEmitter {
4647

4748
post(method, params, callback) {
4849
if (typeof method !== 'string') {
49-
throw new TypeError(
50-
`"method" must be a string, got ${typeof method} instead`);
50+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
51+
'method', 'string', method);
5152
}
5253
if (!callback && util.isFunction(params)) {
5354
callback = params;
5455
params = null;
5556
}
5657
if (params && typeof params !== 'object') {
57-
throw new TypeError(
58-
`"params" must be an object, got ${typeof params} instead`);
58+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
59+
'params', 'object', params);
5960
}
6061
if (callback && typeof callback !== 'function') {
61-
throw new TypeError(
62-
`"callback" must be a function, got ${typeof callback} instead`);
62+
throw new errors.TypeError('ERR_INVALID_CALLBACK');
6363
}
6464

6565
if (!this[connectionSymbol]) {
66-
throw new Error('Session is not connected');
66+
throw new errors.Error('ERR_INSPECTOR_NOT_CONNECTED');
6767
}
6868
const id = this[nextIdSymbol]++;
6969
const message = { id, method };
@@ -83,7 +83,7 @@ class Session extends EventEmitter {
8383
this[connectionSymbol] = null;
8484
const remainingCallbacks = this[messageCallbacksSymbol].values();
8585
for (const callback of remainingCallbacks) {
86-
process.nextTick(callback, new Error('Session was closed'));
86+
process.nextTick(callback, new errors.Error('ERR_INSPECTOR_CLOSED'));
8787
}
8888
this[messageCallbacksSymbol].clear();
8989
this[nextIdSymbol] = 1;

lib/internal/errors.js

+4
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ E('ERR_HTTP_INVALID_STATUS_CODE',
236236
E('ERR_HTTP_TRAILER_INVALID',
237237
'Trailers are invalid with this transfer encoding');
238238
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
239+
E('ERR_INSPECTOR_ALREADY_CONNECTED', 'The inspector is already connected');
240+
E('ERR_INSPECTOR_CLOSED', 'Session was closed');
241+
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available');
242+
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected');
239243
E('ERR_INVALID_ARG_TYPE', invalidArgType);
240244
E('ERR_INVALID_ARG_VALUE',
241245
(name, value) => {
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
common.skipIfInspectorDisabled();
6+
7+
const assert = require('assert');
8+
const { Session } = require('inspector');
9+
10+
const session = new Session();
11+
12+
common.expectsError(
13+
() => session.post('Runtime.evaluate', { expression: '2 + 2' }),
14+
{
15+
code: 'ERR_INSPECTOR_NOT_CONNECTED',
16+
type: Error,
17+
message: 'Session is not connected'
18+
}
19+
);
20+
21+
assert.doesNotThrow(() => session.connect());
22+
23+
assert.doesNotThrow(
24+
() => session.post('Runtime.evaluate', { expression: '2 + 2' }));
25+
26+
[1, {}, [], true, Infinity, undefined].forEach((i) => {
27+
common.expectsError(
28+
() => session.post(i),
29+
{
30+
code: 'ERR_INVALID_ARG_TYPE',
31+
type: TypeError,
32+
message:
33+
'The "method" argument must be of type string. ' +
34+
`Received type ${typeof i}`
35+
}
36+
);
37+
});
38+
39+
[1, true, Infinity].forEach((i) => {
40+
common.expectsError(
41+
() => session.post('test', i),
42+
{
43+
code: 'ERR_INVALID_ARG_TYPE',
44+
type: TypeError,
45+
message:
46+
'The "params" argument must be of type object. ' +
47+
`Received type ${typeof i}`
48+
}
49+
);
50+
});
51+
52+
common.expectsError(
53+
() => session.connect(),
54+
{
55+
code: 'ERR_INSPECTOR_ALREADY_CONNECTED',
56+
type: Error,
57+
message: 'The inspector is already connected'
58+
}
59+
);
60+
61+
assert.doesNotThrow(() => session.disconnect());
62+
assert.doesNotThrow(() => session.disconnect());

0 commit comments

Comments
 (0)