-
-
Notifications
You must be signed in to change notification settings - Fork 958
/
Copy pathexception-handler.provider.js
66 lines (60 loc) · 2.14 KB
/
exception-handler.provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Include in index.html so that app level exceptions are handled.
// Exclude from testRunner.html which should run exactly what it wants to run
(function() {
'use strict';
angular
.module('blocks.exception')
.provider('exceptionHandler', exceptionHandlerProvider)
.config(config);
/**
* Must configure the exception handling
* @return {[type]}
*/
function exceptionHandlerProvider() {
/* jshint validthis:true */
this.config = {
appErrorPrefix: undefined
};
this.configure = function (appErrorPrefix) {
this.config.appErrorPrefix = appErrorPrefix;
};
this.$get = function() {
return {config: this.config};
};
}
/**
* Configure by setting an optional string value for appErrorPrefix.
* Accessible via config.appErrorPrefix (via config value).
* @param {[type]} $provide
* @return {[type]}
* @ngInject
*/
function config($provide) {
$provide.decorator('$exceptionHandler', extendExceptionHandler);
}
/**
* Extend the $exceptionHandler service to also display a toast.
* @param {Object} $delegate
* @param {Object} exceptionHandler
* @param {Object} logger
* @return {Function} the decorated $exceptionHandler service
*/
function extendExceptionHandler($delegate, exceptionHandler, logger) {
return function(exception, cause) {
var appErrorPrefix = exceptionHandler.config.appErrorPrefix || '';
var errorData = {exception: exception, cause: cause};
exception.message = appErrorPrefix + exception.message;
$delegate(exception, cause);
/**
* Could add the error to a service's collection,
* add errors to $rootScope, log errors to remote web server,
* or log locally. Or throw hard. It is entirely up to you.
* throw exception;
*
* @example
* throw { message: 'error message we added' };
*/
logger.error(exception.message, errorData);
};
}
})();