Skip to content

Instantly share code, notes, and snippets.

@spiralx
Created December 21, 2017 14:47
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 spiralx/be2ee0016285daae75a505342e75baf5 to your computer and use it in GitHub Desktop.
Save spiralx/be2ee0016285daae75a505342e75baf5 to your computer and use it in GitHub Desktop.
Decorator for exception handling in Angular.JS
/*
* See http://ways-of-working-in-allatra.readthedocs.io/en/latest/angularjs/README.html
*/
import * as angular from 'angular'
// ------------------------------------------------------------
angular
.module('app.exception')
.config([ '$provide', exceptionConfig ])
.factory('exception', [
'logger',
function exception(logger) {
return new ExceptionLogger(logger)
}
])
// ------------------------------------------------------------
function exceptionConfig($provide) {
$provide.decorator('$exceptionHandler', [
'$delegate',
'toastr',
($delegate, toastr) => (exception, cause) => {
$delegate(exception, cause)
toastr.error(exception.msg, { exception, cause })
}
]
}
// ------------------------------------------------------------
class ExceptionLogger {
constructor(logger) {
this.logger = logger
}
catch(message) {
return reason => logger.error(message, reason)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment