Skip to content

Instantly share code, notes, and snippets.

@dlidstrom
Created October 30, 2014 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlidstrom/ab44635128d5b3616d2a to your computer and use it in GitHub Desktop.
Save dlidstrom/ab44635128d5b3616d2a to your computer and use it in GitHub Desktop.
(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