Skip to content

Wrap send errors with proper stack trace #571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions lib/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const defaults = require('./defaults.js');
const devtools = require('./devtools.js');

class ProtocolError extends Error {
constructor(request, response) {
constructor(request, response, opts) {
let {message} = response;
if (response.data) {
message += ` (${response.data})`;
}
super(message);
super(message, opts);
// attach the original response as well
this.request = request;
this.response = response;
Expand Down Expand Up @@ -84,20 +84,27 @@ class Chrome extends EventEmitter {
this._enqueueCommand(method, params, sessionId, callback);
return undefined;
} else {
return new Promise((fulfill, reject) => {
return this._sendPromise(method, params, sessionId);
}
}

async _sendPromise(method, params, sessionId) {
try {
return await new Promise((fulfill, reject) => {
this._enqueueCommand(method, params, sessionId, (error, response) => {
if (error) {
const request = {method, params, sessionId};
reject(
error instanceof Error
? error // low-level WebSocket error
: new ProtocolError(request, response)
);
reject({ error, response });
} else {
fulfill(response);
}
});
});
} catch (err) {
if (err instanceof Error) {
throw new Error('low-level WebSocket error', { cause: err });
}
const request = { method, params, sessionId };
throw new ProtocolError(request, err?.response, { cause: err?.error });
}
}

Expand Down