Skip to content

Commit

Permalink
improve InspectorProxyWorker debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
RamIdeas committed Nov 3, 2023
1 parent 42fcf89 commit b55a13a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
5 changes: 4 additions & 1 deletion packages/wrangler/src/api/startDevWorker/ProxyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,10 @@ export class ProxyController extends EventEmitter {
switch (message.type) {
case "runtime-websocket-error":
// TODO: consider sending proxyData again to trigger the InspectorProxyWorker to reconnect to the runtime
logger.error(message.error);
logger.error(
"[InspectorProxyWorker] 'runtime websocket' error",
message.error
);

break;
case "error":
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/api/startDevWorker/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function serialiseError(e: unknown): SerializedError {
message: e.message,
name: e.name,
stack: e.stack,
cause: serialiseError(e.cause),
cause: e.cause && serialiseError(e.cause),
};
} else {
return { message: String(e) };
Expand Down
37 changes: 30 additions & 7 deletions packages/wrangler/templates/startDevWorker/InspectorProxyWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,27 @@ export class InspectorProxyWorker implements DurableObject {

const { 0: response, 1: proxyController } = new WebSocketPair();
proxyController.accept();
proxyController.addEventListener("close", () => {
proxyController.addEventListener("close", (event) => {
// don't reconnect the proxyController websocket
// ProxyController can detect this event and reconnect itself

this.sendDebugLog(
"PROXY CONTROLLER WEBSOCKET CLOSED",
event.code,
event.reason
);

if (this.websockets.proxyController === proxyController) {
this.websockets.proxyController = undefined;
}
});
proxyController.addEventListener("error", () => {
proxyController.addEventListener("error", (event) => {
// don't reconnect the proxyController websocket
// ProxyController can detect this event and reconnect itself

const error = serialiseError(event.error);
this.sendDebugLog("PROXY CONTROLLER WEBSOCKET ERROR", error);

if (this.websockets.proxyController === proxyController) {
this.websockets.proxyController = undefined;
}
Expand Down Expand Up @@ -310,6 +319,9 @@ export class InspectorProxyWorker implements DurableObject {
});

runtime.addEventListener("error", (event) => {
const error = serialiseError(event.error);
this.sendDebugLog("RUNTIME WEBSOCKET ERROR", error);

clearInterval(this.runtimeKeepAliveInterval);

if (this.websockets.runtime === runtime) {
Expand All @@ -318,10 +330,7 @@ export class InspectorProxyWorker implements DurableObject {

this.sendProxyControllerRequest({
type: "runtime-websocket-error",
error: {
message: event.message,
cause: event.error,
},
error,
});

// don't reconnect the runtime websocket
Expand Down Expand Up @@ -452,12 +461,21 @@ export class InspectorProxyWorker implements DurableObject {
);
} else {
devtools.addEventListener("message", this.handleDevToolsIncomingMessage);
devtools.addEventListener("close", () => {
devtools.addEventListener("close", (event) => {
this.sendDebugLog(
"DEVTOOLS WEBSOCKET CLOSED",
event.code,
event.reason
);

if (this.websockets.devtools === devtools) {
this.websockets.devtools = undefined;
}
});
devtools.addEventListener("error", (event) => {
const error = serialiseError(event.error);
this.sendDebugLog("DEVTOOLS WEBSOCKET ERROR", error);

if (this.websockets.devtools === devtools) {
this.websockets.devtools = undefined;
}
Expand Down Expand Up @@ -546,6 +564,11 @@ export class InspectorProxyWorker implements DurableObject {
url: message.params.url,
});
if (response === undefined) {
this.sendDebugLog(
`ProxyController could not resolve Network.loadNetworkResource for "${message.params.url}"`
);

// When the ProxyController cannot resolve a resource, let the runtime handle the request
this.sendRuntimeMessage(JSON.stringify(message));
} else {
// this.websockets.devtools can be undefined here
Expand Down

0 comments on commit b55a13a

Please sign in to comment.