4 ms·
With AngularJS, you can process errors your way by implementing $exceptionHandler. (Otherwise errors originating within framework wouldn't reach window.onerror—
by goblin89 13y ago
With AngularJS, you can process errors your way by implementing $exceptionHandler. (Otherwise errors originating within framework wouldn't reach window.onerror—default implementation of $exceptionHandler will catch them and log to console.)
This code, for example, reports each otherwise unhandled exception to user prettily using Messenger.js:
window.angular.module('yourApp', []).
// <…>
factory('$exceptionHandler', [
'$log',
function ($log) {
return function (exception, cause) {
// Log to console
$log.error.apply($log, arguments);
// Show to user
var msg = sprintf("Error: %s", exception.message);
if (cause) { msg = sprintf("%s. %s", msg, cause); }
window.Messenger().post({
type: 'error',
hideAfter: 0,
showCloseButton: true,
message: msg
});
};
}
]);
To replicate functionality from the article, you could make a request using $http service in there.
(I assume it's possible to override not $exceptionHandler, but $log service directly. This would allow for more extensive server-side log collection, not just error reporting. Never tried that myself, though.)