Skip to content

Commit

Permalink
Added debug event info to WebSocketProvider (#1018).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Oct 7, 2020
1 parent ef8e433 commit 8e682cc
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions packages/providers/src.ts/websocket-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,32 @@ export class WebSocketProvider extends JsonRpcProvider {
if (result.result !== undefined) {
request.callback(null, result.result);

this.emit("debug", {
action: "response",
request: JSON.parse(request.payload),
response: result.result,
provider: this
});

} else {
let error: Error = null;
if (result.error) {
const error: any = new Error(result.error.message || "unknown error");
defineReadOnly(error, "code", result.error.code || null);
defineReadOnly(error, "response", data);
request.callback(error, undefined);
error = new Error(result.error.message || "unknown error");
defineReadOnly(<any>error, "code", result.error.code || null);
defineReadOnly(<any>error, "response", data);
} else {
request.callback(new Error("unknown error"), undefined);
error = new Error("unknown error");
}

request.callback(error, undefined);

this.emit("debug", {
action: "response",
error: error,
request: JSON.parse(request.payload),
provider: this
});

}

} else if (result.method === "eth_subscription") {
Expand Down Expand Up @@ -176,6 +193,12 @@ export class WebSocketProvider extends JsonRpcProvider {
jsonrpc: "2.0"
});

this.emit("debug", {
action: "request",
request: JSON.parse(payload),
provider: this
});

this._requests[String(rid)] = { callback, payload };

if (this._wsReady) { this._websocket.send(payload); }
Expand Down

0 comments on commit 8e682cc

Please sign in to comment.