Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Use custom mappers inside ProblemDetailsResultFilter #203

Open
alex-zyl opened this issue Dec 30, 2023 · 0 comments
Open

Comments

@alex-zyl
Copy link

Hi @khellang
Thank you for your work. I really enjoy using this library.

There's one scenario I'd really love if the library could support: is to allow defining custom action result mappers that will be executed from ProblemDetailsResultFilter to turn object result into ProblemDetails during OnResultExecuting execution.

What I'm currently trying to achieve is to return my own Error types with problem action results (as I want to avoid using exception flow where possible) and turn them into ProblemDetails when action result is executed, e.g.

...
return BadRequest(new BusinessError(ErrorCodes.InsufficientFunds, "some_message);

To achieve that I have to

  1. Define custom result filter to transform value into ProblemDetails
if (result.Value is BusinessError error)
{
    var problem = factory.CreateProblemDetails(context.HttpContext, result.StatusCode, detail: error.Message);
    problem.Extensions.Add("errorCode", error.ErrorCode.ToString());
    context.Result = new ObjectResult(p)
    {
        StatusCode = p.Status,
        ContentTypes = result.ContentTypes
    };
}
  1. Replicate traceId population logic as I cannot call CallBeforeWriteHook as it currently has internal access

To make the library support this scenario:

  1. Extend ProblemDetailsOptions to register Result mappers
options.MapResult<BusinessError>((context, result) =>
{
// logic goes here
})
  1. Make ProblemDetailsResultFilter to go through registered mappers and execute it if found just like it is currently done for exception mapping, e.g.
if (Options.TryMapResult(context, result.Value, out var details))
{
    context.Result = CreateResult(context, details);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant