Guest User

Untitled

a guest
Feb 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. class ErrorInterceptor {
  2.  
  3. static $inject = ['$q'];
  4. constructor(private $q: ng.IQService) {}
  5.  
  6. public request = (config) => {
  7. // Do something on success
  8. return config;
  9. }
  10.  
  11. public requestError = (config) => {
  12. // Do something on error
  13. return config;
  14. }
  15.  
  16. public response = (response) => {
  17. // Do something on success
  18. return response;
  19. }
  20.  
  21. public responseError = (responseFailure) => {
  22. // Do something on error
  23. // Handle error codes separately if needed, e.g.:
  24.  
  25. if (responseFailure.status === -1) {
  26.  
  27. } else if (responseFailure.status === 401) {
  28.  
  29. } else if (responseFailure.status === 403) {
  30.  
  31. } else if (responseFailure.status === 404) {
  32.  
  33. } else if (responseFailure.status === 500) {
  34.  
  35. } else if (responseFailure.status === 503) {
  36.  
  37. } else {
  38.  
  39. }
  40.  
  41. return this.$q.reject(responseFailure);
  42. }
  43. }
  44.  
  45. const ErrorInterceptorConfig = ['$httpProvider', ($httpProvider) => {
  46. $httpProvider.interceptors.push('ErrorInterceptor');
  47. }];
  48.  
  49. // Register the service and configuration:
  50. angular.module('yourModule')
  51. .service('ErrorInterceptor', ErrorInterceptor)
  52. .config(ErrorInterceptorConfig);
Add Comment
Please, Sign In to add comment