UNPKG

unserver-unify

Version:

101 lines 3.86 kB
'use strict'; angular.module('bamboo.common', []).constant('httpErrors', { 0: 'HTTP_ERROR_0', 400: 'HTTP_ERROR_400', 401: 'HTTP_ERROR_401', 402: 'HTTP_ERROR_402', 403: 'HTTP_ERROR_403', 404: 'HTTP_ERROR_404', 500: 'HTTP_ERROR_500' }).provider('errorHandler', function() { // Wrap a single function [func] in another function that handles both synchronous and asynchonous errors. function decorate($injector, obj, func) { return angular.extend(function() { var handler = $injector.get('errorHandler'); return handler.call(func, obj, arguments); }, func); } // Decorate all functions of the service [$delegate] with error handling. This function should be used as decorator // function in a call to $provide.decorator(). var decorator = ['$delegate', '$injector', function($delegate, $injector) { // Loop over all functions in $delegate and wrap these functions using the [decorate] functions above. for (var prop in $delegate) { if (angular.isFunction($delegate[prop])) { $delegate[prop] = decorate($injector, $delegate, $delegate[prop]); } } return $delegate; }]; var showNoBlockErr = function(CommonService, error) { if (typeof(error) == "string" && error.length !== 0) { CommonService.showNoBlockErr(error); } }; // The actual service: return { // Decorate the mentioned [services] with automatic error handling. decorate: function($provide, services) { angular.forEach(services, function(service) { $provide.decorator(service, decorator); }); }, $get: function($log, $translate, CommonService, httpErrors) { var handler = { errors: [], funcError: function(func, err) { if (err && !angular.isUndefined(err.status)) { if (err.status == 401 || err.status == 403) { err = httpErrors[err.status]; var errmsg = $translate.instant(err); showNoBlockErr(CommonService, errmsg); } else { return; } } else if (err && err.message) { // Exceptions are unwrapped. err = err.message; } console.log(err); console.log(func); if (!angular.isString(err)) { err = 'An unknown error occurred.'; } var errmsg = $translate.instant(err); $log.error('Caught error: ' + errmsg); //handler.errors.push(errmsg); //if (err.status == 401 || err.status == 403) { //showNoBlockErr(CommonService, errmsg); //} //showNoBlockErr(CommonService, errmsg); }, // Call the provided function [func] with the provided [args] and error handling enabled. call: function(func, self, args) { //$log.debug('Function called: ', (func.name || func)); var result; try { result = func.apply(self, args); } catch (err) { // Catch synchronous errors. handler.funcError(func, err); throw err; } // Catch asynchronous errors. var promise = result && result.$promise || result; if (promise && angular.isFunction(promise.then) && angular.isFunction(promise['catch'])) { // promise is a genuine promise, so we call [handler.async]. handler.async(func, promise); } return result; }, // Automatically record rejections of the provided [promise]. async: function(func, promise) { promise['catch'](function(err) { handler.funcError(func, err); }); return promise; } }; return handler; } }; })