Skip to content

[Feature request] Use custom mappers inside ProblemDetailsResultFilter #203

Description

@alex-zyl

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);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions