From dc9218136b2e973129bc8cc86a77157746fa404e 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 Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca Reviewed-By: Jan Krems --- lib/internal/inspector/inspect_client.js | 25 +++++++++++------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/internal/inspector/inspect_client.js b/lib/internal/inspector/inspect_client.js index 4c89c56af93ef6..cd277a4cf5a946 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,7 @@ 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 +201,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 +223,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,13 +251,13 @@ 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}`)); } }