remote/Error.jsm
author Yuki Shiino <yukishiino@chromium.org>
Wed, 06 Mar 2019 12:36:18 +0000
changeset 522566 80f87e0a5ed3cd2f37f5f7d0664d61c479826b72
parent 521127 06c54b40f31249275aebae31d17a1aa3add93058
child 526569 c1ee8401a2c7fdf5d5fe2653f96616657648fc44
permissions -rw-r--r--
Bug 1530852 [wpt PR 15497] - v8binding: Make PaintWorkletGlobalScope use IDL callback function, a=testonly Automatic update from web-platform-tests v8binding: Make PaintWorkletGlobalScope use IDL callback function Converts PaintWorkletGlobalScope.registerPaint's second argument |paintCtor| into IDL callback function type. Also, makes Paint Worklet use |PaintCallback| of callback function type. Change-Id: I6706499705a116c85f5c092a46309854db88708c Reviewed-on: https://chromium-review.googlesource.com/c/1478850 Reviewed-by: Kentaro Hara <haraken@chromium.org> Reviewed-by: Hitoshi Yoshida <peria@chromium.org> Commit-Queue: Yuki Shiino <yukishiino@chromium.org> Cr-Commit-Position: refs/heads/master@{#634582} -- wpt-commits: 270d9bf4444b52a2d66dd10ade64cdfa455f2300 wpt-pr: 15497

/* 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",
  "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");

XPCOMUtils.defineLazyGetter(this, "log", Log.get);

class RemoteAgentError extends Error {
  constructor(message = "", cause = undefined) {
    cause = cause || message;
    super(cause);

    this.name = this.constructor.name;
    this.message = message;
    this.cause = cause;

    this.notify();
  }

  notify() {
    Cu.reportError(this);
    log.error(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.toString());
  }

  toString() {
    return formatError(this, {stack: true});
  }

  quit(mode = Ci.nsIAppStartup.eForceQuit) {
    Services.startup.quit(mode);
  }
}

/** When an operation is not yet implemented. */
class UnsupportedError extends RemoteAgentError {}

/** 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 && 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}));
    }
  }

  return ls.join("\n");
}