public override void OnException(ExceptionContext context) { if (context.Exception is UserException) { UserException izuzetak = context.Exception as UserException; foreach ((string key, string msg)x in izuzetak.Errori) { context.ModelState.AddModelError(x.key, x.msg); } context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; } else if (context.Exception is AuthorizeException) { AuthorizeException izuzetak = context.Exception as AuthorizeException; context.ModelState.AddModelError("", izuzetak.Message); context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; } else { context.ModelState.AddModelError("ERROR", "Greska na serveru"); context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; } var lista = context.ModelState.Where(x => x.Value.Errors.Count() > 0) .ToDictionary(x => x.Key, y => y.Value.Errors.Select(c => c.ErrorMessage)); context.Result = new JsonResult(lista); }
public async Task <TResult> MakeApiCall <TResult>(string url, HttpMethod method, object data = null, CancellationToken cancellationToken = default(CancellationToken)) where TResult : class { try { HttpResponseMessage response = await Call(url, method, cancellationToken, data); cancellationToken.ThrowIfCancellationRequested(); var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (response.StatusCode == System.Net.HttpStatusCode.OK) { try { var result = JsonConvert.DeserializeObject <TResult>(responseContent, _deserializationSettings); return(result); } catch (JsonException ex) { if (response != null) { response.Dispose(); } throw new SerializationException("Unable to deserialize the response.", ex); } } else { cancellationToken.ThrowIfCancellationRequested(); if (response.StatusCode == System.Net.HttpStatusCode.NoContent) { return(null); } if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { var excep = new AuthorizeException(); throw excep; } if (response.StatusCode == System.Net.HttpStatusCode.PreconditionFailed) { var excep = new ValidationException(responseContent); throw excep; } else { throw new Exception(responseContent); } } } catch (Exception ex) { throw ex; } }
public async Task <bool> MakeApiCall(string url, HttpMethod method, object data = null, CancellationToken cancellationToken = default(CancellationToken)) { try { HttpResponseMessage response = await Call(url, method, cancellationToken, data); cancellationToken.ThrowIfCancellationRequested(); if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent) { return(true); } else { cancellationToken.ThrowIfCancellationRequested(); if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { var excep = new AuthorizeException(); throw excep; } var stringSerialized = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (response.StatusCode == System.Net.HttpStatusCode.PreconditionFailed) { var excep = new ValidationException(stringSerialized); throw excep; } else { throw new Exception(stringSerialized); } } } catch (Exception ex) { throw ex; } }
public override void OnException(HttpActionExecutedContext context) { InternalException internalException = context.Exception as InternalException; if (internalException != null) { AuthorizeException authorizeException = internalException as AuthorizeException; HttpStatusCode statusCode = authorizeException != null ? HttpStatusCode.Forbidden : HttpStatusCode.InternalServerError; context.Response = new HttpResponseMessage(statusCode); ErrorResponse errResponse = ErrorCodeLocalizator.Localize(internalException); context.Response.Content = new ObjectContent( typeof(ErrorResponse), errResponse, GlobalConfiguration.Configuration.Formatters.JsonFormatter); Logger.Error(context.Exception.Message); } else { Logger.Error(context.Exception.Message, context.Exception); } }