Created
October 30, 2014 06:50
-
-
Save dlidstrom/ab44635128d5b3616d2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
//'use strict'; allow default mode here, to be able to capture more detail | |
angular.module('PortalModule') | |
.config(ExceptionHandlerDecorator); | |
function ExceptionHandlerDecorator($provide) { | |
$provide.decorator('$exceptionHandler', ['$delegate', '$injector', function ($delegate, $injector) { | |
var apiUrl = '/api/logerror'; | |
return function (exception, cause) { | |
// original implementation | |
$delegate(exception, cause); | |
// avoid sending a bunch of the same error | |
var cache = {}; | |
var out = FormatMsg(exception); | |
if (cache.hasOwnProperty(out) == false) { | |
try { | |
// service locator pattern... | |
var $http = $injector.get('$http'); | |
$http.post( | |
apiUrl, | |
{ | |
message: out | |
}); | |
} catch (e) { | |
} | |
cache[out] = true; | |
} | |
}; | |
}]); | |
function FormatMsg(ex, stack) { | |
if (ex == null) return ''; | |
var url = ex.fileName != null ? ex.fileName : document.location; | |
if (stack == null && ex.stack != null) stack = ex.stack; | |
// format output | |
var out = ex.message != null ? ex.name + ": " + ex.message : ex; | |
out += ": at document path '" + url + "'."; | |
if (stack != null) { | |
try { | |
// not sure about the stack property, best be careful... | |
out += "\n at " + stack; | |
} catch (e) { | |
} | |
} | |
return out; | |
} | |
} | |
ExceptionHandlerDecorator.$inject = ['$provide']; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment