-
Notifications
You must be signed in to change notification settings - Fork 309
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
Question: Best way to map failure results to ActionResults? #484
Comments
All these protected new IActionResult Ok()
{
return base.Ok(Envelope.Ok());
}
protected IActionResult Ok<T>(T result)
{
return base.Ok(Envelope.Ok(result));
}
protected IActionResult Error(Error error)
{
_logger.LogInformation($"Error: {error}");
return BadRequest(Envelope.Error(error));
}
protected IActionResult FromResult(Result result)
{
return result.IsSuccess ? Ok() : Error(result.Error);
}
public sealed class Envelope
{
public object Result { get; }
public string ErrorMessage { get; }
public string ErrorCode { get; }
public DateTime TimeGenerated { get; }
private Envelope(object result, Error error)
{
Result = result;
ErrorMessage = error?.Message;
ErrorCode = error?.Code;
TimeGenerated = DateTime.Now;
}
public static Envelope Ok(object result = null)
{
return new Envelope(result, null);
}
public static Envelope Error(Error error)
{
return new Envelope(null, error);
}
} You can also use the base |
Thanks for your example. That's why I think the mapping is not that easy, as in your reply. |
For MVC (not just API) responses, there could be additional properties needed, indeed. You can keep helper methods like |
Hello, I was asking myself how I can improve my error handling in a web api with CSharpFunctionalExtensions. Since there is the possibility to use an error object in failure results instead of a simple string, I think there are several opportunities for improvement available. For example, you can provide additional info like the HTTP status code beside the error message based on the context.
Currently, I'm using a custom error object for failure results which contains a HTTP status code, error title, error messages and a trace id.
Result<T,HttpError>
Then in my controller I call an extension method
.Envelope()
on the result, which maps it to aObjectResult
respectivelyActionResult
. When the result is a success the status code is 200 by default but can be adjusted by passing the code to the.Envelope()
method.This solution works fine in the beginning, but it soon gets to its limits, especially when your action method on the controller can return different types. Then you may need a method which returns a non-generic ActionResult like this:
Same problem when you want to return a file:
Or you want to return a CreatedAtActionResult to properly populate the location header when creating a resource:
So you have to implement many different methods for each use case.
Does anybody have a better idea on how to map failure results to ActionResults?
The text was updated successfully, but these errors were encountered: