Created
October 15, 2015 14:26
-
-
Save rokerkony/76248ada884ae67c4b66 to your computer and use it in GitHub Desktop.
This file contains 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
namespace SellerCenterExpress { | |
'use strict'; | |
class ResponseHandler implements ng.IHttpInterceptor { | |
public response: (response: ng.IHttpPromiseCallbackArg<any>) => any; | |
public responseError: (rejection: any) => any; | |
constructor ( | |
protected $log: ng.ILogService, | |
protected $q: ng.IQService, | |
protected ErrorTypes: IErrorTypes, | |
protected NotificationService: service.INotificationService | |
) { | |
ResponseHandler.prototype.response = (response: ng.IHttpPromiseCallbackArg<any>) => { | |
this.$log.info('HTTP response ', response); | |
// if content type is null make it application/json (for templates in cache) | |
let contentType = response.headers('content-type') || 'application/json'; | |
if ('application/json' !== contentType) { | |
let errorMsg: model.Error = this.ErrorTypes.get(HttpStatusCodes.INTERNAL_SERVER_ERROR); | |
this.NotificationService.broadcast( | |
service.NotificationType.error, | |
errorMsg.text, | |
errorMsg.title | |
); | |
} | |
return response; | |
}; | |
ResponseHandler.prototype.responseError = (rejection) => { | |
this.$log.error('HTTP error ', rejection); | |
// todo atm we handle all API errors here | |
// let errorMessage = rejection.data; | |
let error: model.Error = this.ErrorTypes.get(rejection.status); | |
this.NotificationService.broadcast( | |
service.NotificationType.error, | |
error.text, | |
error.title | |
); | |
return this.$q.reject(rejection); | |
}; | |
} | |
static factory () { | |
let handler = ( | |
$log: ng.ILogService, | |
$q: ng.IQService, | |
ErrorTypes: IErrorTypes, | |
NotificationService: service.INotificationService | |
) => { | |
return new ResponseHandler($log, $q, ErrorTypes, NotificationService); | |
}; | |
handler.$inject = [ | |
'$log', | |
'$q', | |
'ErrorTypes', | |
'NotificationService', | |
]; | |
return handler; | |
} | |
} | |
/** | |
* @ngdoc function | |
* @name scxApp.factory:ResponseHandler | |
* @description | |
* # ResponseHandler | |
* checks API responses and redirects accordingly | |
*/ | |
scxApp.factory('ResponseHandler', ResponseHandler.factory()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment