0

I'm working on a fairly standard ASP.NET Core 8 Web API project. I added a module to globally capture exceptions, and it works fine. But now I'm trying to standardize the responses in the controllers more, and I've added a small class to help me with this:

public class ActionResponse<T>
{
    public bool WasSuccess { get; set; }

    public string? Message { get; set; }

    public T? Result { get; set; }
}

So far, nothing unusual. The point is, I'd now like to return that "enhanced message" I'm returning in the global exception in that Message property... perhaps from the same controller or somewhere else, but still in that format (WasSuccess = false, Message = "new error...")... but I don't know at what point or how I could do this.

4
  • Maybe you could Serialize to JSON and put that in a string? Watching this question to see what kinds of answers you get. Commented Apr 7 at 12:38
  • @Prof.Falken Sure, I'd put it in JSON. But the way I've done it now, the exception message comes from somewhere outside the controller. If I do it in the controller (try exception), my ActionResponse class would serve both as a valid response and as an error message. Commented Apr 7 at 12:47
  • 3
    What you try is already standardized (as in an actual IETF RFC, datatracker.ietf.org/doc/html/rfc7807) and supported in ASP.NET Core. There's even automatic ProblemDetails generation for validation errors. As the docs show, it's possible to customize the response. There are even methods for returning custom Problem results, like Results.Problem Commented Apr 7 at 12:48
  • 1
    You shouldn't create your own custom format. Many client libraries know how to handle RFC7807 responses and eg highlighting the input controls that failed. The question's design suffers from a big problem already - it says it succeeded (the status code seems to be 200) even when if failed. That's not just bad for clients. Browsers, caches and proxies will cache 2xx results. Retrying with eg Polly is a lot harder and more expensive if the response says it succeeded when it hasn't. Even your own code would have to parse the JSON payload before it can decide to retry Commented Apr 7 at 12:53

1 Answer 1

1

Based on Panagiotis Kanavos's cpmment

you could customize ProblemDetails as follow:

builder.Services.AddProblemDetails(x =>
{
    x.CustomizeProblemDetails = ctx =>
    {
        var exception = ctx.HttpContext.Features.Get<IExceptionHandlerPathFeature>()?.Error;
        
        if(exception != null&& exception is ArgumentNullException)
        {
            ctx.ProblemDetails.Title = "Null exception";
            ctx.ProblemDetails.Detail = exception.Message;
            ctx.ProblemDetails.Status = 409;
          
        }
        .....
    };
});

var app = builder.Build();


app.UseExceptionHandler();
Sign up to request clarification or add additional context in comments.

Comments

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.