public override void OnActionExecuting(HttpActionContext actionContext)
 {
     if (!actionContext.ModelState.IsValid)
     {
         var errors = actionContext.ModelState
                      .Where(e => e.Value.Errors.Count > 0)
                      .Select(e => new ValidationErrorDetails()
         {
             PropertyName = e.Key,
             ErrorMessage =
                 (e.Value.Errors.First().Exception != null) &&
                 String.IsNullOrEmpty(e.Value.Errors.First().ErrorMessage)
                         ? e.Value.Errors.First().Exception.Message
                         : e.Value.Errors.First().ErrorMessage
         }).ToArray();
         var response     = new HttpResponseMessage(HttpStatusCode.BadRequest);
         var responseData =
             new DtoModelOutgoing(
                 new DtoModelIncomingValidationException("Validation failed", "Validation failed"),
                 _enableVerboseMode ? errors : null);
         response.Content = new ObjectContent <DtoModelOutgoing>(responseData, new JsonMediaTypeFormatter());
         // TODO: Add logger!
         actionContext.Response = response;
     }
 }
Beispiel #2
0
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            Exception        exception    = actionExecutedContext.Exception;
            var              response     = new HttpResponseMessage(HttpStatusCode.BadRequest);
            DtoModelOutgoing responseData = null;

            responseData = new DtoModelOutgoing(new ManagedException(ErrorCodes.InternalServerError, "Internal Server error"), _enableVerboseMode ? new ValidationErrorDetails()
            {
                ErrorMessage = exception.Message
            } : null);
            response.Content = new ObjectContent <DtoModelOutgoing>(responseData, new JsonMediaTypeFormatter());
            //general HTTP 500 response
            _logger.Error("Unhandled exception occurs.", exception);
            actionExecutedContext.Response = response;
        }