Skip to content

Commit ac6c767

Browse files
knightburtonyichoi
authored andcommitted
Override the sendErrorResponse function (#35)
IoT.js-Debug-DCO-1.0-Signed-off-by: Imre Kiss [email protected]
1 parent 8cb80c0 commit ac6c767

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

src/IotjsDebugger.ts

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ class IotjsDebugSession extends DebugSession {
105105
this.log('attachRequest');
106106

107107
if (!args.address || args.address === '') {
108-
this.sendErrorResponse(response, 0, 'Must specify an address');
108+
this.sendErrorResponse(response, new Error('Must specify an address'));
109109
return;
110110
}
111111

112112
if (!args.port || args.port <= 0 || args.port > 35535) {
113-
this.sendErrorResponse(response, 0, 'Must specify a valid port');
113+
this.sendErrorResponse(response, new Error('Must specify a valid port'));
114114
return;
115115
}
116116

117117
if (!args.localRoot || args.localRoot === '') {
118-
this.sendErrorResponse(response, 0, 'Must specify a localRoot');
118+
this.sendErrorResponse(response, new Error('Must specify a localRoot'));
119119
return;
120120
}
121121

@@ -187,7 +187,7 @@ class IotjsDebugSession extends DebugSession {
187187
})
188188
.catch(error => {
189189
this.log(error);
190-
this.sendErrorResponse(response, 0, error.message);
190+
this.sendErrorResponse(response, error);
191191
})
192192
.then(() => {
193193
this.sendEvent(new InitializedEvent());
@@ -197,7 +197,7 @@ class IotjsDebugSession extends DebugSession {
197197
protected launchRequest(response: DebugProtocol.LaunchResponse, args: DebugProtocol.LaunchRequestArguments): void {
198198
this.log('launchRequest');
199199

200-
this.sendErrorResponse(response, 0, 'Launching is not supported. Use Attach.');
200+
this.sendErrorResponse(response, new Error('Launching is not supported. Use Attach.'));
201201
}
202202

203203
protected disconnectRequest(
@@ -224,7 +224,7 @@ class IotjsDebugSession extends DebugSession {
224224
.then(() => {
225225
this.sendResponse(response);
226226
})
227-
.catch(error => this.sendErrorResponse(response, 0, (<Error>error).message));
227+
.catch(error => this.sendErrorResponse(response, <Error>error));
228228
}
229229

230230
protected nextRequest(response: DebugProtocol.NextResponse, args: DebugProtocol.NextArguments): void {
@@ -234,7 +234,7 @@ class IotjsDebugSession extends DebugSession {
234234
.then(() => {
235235
this.sendResponse(response);
236236
})
237-
.catch(error => this.sendErrorResponse(response, 0, (<Error>error).message));
237+
.catch(error => this.sendErrorResponse(response, <Error>error));
238238
}
239239

240240
protected stepInRequest(response: DebugProtocol.StepInResponse, args: DebugProtocol.StepInArguments): void {
@@ -244,7 +244,7 @@ class IotjsDebugSession extends DebugSession {
244244
.then(() => {
245245
this.sendResponse(response);
246246
})
247-
.catch(error => this.sendErrorResponse(response, 0, (<Error>error).message));
247+
.catch(error => this.sendErrorResponse(response, <Error>error));
248248
}
249249

250250
protected stepOutRequest(response: DebugProtocol.StepOutResponse, args: DebugProtocol.StepOutArguments): void {
@@ -254,7 +254,7 @@ class IotjsDebugSession extends DebugSession {
254254
.then(() => {
255255
this.sendResponse(response);
256256
})
257-
.catch(error => this.sendErrorResponse(response, 0, (<Error>error).message));
257+
.catch(error => this.sendErrorResponse(response, <Error>error));
258258
}
259259

260260
protected pauseRequest(response: DebugProtocol.PauseResponse, args: DebugProtocol.PauseArguments): void {
@@ -264,7 +264,7 @@ class IotjsDebugSession extends DebugSession {
264264
.then(() => {
265265
this.sendResponse(response);
266266
})
267-
.catch(error => this.sendErrorResponse(response, 0, (<Error>error).message));
267+
.catch(error => this.sendErrorResponse(response, <Error>error));
268268
}
269269

270270
protected async setBreakPointsRequest(
@@ -312,7 +312,7 @@ class IotjsDebugSession extends DebugSession {
312312
response.body = { breakpoints: [...persistingBreakpoints, ...newBreakpoints] };
313313
} catch (error) {
314314
this.log(error);
315-
this.sendErrorResponse(response, 0, (<Error>error).message);
315+
this.sendErrorResponse(response, <Error>error);
316316
return;
317317
}
318318

@@ -333,7 +333,7 @@ class IotjsDebugSession extends DebugSession {
333333

334334
this.sendResponse(response);
335335
})
336-
.catch(error => this.sendErrorResponse(response, 0, (<Error>error).message));
336+
.catch(error => this.sendErrorResponse(response, <Error>error));
337337
}
338338

339339
protected stackTraceRequest(
@@ -359,7 +359,7 @@ class IotjsDebugSession extends DebugSession {
359359

360360
this.sendResponse(response);
361361
})
362-
.catch(error => this.sendErrorResponse(response, 0, (<Error>error).message));
362+
.catch(error => this.sendErrorResponse(response, <Error>error));
363363
}
364364

365365
protected customRequest(command: string, response: DebugProtocol.Response, args: any): void {
@@ -377,7 +377,7 @@ class IotjsDebugSession extends DebugSession {
377377
.catch(error => {
378378
this.log(error);
379379
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.NOP;
380-
this.sendErrorResponse(response, 0, (<Error>error).message, null, ErrorDestination.User);
380+
this.sendErrorResponse(response, <Error>error, ErrorDestination.User);
381381
});
382382
return;
383383
}
@@ -386,6 +386,45 @@ class IotjsDebugSession extends DebugSession {
386386
}
387387
}
388388

389+
// Overrides.
390+
391+
protected sendErrorResponse(
392+
response: DebugProtocol.Response,
393+
error: Error,
394+
dest?: ErrorDestination
395+
): void;
396+
397+
protected sendErrorResponse(
398+
response: DebugProtocol.Response,
399+
codeOrMessage: number | DebugProtocol.Message,
400+
format?: string,
401+
variables?: any,
402+
dest?: ErrorDestination
403+
): void;
404+
405+
protected sendErrorResponse(response: DebugProtocol.Response) {
406+
if (arguments[1] instanceof Error) {
407+
const error = arguments[1] as Error & {code?: number | string; errno?: number};
408+
const dest = arguments[2] as ErrorDestination;
409+
410+
let code: number;
411+
412+
if (typeof error.code === 'number') {
413+
code = error.code as number;
414+
} else if (typeof error.errno === 'number') {
415+
code = error.errno;
416+
} else {
417+
code = 0;
418+
}
419+
420+
super.sendErrorResponse(response, code, error.message, dest);
421+
} else {
422+
super.sendErrorResponse(response, arguments[1], arguments[2], arguments[3], arguments[4]);
423+
}
424+
}
425+
426+
// Helper functions
427+
389428
private handleSource(data: JerryMessageScriptParsed): void {
390429
const path = `${this._args.localRoot}/${this.pathToBasename(data.name)}`;
391430
const src = this._protocolhandler.getSource(data.id);

0 commit comments

Comments
 (0)