1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 var EXPORTED_SYMBOLS = [
14 const { Log } = ChromeUtils.import("chrome://remote/content/Log.jsm");
15 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
16 const { XPCOMUtils } = ChromeUtils.import(
17 "resource://gre/modules/XPCOMUtils.jsm"
20 XPCOMUtils.defineLazyGetter(this, "log", Log.get);
22 class RemoteAgentError extends Error {
23 constructor(message = "", cause = undefined) {
24 cause = cause || message;
27 this.name = this.constructor.name;
28 this.message = message;
36 log.error(this.toString({ stack: true }));
39 toString({ stack = false } = {}) {
40 return RemoteAgentError.format(this, { stack });
43 static format(e, { stack = false } = {}) {
44 return formatError(e, { stack });
48 * Takes a serialised CDP error and reconstructs it
49 * as a RemoteAgentError.
51 * The error must be of this form:
53 * {"message": "TypeError: foo is not a function\n
54 * execute@chrome://remote/content/sessions/Session.jsm:73:39\n
55 * onMessage@chrome://remote/content/sessions/TabSession.jsm:65:20"}
57 * This approach has the notable deficiency that it cannot deal
58 * with causes to errors because of the unstructured nature of CDP
59 * errors. A possible future improvement would be to extend the
60 * error serialisation to include discrete fields for each data
63 * @param {Object} json
64 * CDP error encoded as a JSON object, which must have a
65 * "message" field, where the first line will make out the error
66 * message and the subsequent lines the stacktrace.
68 * @return {RemoteAgentError}
70 static fromJSON(json) {
71 const [message, ...stack] = json.message.split("\n");
72 const err = new RemoteAgentError();
73 err.message = message.slice(0, -1);
74 err.stack = stack.map(s => s.trim()).join("\n");
81 * A fatal error that it is not possible to recover from
82 * or send back to the client.
84 * Constructing this error will force the application to quit.
86 class FatalError extends RemoteAgentError {
87 constructor(...args) {
93 log.fatal(this.toString({ stack: true }));
96 quit(mode = Ci.nsIAppStartup.eForceQuit) {
97 Services.startup.quit(mode);
101 /** When an operation is not yet implemented. */
102 class UnsupportedError extends RemoteAgentError {}
104 /** The requested remote method does not exist. */
105 class UnknownMethodError extends RemoteAgentError {
106 constructor(domain, command = null) {
108 super(`${domain}.${command}`);
115 function formatError(error, { stack = false } = {}) {
118 els.push(error.name);
121 els.push(error.message);
124 if (stack && error.stack) {
127 const stack = error.stack.trim().split("\n");
128 els.push(stack.map(line => `\t${line}`).join("\n"));
132 els.push("caused by: " + formatError(error.cause, { stack }));