2
votes

We're working on a project using ServiceStack (loving it) but need some help with a strange problem. In our project we throw or return various types of HttpError with ErrorResponse and ResponseStatus objects using a helper method.

It follows the pattern described here. Something like this:

protected HttpError BusinessError(string summaryMessage = "Some generic validation summary.")
    {           
        return new HttpError(HttpStatusCode.BadRequest, "MyValidationType", summaryMessage)
        {                           
            Response = new ErrorResponse
            {
                ResponseStatus = new ResponseStatus
                {
                    ErrorCode = "MyValidationType",
                    Message = summaryMessage,
                    Errors = new List<ResponseError>()
                },
            }
        };
    }

In a service call we would use it like so:

throw BusinessError("Help I've fallen and can't get up!");

This would work a treat, and we'd feed it in to the ss-validation JS library to render our validation messages. Worked fine.

The problem is that now ServiceStack won't serialize any of the HttpError's details into the response. All we get is a response with the 400 status code, the 'MyValidationType' error code, and empty JSON response {}.

I've tried playing with combinations of throwing/returning the error and switching the service method's return type to object etc, but nothing seems to make a difference. To be honest I'm not sure what we could have changed in our project to cause this behavior.

I'd appreciate any advice or pointers as to what could cause this?

1

1 Answers

5
votes

This can happen if you're using a {RequestName} and {RequestName}Response naming convention and the Response DTO does not have a ResponseStatus property:

The {RequestDto}Response is returned, regardless of the service method's response type. If the {RequestDto}Response DTO has a ResponseStatus property, it is populated otherwise no ResponseStatus will be returned. (If you have decorated the {ResponseDto}Response class and properties with [DataContract]/[DataMember] attributes, then ResponseStatus also needs to be decorated, to get populated).

In which case the solution is just to add a ResponseStatus property to the Response DTO:

public class RequestNameResponse
{
    public ResponseStatus ResponseStatus { get; set; }
}