Skip to content

Commit

Permalink
debugger: use ERR_DEBUGGER_ERROR in debugger client
Browse files Browse the repository at this point in the history
PR-URL: nodejs#39024
Backport-PR-URL: nodejs#39446
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
  • Loading branch information
Trott authored and foxxyz committed Oct 18, 2021
1 parent af9e841 commit 753e2be
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions lib/internal/inspector/inspect_client.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// TODO(aduh95): use errors exported by the internal/errors module
/* eslint-disable no-restricted-syntax */

'use strict';

const {
ArrayPrototypePush,
Error,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
JSONParse,
Expand All @@ -15,6 +11,7 @@ const {
} = primordials;

const Buffer = require('buffer').Buffer;
const { ERR_DEBUGGER_ERROR } = require('internal/errors').codes;
const { EventEmitter } = require('events');
const http = require('http');
const URL = require('url');
Expand All @@ -39,7 +36,7 @@ const kEightBytePayloadLengthField = 127;
const kMaskingKeyWidthInBytes = 4;

function unpackError({ code, message, data }) {
const err = new Error(`${message} - ${data}`);
const err = new ERR_DEBUGGER_ERROR(`${message} - ${data}`);
err.code = code;
ErrorCaptureStackTrace(err, unpackError);
return err;
Expand Down Expand Up @@ -101,14 +98,14 @@ function decodeFrameHybi17(data) {
const masked = (secondByte & kMaskBit) !== 0;
const compressed = reserved1;
if (compressed) {
throw new Error('Compressed frames not supported');
throw new ERR_DEBUGGER_ERROR('Compressed frames not supported');
}
if (!final || reserved2 || reserved3) {
throw new Error('Only compression extension is supported');
throw new ERR_DEBUGGER_ERROR('Only compression extension is supported');
}

if (masked) {
throw new Error('Masked server frame - not supported');
throw new ERR_DEBUGGER_ERROR('Masked server frame - not supported');
}

let closed = false;
Expand All @@ -119,7 +116,7 @@ function decodeFrameHybi17(data) {
case kOpCodeText:
break;
default:
throw new Error(`Unsupported op code ${opCode}`);
throw new ERR_DEBUGGER_ERROR(`Unsupported op code ${opCode}`);
}

let payloadLength = secondByte & kPayloadLengthMask;
Expand Down Expand Up @@ -183,7 +180,9 @@ class Client extends EventEmitter {
debuglog('< %s', payloadStr);
const lastChar = payloadStr[payloadStr.length - 1];
if (payloadStr[0] !== '{' || lastChar !== '}') {
throw new Error(`Payload does not look like JSON: ${payloadStr}`);
throw new ERR_DEBUGGER_ERROR(
`Payload does not look like JSON: ${payloadStr}`
);
}
let payload;
try {
Expand All @@ -204,7 +203,7 @@ class Client extends EventEmitter {
this.emit('debugEvent', method, params);
this.emit(method, params);
} else {
throw new Error(`Unsupported response: ${payloadStr}`);
throw new ERR_DEBUGGER_ERROR(`Unsupported response: ${payloadStr}`);
}
}
}
Expand All @@ -226,7 +225,7 @@ class Client extends EventEmitter {
callMethod(method, params) {
return new Promise((resolve, reject) => {
if (!this._socket) {
reject(new Error('Use `run` to start the app again.'));
reject(new ERR_DEBUGGER_ERROR('Use `run` to start the app again.'));
return;
}
const data = { id: ++this._lastId, method, params };
Expand Down Expand Up @@ -254,14 +253,17 @@ class Client extends EventEmitter {
function parseChunks() {
const resBody = Buffer.concat(chunks).toString();
if (httpRes.statusCode !== 200) {
reject(new Error(`Unexpected ${httpRes.statusCode}: ${resBody}`));
reject(new ERR_DEBUGGER_ERROR(
`Unexpected ${httpRes.statusCode}: ${resBody}`
));
return;
}
try {
resolve(JSONParse(resBody));
} catch {
reject(new Error(`Response didn't contain JSON: ${resBody}`));

reject(new ERR_DEBUGGER_ERROR(
`Response didn't contain JSON: ${resBody}`
));
}
}

Expand Down

0 comments on commit 753e2be

Please sign in to comment.