diff --git a/remote/domains/content/runtime/ExecutionContext.jsm b/remote/domains/content/runtime/ExecutionContext.jsm index 498971626e53..063c152691e1 100644 --- a/remote/domains/content/runtime/ExecutionContext.jsm +++ b/remote/domains/content/runtime/ExecutionContext.jsm @@ -59,24 +59,37 @@ class ExecutionContext { }; } if (rv.throw) { - if (this._debuggee.executeInGlobalWithBindings("e instanceof Error", {e: rv.throw}).return) { - return { - exceptionDetails: { - text: this._debuggee.executeInGlobalWithBindings("e.message", {e: rv.throw}).return, - }, - }; - } - return { - exceptionDetails: { - exception: this._createRemoteObject(rv.throw), - }, - }; + return this._returnError(rv.throw); } return { result: this._createRemoteObject(rv.return), }; } + /** + * Given a Debugger.Object reference for an Exception, return a JSON object + * describing the exception by following CDP ExceptionDetails specification. + */ + _returnError(exception) { + if (this._debuggee.executeInGlobalWithBindings("exception instanceof Error", + {exception}).return) { + const text = this._debuggee.executeInGlobalWithBindings("exception.message", + {exception}).return; + return { + exceptionDetails: { + text, + }, + }; + } + + // If that isn't an Error, consider the exception as a JS value + return { + exceptionDetails: { + exception: this._toRemoteObject(exception), + }, + }; + } + /** * Convert a given `Debugger.Object` to a JSON string. *