Skip to content

Commit da8b41b

Browse files
Matt Blagdenfacebook-github-bot
authored andcommitted
Report errors in the console
Summary: Error responses in CDP aren't generally displayed to the user. Currently, when metro-inspector-proxy fails to fetch a resource, we report success and embed the error message in the resource, allowing the user to see the error (e.g. if a source file failed to load, the debug client would still receive a success message, and display a "fake" source file containing the error). This change makes metro-inspector-proxy report errors at the CDP layer (if applicable), and also send an error message to the console so the user can see it. Reviewed By: newobj Differential Revision: D43167737 fbshipit-source-id: e9ecf7f663e8f1aa8a6d48483bef0a0a7ca28ed8
1 parent 6690b39 commit da8b41b

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

packages/metro-inspector-proxy/src/Device.js

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import type {
1313
DebuggerRequest,
14+
ErrorResponse,
1415
GetScriptSourceRequest,
1516
GetScriptSourceResponse,
1617
MessageFromDevice,
@@ -385,11 +386,9 @@ class Device {
385386
'data:application/json;charset=utf-8;base64,' +
386387
new Buffer(sourceMap).toString('base64');
387388
} catch (exception) {
388-
payload.params.sourceMapURL =
389-
'data:application/json;charset=utf-8;base64,' +
390-
new Buffer(
391-
`Failed to fetch source map: ${exception.message}`,
392-
).toString('base64');
389+
this._sendErrorToDebugger(
390+
`Failed to fetch source map ${params.sourceMapURL}: ${exception.message}`,
391+
);
393392
}
394393
}
395394
}
@@ -497,16 +496,22 @@ class Device {
497496
}
498497
}
499498

500-
async _processDebuggerGetScriptSource(
499+
_processDebuggerGetScriptSource(
501500
req: GetScriptSourceRequest,
502501
socket: typeof WS,
503502
) {
504-
let scriptSource = `Source for script with id '${req.params.scriptId}' was not found.`;
505-
506-
const sendResponse = () => {
503+
const sendSuccessResponse = (scriptSource: string) => {
507504
const result: GetScriptSourceResponse = {scriptSource};
508505
socket.send(JSON.stringify({id: req.id, result}));
509506
};
507+
const sendErrorResponse = (error: string) => {
508+
// Tell the client that the request failed
509+
const result: ErrorResponse = {error: {message: error}};
510+
socket.send(JSON.stringify({id: req.id, result}));
511+
512+
// Send to the console as well, so the user can see it
513+
this._sendErrorToDebugger(error);
514+
};
510515

511516
const pathToSource = this._scriptIdToSourcePathMapping.get(
512517
req.params.scriptId,
@@ -515,25 +520,27 @@ class Device {
515520
const httpURL = this._tryParseHTTPURL(pathToSource);
516521
if (httpURL) {
517522
this._fetchText(httpURL).then(
518-
text => {
519-
scriptSource = text;
520-
sendResponse();
521-
},
522-
err => {
523-
scriptSource = err.message;
524-
sendResponse();
525-
},
523+
text => sendSuccessResponse(text),
524+
err =>
525+
sendErrorResponse(
526+
`Failed to fetch source url ${pathToSource}: ${err.message}`,
527+
),
526528
);
527529
} else {
530+
let file;
528531
try {
529-
scriptSource = fs.readFileSync(
532+
file = fs.readFileSync(
530533
path.resolve(this._projectRoot, pathToSource),
531534
'utf8',
532535
);
533536
} catch (err) {
534-
scriptSource = err.message;
537+
sendErrorResponse(
538+
`Failed to fetch source file ${pathToSource}: ${err.message}`,
539+
);
540+
}
541+
if (file) {
542+
sendSuccessResponse(file);
535543
}
536-
sendResponse();
537544
}
538545
}
539546
}
@@ -579,6 +586,27 @@ class Device {
579586
}
580587
return text;
581588
}
589+
590+
_sendErrorToDebugger(message: string) {
591+
const debuggerSocket = this._debuggerConnection?.socket;
592+
if (debuggerSocket && debuggerSocket.readyState === WS.OPEN) {
593+
debuggerSocket.send(
594+
JSON.stringify({
595+
method: 'Runtime.consoleAPICalled',
596+
params: {
597+
args: [
598+
{
599+
type: 'string',
600+
value: message,
601+
},
602+
],
603+
executionContextId: 0,
604+
type: 'error',
605+
},
606+
}),
607+
);
608+
}
609+
}
582610
}
583611

584612
module.exports = Device;

packages/metro-inspector-proxy/src/types.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ export type GetScriptSourceResponse = {
126126
bytecode?: string,
127127
};
128128

129+
export type ErrorResponse = {
130+
error: {
131+
message: string,
132+
},
133+
};
134+
129135
export type DebuggerRequest =
130136
| SetBreakpointByUrlRequest
131137
| GetScriptSourceRequest;

0 commit comments

Comments
 (0)