We're using ServiceStack to build a web API. I have a situation where I want to be able to return a response of 400 (BadRequest) from an API endpoint with some additional data indicating the specific cause(s) of the error.
The two ways I know about to return a specific status code are:
- Throw an exception.
- Return an instance of
HttpResult
.
I'd rather not return an instance of HttpResult
. It would require me to make the method return type more generic (currently the return type is the specific response DTO), and there are other reasons (having to do with our use of ServiceStack) why that is problematic.
The situation with throwing an exception is weird. Based on the form of the response, ServiceStack is using the custom DTO even in the exception case (and the docs confirm this). And the HttpError
exception class has a Response
property which corresponds to the DTO used to issue the response, but setting the Response
property to an instance of the custom DTO does not have the intended effect. It only affects the value of the ResponseStatus
property on the response DTO. The other properties on the DTO are uninitialized as though ServiceStack generated a fresh instance of of the response DTO and used that instead (but set its ResponseStatus
property from the provided DTO.
Is everything I've said actually correct? Is there another option for forming the response that doesn't have the drawbacks I've mentioned above?