When working with Angular $http, how often do you write boiler-plate code to log an error like this?...
angular.module('app').factory('someFactory', function ($http, someLogger) {
...
$http.get(...).then(successCallback, errorCallback);
...
function errorCallback (response) {
if (response.status === 404) {
//do something
}
if (response.status === 500) {
//do another
}
//...and so on~
}
});
Wouldn't you rather do this?...
//Handle Globally with interceptor
angular.module('app').config(function ($httpProvider) {
$httpProvider.interceptors.push('quickErr');
});
//OR handle it with optional Namespace for more specific cases
angular.module('app').factory('someFactory', function ($http, quickErr) {
...
...
function errorCallback (response) {
quickErr.handle(response, 'SOME_ERROR_CODE'); // THIS!!
}
});
YES! This module will allow you to pre-define error handlers as simple JSON, and then replace your super long boiler-plate error handling code with a one-liner!
- Download this repository, or
install with Bower
bower install --save angular-http-quickerr #UNREGISTERED
- Include script
...
<script src="PATH/TO/SCRIPT/dist/angular-http-quickerr.min.js"></script>
...
- Add
'ngHttpQuickErr'
module to your main Angular module
var app = angular.module('app', ['ngHttpQuickErr']);
'quickErrProvider'
will be injected during angular configuration phase
angular.module('app').config(function (quickErrProvider) {
...
});
-
Use
setGlobalHandlers()
to set default handlers to be use across the app Format:quickErrProvider.setGlobalHandlers({ 400: { //See "Configuration Options" section below for description template: 'This is a {{status}}: {{description}}', postLog: true logger: customLogger.error }, ... ... });
-
Use
setCustomHandlers()
to set error handlers with specific "namespace" Format:quickErrProvider.setCustomHandler({ 'some-namespace': { 400: { // This will override global settings if namespace is provided during error handling ... } }, ... });
-
Use
setResponseFormat
to set the property name of the response JSON to be use as the key - DEFAULT: 'status'- Example response JSON:
{ "status-code": 400, "description": "This is a 400 error", "data": {...} }
- Set the key to match the property that contains your error code
quickErrProvider.setResponseFormat({key: 'status-code'});
Note:
By default, the module is set to use console.error
to log error. The module passes 2 argument when calling the log function - message and response object, respectively.
Here is how to replace the global logger setting,
quickErrProvider.setGlobalHandlers({
DEFAULT: {
logger: newLogger.logFunction //provide function to call to log error
}
});
-
'template'
: String -> message to log- Can be plain string, e.g. "Hello, I am your error". Or...
- Template String to interpolate, e.g. "{{status-code}} - {{description}}".
- Anything with
{{Property Name}}
will be replaced with the value of that property from the Response JSON
- Anything with
-
'postLog'
: Action to perform after logging an error message. There are 3 options- Boolean -> set to
true
to rethrow rejected promise - String -> path to redirect, needs to start with '/'. For example, '/login'
- Function -> default callback function
- Boolean -> set to
-
'logger'
: Function -> custom logging function.
As seen in the example above, use 'quickErr'
service to handle an error. simply call quickErr.handle()
inside the Angular promise catch()
or errorCallback block
-
handle error with Global settings, pass
handle()
the response objectangular.module('app').service(function ($http, quickErr) { ... function someFunctionThatCallsHTTP() { $http.get(...) .then(successCallback) .catch(function (res) { quickErr.handle(res); }); ; } });
-
handle error with Custom settings, pass
handle()
the response object AND namespaceangular.module('app').service(function ($http, quickErr) { ... function someFunctionThatCallsHTTP() { $http.get(...) .then(successCallback) .catch(function (res) { quickErr.handle(res, 'some-namespace'); }); ; } });
-
Provide a callback to call after logging an error. quickErr will call the callback function instead of "postLog" action
quickErr.handle(res, undefined, someCallbackFn);