Beispiel #1
0
        private static Task HandleValidationExceptionAsync(HttpContext context, ValidationApiException exception)
        {
            var result = JsonConvert.SerializeObject(exception.Errors);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)HttpStatusCode.UnprocessableEntity;
            return(context.Response.WriteAsync(result));
        }
        public async Task ErrorHandlingMiddleware_ReturnsError400_WithValidationApiException()
        {
            var apiErrorCode         = _faker.PickRandom <ApiErrorCode>();
            var expectedErrorMessage = _faker.Lorem.Sentence();
            var exception            = new ValidationApiException(apiErrorCode, expectedErrorMessage);

            await VerifyErrorHandling(exception, expectedErrorMessage, HttpStatusCode.BadRequest, apiErrorCode);
        }
Beispiel #3
0
        private static Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            context.Response.ContentType = "application/json";

            if (context.Request.Headers.ContainsKey("x-requester") && context.Request.Headers["x-requester"].ToString().Equals("Datatable"))
            {
                context.Response.StatusCode = StatusCodes.Status200OK;
                ApiException ex    = exception as ApiException;
                var          error = JsonConvert.SerializeObject(new GridDataViewModel <object>
                {
                    draw            = 1,
                    data            = null,
                    recordsFiltered = 0,
                    recordsTotal    = 0,
                    error           = new ErrorViewModel()
                    {
                        ResponseText = ex.Content,
                        StatusCode   = StatusCodes.Status500InternalServerError,
                        Message      = exception.Message
                    }
                });

                return(context.Response.WriteAsync(error));
            }
            else
            {
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                ValidationApiException ex = exception as ValidationApiException;
                return(context.Response.WriteAsync(new ErrorViewModel()
                {
                    ResponseText = ex != null ? JsonConvert.SerializeObject(ex.Content) : exception.Message,
                    StatusCode = context.Response.StatusCode,
                    Message = exception.Message
                }.ToString()));
            }
        }
 /// <summary>
 /// Get exception description including all the details about request and error, as text
 /// </summary>
 /// <param name="exception"></param>
 /// <returns></returns>
 public static string GetDescription(this ValidationApiException exception)
 {
     return($"Couldn't execute http request. {exception.GetProblemDetailsPhrase()} {exception.GetRequestPhrase()}");
 }
 /// <summary>
 /// Get http request caused the exception problem details as text
 /// </summary>
 /// <param name="exception"></param>
 /// <returns></returns>
 public static string GetProblemDetailsPhrase(this ValidationApiException exception)
 {
     return(exception.Content != null ? $"Problem details: {exception.Content.ToJson()}." : string.Empty);
 }