Created
December 21, 2017 14:47
-
-
Save spiralx/be2ee0016285daae75a505342e75baf5 to your computer and use it in GitHub Desktop.
Decorator for exception handling in Angular.JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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