From a3991d7c18a21b45db623d0b42b067ca4421138b Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 12 Jun 2021 17:35:49 -0700 Subject: [PATCH] debugger: use ERR_DEBUGGER_ERROR in debugger client PR-URL: https://github.com/nodejs/node/pull/39024 Backport-PR-URL: https://github.com/nodejs/node/pull/39446 Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca Reviewed-By: Jan Krems --- lib/internal/inspector/inspect_client.js | 32 +++++++++++++----------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/internal/inspector/inspect_client.js b/lib/internal/inspector/inspect_client.js index 4c89c56af93ef6..8fc9011ca888ed 100644 --- a/lib/internal/inspector/inspect_client.js +++ b/lib/internal/inspector/inspect_client.js @@ -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, @@ -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'); @@ -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; @@ -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; @@ -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; @@ -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 { @@ -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}`); } } } @@ -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 }; @@ -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}` + )); } }