From db9de349938eba9acb2113618d63f071577c01e2 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 17 Nov 2014 18:11:39 -0800 Subject: [PATCH] Add onWillThrowError --- src/atom.coffee | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/atom.coffee b/src/atom.coffee index 944e5049d..0a0497fbe 100644 --- a/src/atom.coffee +++ b/src/atom.coffee @@ -173,11 +173,21 @@ class Atom extends Model require('grim').deprecate = -> window.onerror = => - @openDevTools() - @executeJavaScriptInDevTools('InspectorFrontendAPI.showConsole()') @lastUncaughtError = Array::slice.call(arguments) + [message, url, line, column, originalError] = @lastUncaughtError + eventObject = {message, url, line, column, originalError} + + openDevTools = true + eventObject.preventDefault = -> openDevTools = false + + @emitter.emit 'will-throw-error', eventObject + + if openDevTools + @openDevTools() + @executeJavaScriptInDevTools('InspectorFrontendAPI.showConsole()') + @emit 'uncaught-error', arguments... - @emitter.emit 'did-throw-error', arguments... + @emitter.emit 'did-throw-error', message @unsubscribe() @setBodyPlatformClass() @@ -248,6 +258,22 @@ class Atom extends Model onDidBeep: (callback) -> @emitter.on 'did-beep', callback + # Extended: Invoke the given callback when there is an unhandled error, but + # before the devtools pop open + # + # * `callback` {Function} to be called whenever there is an unhandled error + # * `event` {Object} + # * `originalError` {Object} the original error object + # * `message` {String} the original error object + # * `url` {String} Url to the file where the error originated. + # * `line` {Number} + # * `column` {Number} + # * `preventDefault` {Function} call this to avoid popping up the dev tools. + # + # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. + onWillThrowError: (callback) -> + @emitter.on 'will-throw-error', callback + # Extended: Invoke the given callback whenever there is an unhandled error. # # * `callback` {Function} to be called whenever there is an unhandled error