2019-01-29 18:18:42 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = [
|
|
|
|
"FatalError",
|
2019-03-08 01:08:44 +03:00
|
|
|
"RemoteAgentError",
|
2019-02-23 21:59:31 +03:00
|
|
|
"UnknownMethodError",
|
|
|
|
"UnsupportedError",
|
2019-01-29 18:18:42 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
const { Log } = ChromeUtils.import("chrome://remote/content/Log.jsm");
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "log", Log.get);
|
|
|
|
|
|
|
|
class RemoteAgentError extends Error {
|
2019-02-27 22:06:20 +03:00
|
|
|
constructor(message = "", cause = undefined) {
|
2019-02-27 22:06:20 +03:00
|
|
|
cause = cause || message;
|
2019-02-27 22:06:20 +03:00
|
|
|
super(cause);
|
|
|
|
|
2019-02-23 21:59:31 +03:00
|
|
|
this.name = this.constructor.name;
|
2019-02-27 22:06:20 +03:00
|
|
|
this.message = message;
|
|
|
|
this.cause = cause;
|
|
|
|
|
2019-01-29 18:18:42 +03:00
|
|
|
this.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
notify() {
|
|
|
|
Cu.reportError(this);
|
2019-04-03 14:42:08 +03:00
|
|
|
log.error(this.toString({ stack: true }));
|
2019-02-27 22:06:20 +03:00
|
|
|
}
|
|
|
|
|
2019-03-08 01:08:44 +03:00
|
|
|
toString({ stack = false } = {}) {
|
|
|
|
return RemoteAgentError.format(this, { stack });
|
|
|
|
}
|
|
|
|
|
|
|
|
static format(e, { stack = false } = {}) {
|
|
|
|
return formatError(e, { stack });
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
2019-04-25 18:16:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a serialised CDP error and reconstructs it
|
|
|
|
* as a RemoteAgentError.
|
|
|
|
*
|
|
|
|
* The error must be of this form:
|
|
|
|
*
|
|
|
|
* {"message": "TypeError: foo is not a function\n
|
|
|
|
* execute@chrome://remote/content/sessions/Session.jsm:73:39\n
|
|
|
|
* onMessage@chrome://remote/content/sessions/TabSession.jsm:65:20"}
|
|
|
|
*
|
|
|
|
* This approach has the notable deficiency that it cannot deal
|
|
|
|
* with causes to errors because of the unstructured nature of CDP
|
|
|
|
* errors. A possible future improvement would be to extend the
|
|
|
|
* error serialisation to include discrete fields for each data
|
|
|
|
* property.
|
|
|
|
*
|
|
|
|
* @param {Object} json
|
|
|
|
* CDP error encoded as a JSON object, which must have a
|
|
|
|
* "message" field, where the first line will make out the error
|
|
|
|
* message and the subsequent lines the stacktrace.
|
|
|
|
*
|
|
|
|
* @return {RemoteAgentError}
|
|
|
|
*/
|
|
|
|
static fromJSON(json) {
|
|
|
|
const [message, ...stack] = json.message.split("\n");
|
|
|
|
const err = new RemoteAgentError();
|
|
|
|
err.message = message.slice(0, -1);
|
|
|
|
err.stack = stack.map(s => s.trim()).join("\n");
|
|
|
|
err.cause = null;
|
|
|
|
return err;
|
|
|
|
}
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A fatal error that it is not possible to recover from
|
|
|
|
* or send back to the client.
|
|
|
|
*
|
2019-02-27 22:06:20 +03:00
|
|
|
* Constructing this error will force the application to quit.
|
2019-01-29 18:18:42 +03:00
|
|
|
*/
|
2019-02-12 16:58:23 +03:00
|
|
|
class FatalError extends RemoteAgentError {
|
2019-01-29 18:18:42 +03:00
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
|
|
|
this.quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
notify() {
|
2019-04-03 14:42:08 +03:00
|
|
|
log.fatal(this.toString({ stack: true }));
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
quit(mode = Ci.nsIAppStartup.eForceQuit) {
|
|
|
|
Services.startup.quit(mode);
|
|
|
|
}
|
2019-02-12 17:44:54 +03:00
|
|
|
}
|
2019-01-29 18:18:42 +03:00
|
|
|
|
2019-02-27 22:06:20 +03:00
|
|
|
/** When an operation is not yet implemented. */
|
2019-02-12 17:44:54 +03:00
|
|
|
class UnsupportedError extends RemoteAgentError {}
|
2019-01-29 18:18:42 +03:00
|
|
|
|
2019-02-27 22:06:20 +03:00
|
|
|
/** The requested remote method does not exist. */
|
2019-05-27 14:39:16 +03:00
|
|
|
class UnknownMethodError extends RemoteAgentError {
|
|
|
|
constructor(domain, command = null) {
|
|
|
|
if (command) {
|
|
|
|
super(`${domain}.${command}`);
|
|
|
|
} else {
|
|
|
|
super(domain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-23 21:59:31 +03:00
|
|
|
|
2019-01-29 18:18:42 +03:00
|
|
|
function formatError(error, { stack = false } = {}) {
|
2019-04-04 12:55:14 +03:00
|
|
|
const els = [];
|
2019-02-27 22:06:20 +03:00
|
|
|
|
2019-04-04 12:55:14 +03:00
|
|
|
els.push(error.name);
|
|
|
|
if (error.message) {
|
|
|
|
els.push(": ");
|
|
|
|
els.push(error.message);
|
|
|
|
}
|
2019-02-27 22:06:20 +03:00
|
|
|
|
2019-03-08 01:08:44 +03:00
|
|
|
if (stack && error.stack) {
|
2019-04-04 12:55:14 +03:00
|
|
|
els.push(":\n");
|
|
|
|
|
2019-02-27 22:06:20 +03:00
|
|
|
const stack = error.stack.trim().split("\n");
|
2019-04-04 12:55:14 +03:00
|
|
|
els.push(stack.map(line => `\t${line}`).join("\n"));
|
2019-02-27 22:06:20 +03:00
|
|
|
|
|
|
|
if (error.cause) {
|
2019-04-04 12:55:14 +03:00
|
|
|
els.push("\n");
|
|
|
|
els.push("caused by: " + formatError(error.cause, { stack }));
|
2019-02-27 22:06:20 +03:00
|
|
|
}
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
2019-04-04 12:55:14 +03:00
|
|
|
return els.join("");
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|