In error situations, I wanted to return a specific error details class, in whatever format the client requested instead of the happy path object.
I want to have my controller methods return the domain specific happy path object and to throw an exception otherwise.
The problem I had was that the HttpResponseException constructors do not allow domain objects.
This is what I eventually came up with
public ProviderCollection GetProviders(string providerName)
{
try
{
return _providerPresenter.GetProviders(providerName);
}
catch (BadInputValidationException badInputValidationException)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest,
badInputValidationException.Result));
}
}
Result
is a class that contains error details, while ProviderCollection
is my happy path result.