protected override void HandleUnauthorizedRequest(HttpActionContext actionContext) { var apiError = new ApiErro { Mensagem = GeneralStrings.AuthorizationDenied }; var apiErrorResponse = new ApiErroResposta { Erros = new List <ApiErro> { apiError } }; var httpResponseMessage = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(apiErrorResponse)), StatusCode = HttpStatusCode.Unauthorized }; httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); actionContext.Response = httpResponseMessage; var logWarnMessage = string.Format("{0}: {1}", apiError.Mensagem, actionContext.Request.ToString()); LogHelper.Warn(logWarnMessage); }
public override void OnException(HttpActionExecutedContext context) { var httpResponse = new HttpResponseMessage(); var apiError = new ApiErro(); if (context.Exception is HttpException httpException) { httpResponse.StatusCode = (HttpStatusCode)httpException.GetHttpCode(); apiError.Mensagem = httpException.Message; } else { httpResponse.StatusCode = HttpStatusCode.InternalServerError; apiError.Mensagem = GeneralStrings.InternalServerError; } var apiErrorResponse = new ApiErroResposta { Erros = new List <ApiErro> { apiError } }; httpResponse.Content = new StringContent(JsonConvert.SerializeObject(apiErrorResponse)); httpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); context.Response = httpResponse; if (IsServerError(httpResponse.StatusCode.GetHashCode())) { LogHelper.Error(context.Exception); } }
private static ApiErro BuildError(string message) { var apiError = new ApiErro { Mensagem = message }; return(apiError); }
public static ApiErroResposta BuildResponse(ModelStateDictionary modelState) { var apiErrorResponse = new ApiErroResposta(); foreach (ModelState value in modelState.Values) { foreach (ModelError error in value.Errors) { var apiError = new ApiErro { Mensagem = error.ErrorMessage.NullOrEmpty() ? string.Format("Um campo obrigatório não foi informado ou não está no padrão correto, favor verificar o modelo. A exceção gerada foi: {0}.", error.Exception) : error.ErrorMessage }; apiErrorResponse.Erros.Add(apiError); } } return(apiErrorResponse); }