bug 1523104: remote: associate formatting with error prototype; r=ochameau
authorAndreas Tolfsen <ato@sny.no>
Thu, 07 Mar 2019 22:08:44 +0000
changeset 521127 06c54b40f312
parent 521126 2b1bde33c72e
child 521128 d9cce6c1e153
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersochameau
bugs1523104
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 1523104: remote: associate formatting with error prototype; r=ochameau
remote/Error.jsm
--- a/remote/Error.jsm
+++ b/remote/Error.jsm
@@ -1,17 +1,17 @@
 /* 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",
-  "formatError",
+  "RemoteAgentError",
   "UnknownMethodError",
   "UnsupportedError",
 ];
 
 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");
 
@@ -29,38 +29,42 @@ class RemoteAgentError extends Error {
     this.notify();
   }
 
   notify() {
     Cu.reportError(this);
     log.error(formatError(this));
   }
 
-  format() {
-    return formatError(this);
+  toString({stack = false} = {}) {
+    return RemoteAgentError.format(this, {stack});
+  }
+
+  static format(e, {stack = false} = {}) {
+    return formatError(e, {stack});
   }
 }
 
 /**
  * A fatal error that it is not possible to recover from
  * or send back to the client.
  *
  * Constructing this error will force the application to quit.
  */
 class FatalError extends RemoteAgentError {
   constructor(...args) {
     super(...args);
     this.quit();
   }
 
   notify() {
-    log.fatal(this.format());
+    log.fatal(this.toString());
   }
 
-  format() {
+  toString() {
     return formatError(this, {stack: true});
   }
 
   quit(mode = Ci.nsIAppStartup.eForceQuit) {
     Services.startup.quit(mode);
   }
 }
 
@@ -70,17 +74,17 @@ class UnsupportedError extends RemoteAge
 /** The requested remote method does not exist. */
 class UnknownMethodError extends RemoteAgentError {}
 
 function formatError(error, {stack = false} = {}) {
   const ls = [];
 
   ls.push(`${error.name}: ${error.message ? `${error.message}:` : ""}`);
 
-  if (stack) {
+  if (stack && error.stack) {
     const stack = error.stack.trim().split("\n");
     ls.push(stack.map(line => `\t${line}`).join("\n"));
 
     if (error.cause) {
       ls.push("caused by: " + formatError(error.cause, {stack}));
     }
   }