AngularJS provides a service called $exceptionHandler. It handles errors by capturing them and logging them to the console using the $log service, another AngularJS service that wraps up console.log() to make it safe to use if the console object doesn’t exist. Also, Angular tries to provide a cause along with the error to provide some additional context to what went wrong.
Here is the example
angular.module("YOUR_APP", [])
.config(["$provide", function ($provide) {
$provide.decorator("$exceptionHandler", ["$delegate", "$window", "myConfig", function($delegate, $window, myConfig) {
return function (exception, cause) {
if(myConfig.environment === "production") {
$window.sessionStorage.log = $window.sessionStorage.log || [];
$window.sessionStorage.log.append({ exception: exception, cause: cause });
}
else {
$delegate(exception, cause);
}
};
}]);
}]);