(function(){ "use strict"; angular .module("app.utils") .service("appUtils.errorHandler", errorHandler); function errorHandler(uiNotifications, APP_SETTINGS, messages){ var _this = this; this.config = { "CUSTOM_HANDLER": uiNotifications.popup, "SHOW_MESSAGE": APP_SETTINGS.SHOW_UI_MESSAGES, "DEV_MODE": APP_SETTINGS.DEVELOPMENT }; this.STATUS = { "SUCCESS": true, "FAIL": false }; var uiComponent = _this.config.CUSTOM_HANDLER || window.alert; // Handle response status and inform about it user. this.check = function(response, status, config) { if (status == -1) { _this.config.DEV_MODE && siteDisabledException(response, status, config); return false; } if (!response && status !== 404) { _this.config.SHOW_MESSAGE && uiComponent(messages.CAN_NOT_ESTABLISH_CONNECTION); return false; } if (status == 400) { _this.config.DEV_MODE && handleBadRequestException(response, status, config); return false; } if (status == 401) { _this.config.DEV_MODE && authorizationDeniedException(response, status, config); return false; } if (status == 403) { _this.config.DEV_MODE && serverDisabledException(response, status, config); return false; } if (status == 404) { _this.config.DEV_MODE && handleNotFoundException(response, status, config); config.url.indexOf('/login') > -1 && _this.config.SHOW_MESSAGE && uiComponent(response.message); return false; } if (status == 409) { _this.config.DEV_MODE && conflictException(response, status, config); config.url.indexOf('/login') > -1 && _this.config.SHOW_MESSAGE && uiComponent(response.message); return false; } if (status == 415) { _this.config.DEV_MODE && incorrectMediaTypeException(response, status, config); return false; } if (status == 500) { _this.config.DEV_MODE && handleServerException(response, status, config); return false; } return true; }; function authorizationDeniedException(response, status, config){ displayCommonResponseInfo(response, status, config); console.error("Details", messages.ATTEMPT_USE_HTTP_REQUEST_WITHOUT_PERMISSION); } function handleBadRequestException(response, status, config){ displayCommonResponseInfo(response, status, config); } function incorrectMediaTypeException(response, status, config){ displayCommonResponseInfo({message: "Incorrect media type."}, status, config); } function handleNotFoundException(response, status, config){ displayCommonResponseInfo(response, status, config); } function conflictException(response, status, config){ handleNotFoundException(response, status, config); } function handleServerException(response, status, config){ displayCommonResponseInfo(response, status, config); } function siteDisabledException(response, status, config){ displayCommonResponseInfo(messages.SITE_DISABLED_403, status, config); } function displayCommonResponseInfo(response, status, config){ console.info("Url: " + config.url); console.warn("Http status: " + status); console.error("Response message: ", response.message || response); } } errorHandler.$inject = [ "appUtils.uiNotifications", "app.SETTINGS", "appConstant.messages" ]; })();