1- // TODO(aduh95): use errors exported by the internal/errors module
2- /* eslint-disable no-restricted-syntax */
3-
41'use strict' ;
52
63const {
74 ArrayPrototypePush,
8- Error,
95 ErrorCaptureStackTrace,
106 FunctionPrototypeBind,
117 JSONParse,
@@ -15,6 +11,7 @@ const {
1511} = primordials ;
1612
1713const Buffer = require ( 'buffer' ) . Buffer ;
14+ const { ERR_DEBUGGER_ERROR } = require ( 'internal/errors' ) . codes ;
1815const { EventEmitter } = require ( 'events' ) ;
1916const http = require ( 'http' ) ;
2017const URL = require ( 'url' ) ;
@@ -39,7 +36,7 @@ const kEightBytePayloadLengthField = 127;
3936const kMaskingKeyWidthInBytes = 4 ;
4037
4138function unpackError ( { code, message, data } ) {
42- const err = new Error ( `${ message } - ${ data } ` ) ;
39+ const err = new ERR_DEBUGGER_ERROR ( `${ message } - ${ data } ` ) ;
4340 err . code = code ;
4441 ErrorCaptureStackTrace ( err , unpackError ) ;
4542 return err ;
@@ -101,14 +98,14 @@ function decodeFrameHybi17(data) {
10198 const masked = ( secondByte & kMaskBit ) !== 0 ;
10299 const compressed = reserved1 ;
103100 if ( compressed ) {
104- throw new Error ( 'Compressed frames not supported' ) ;
101+ throw new ERR_DEBUGGER_ERROR ( 'Compressed frames not supported' ) ;
105102 }
106103 if ( ! final || reserved2 || reserved3 ) {
107- throw new Error ( 'Only compression extension is supported' ) ;
104+ throw new ERR_DEBUGGER_ERROR ( 'Only compression extension is supported' ) ;
108105 }
109106
110107 if ( masked ) {
111- throw new Error ( 'Masked server frame - not supported' ) ;
108+ throw new ERR_DEBUGGER_ERROR ( 'Masked server frame - not supported' ) ;
112109 }
113110
114111 let closed = false ;
@@ -119,7 +116,7 @@ function decodeFrameHybi17(data) {
119116 case kOpCodeText :
120117 break ;
121118 default :
122- throw new Error ( `Unsupported op code ${ opCode } ` ) ;
119+ throw new ERR_DEBUGGER_ERROR ( `Unsupported op code ${ opCode } ` ) ;
123120 }
124121
125122 let payloadLength = secondByte & kPayloadLengthMask ;
@@ -183,7 +180,9 @@ class Client extends EventEmitter {
183180 debuglog ( '< %s' , payloadStr ) ;
184181 const lastChar = payloadStr [ payloadStr . length - 1 ] ;
185182 if ( payloadStr [ 0 ] !== '{' || lastChar !== '}' ) {
186- throw new Error ( `Payload does not look like JSON: ${ payloadStr } ` ) ;
183+ throw new ERR_DEBUGGER_ERROR (
184+ `Payload does not look like JSON: ${ payloadStr } `
185+ ) ;
187186 }
188187 let payload ;
189188 try {
@@ -204,7 +203,7 @@ class Client extends EventEmitter {
204203 this . emit ( 'debugEvent' , method , params ) ;
205204 this . emit ( method , params ) ;
206205 } else {
207- throw new Error ( `Unsupported response: ${ payloadStr } ` ) ;
206+ throw new ERR_DEBUGGER_ERROR ( `Unsupported response: ${ payloadStr } ` ) ;
208207 }
209208 }
210209 }
@@ -226,7 +225,7 @@ class Client extends EventEmitter {
226225 callMethod ( method , params ) {
227226 return new Promise ( ( resolve , reject ) => {
228227 if ( ! this . _socket ) {
229- reject ( new Error ( 'Use `run` to start the app again.' ) ) ;
228+ reject ( new ERR_DEBUGGER_ERROR ( 'Use `run` to start the app again.' ) ) ;
230229 return ;
231230 }
232231 const data = { id : ++ this . _lastId , method, params } ;
@@ -254,14 +253,17 @@ class Client extends EventEmitter {
254253 function parseChunks ( ) {
255254 const resBody = Buffer . concat ( chunks ) . toString ( ) ;
256255 if ( httpRes . statusCode !== 200 ) {
257- reject ( new Error ( `Unexpected ${ httpRes . statusCode } : ${ resBody } ` ) ) ;
256+ reject ( new ERR_DEBUGGER_ERROR (
257+ `Unexpected ${ httpRes . statusCode } : ${ resBody } `
258+ ) ) ;
258259 return ;
259260 }
260261 try {
261262 resolve ( JSONParse ( resBody ) ) ;
262263 } catch {
263- reject ( new Error ( `Response didn't contain JSON: ${ resBody } ` ) ) ;
264-
264+ reject ( new ERR_DEBUGGER_ERROR (
265+ `Response didn't contain JSON: ${ resBody } `
266+ ) ) ;
265267 }
266268 }
267269
0 commit comments