1

I am starting to work on ASP.NET core error handling, and I noticed that I am getting an error in a format

{
    "errors": {},
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|caeb2316-47fb93bd8a17bc0a."
}

I am trying to get a more detailed error message, but I cannot get rid of this message.

I just followed instruction on Handle errors in ASP.NET Core, but error is always there.

How can I get rid of this format of error message?

4
  • 1
    This response is the standardized ProblemDetails response for APIs. It is a good thing that you retrieve the error in this format. What more information would you want the response to contain? Usually, if you get “One or more validation errors occurred“, then the errors property should contain all the individual errors. Commented Apr 26, 2020 at 10:07
  • I was expecting a raw system error message. How can I remove this format in development stage and try to find a problem that is throwing this error message Commented Apr 26, 2020 at 10:10
  • “find a problem that is throwing this error message” – This is a model validation problem. All the information is there in the response. And since this ProblemDetails object is only used for API controllers by default, you wouldn’t want this to cause a HTML-based response since your API clients likely cannot work with that. Commented Apr 26, 2020 at 10:16
  • Because this error message is not providing any error information, I am surprised you even ask this question. Validaton error without a detailed futher explanation or innerException is pretty vague Commented Apr 26, 2020 at 10:16

1 Answer 1

2

Using the below code you can create custom error response or custom problem details:

services.AddControllers()
 .ConfigureApiBehaviorOptions(o =>
 {
     o.InvalidModelStateResponseFactory = context =>
     {
         var problemsDetailsFactory = context.HttpContext.RequestServices
             .GetRequiredService<ProblemDetailsFactory>();
         var problemDetails = problemsDetailsFactory.CreateValidationProblemDetails(
             context.HttpContext, 
             context.ModelState);
         problemDetails.Detail = "Custom Details";
         problemDetails.Instance = context.HttpContext.Request.Path;
         problemDetails.Type = "https://tools.etf............";
         //problemDetails.Status = StatusCodes.Status422UnprocessableEntity;
         problemDetails.Status = StatusCodes.Status400BadRequest;
         problemDetails.Title = "One or more errors on input occured";
         return new BadRequestObjectResult(problemDetails)
         {
             //ContentTypes= {"application/custom+json"}
         };
     };
 });
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Deepak. What is Implementation of problemsDetailFactory?
@silversk8terz, you can ask your specific questions as a seperate question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.