Bug 1568151 - Replace `target.getInspector()` by `target.getFront("inspector")`....
[gecko.git] / remote / Error.jsm
blobd7a83400cb7b8ac1be2fe175e626e189b2c646a8
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/. */
5 "use strict";
7 var EXPORTED_SYMBOLS = [
8   "FatalError",
9   "RemoteAgentError",
10   "UnknownMethodError",
11   "UnsupportedError",
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;
25     super(cause);
27     this.name = this.constructor.name;
28     this.message = message;
29     this.cause = cause;
31     this.notify();
32   }
34   notify() {
35     Cu.reportError(this);
36     log.error(this.toString({ stack: true }));
37   }
39   toString({ stack = false } = {}) {
40     return RemoteAgentError.format(this, { stack });
41   }
43   static format(e, { stack = false } = {}) {
44     return formatError(e, { stack });
45   }
47   /**
48    * Takes a serialised CDP error and reconstructs it
49    * as a RemoteAgentError.
50    *
51    * The error must be of this form:
52    *
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"}
56    *
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
61    * property.
62    *
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.
67    *
68    * @return {RemoteAgentError}
69    */
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");
75     err.cause = null;
76     return err;
77   }
80 /**
81  * A fatal error that it is not possible to recover from
82  * or send back to the client.
83  *
84  * Constructing this error will force the application to quit.
85  */
86 class FatalError extends RemoteAgentError {
87   constructor(...args) {
88     super(...args);
89     this.quit();
90   }
92   notify() {
93     log.fatal(this.toString({ stack: true }));
94   }
96   quit(mode = Ci.nsIAppStartup.eForceQuit) {
97     Services.startup.quit(mode);
98   }
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) {
107     if (command) {
108       super(`${domain}.${command}`);
109     } else {
110       super(domain);
111     }
112   }
115 function formatError(error, { stack = false } = {}) {
116   const els = [];
118   els.push(error.name);
119   if (error.message) {
120     els.push(": ");
121     els.push(error.message);
122   }
124   if (stack && error.stack) {
125     els.push(":\n");
127     const stack = error.stack.trim().split("\n");
128     els.push(stack.map(line => `\t${line}`).join("\n"));
130     if (error.cause) {
131       els.push("\n");
132       els.push("caused by: " + formatError(error.cause, { stack }));
133     }
134   }
136   return els.join("");