How would you implement application-wide exception handling in your Angular app?

Technology CommunityCategory: AngularJSHow would you implement application-wide exception handling in your Angular app?
VietMX Staff asked 3 years ago

Angular has a built-in error handler service called $exceptionHandler which can easily be overriden as seen below:

myApp.factory('$exceptionHandler', function ($log, ErrorService) {
	return function (exception, cause) {
		if (console) {
			$log.error(exception);
			$log.error(cause);
		}

		ErrorService.send(exception, cause);
	};
});

This is very useful for sending errors to third party error logging services or helpdesk applications. Errors trapped inside of event callbacks are not propagated to this handler, but can manually be relayed to this handler by calling $exceptionHandler(e) from within a try catch block.