Niko Yakimov
Niko Yakimov

Reputation: 83

ServiceStack doesn't populate the response DTO when throwing HttpErrors

ServiceStack doesn't populate the original response in the WebServiceException's responseDTO property.

I'm running the code below which should always return a 404 response code with the ResponseStatus property of the TestResponse populated with "Some bad request" but it also seems like should return the original good response with it's output property populated from the request's input property. However I get null when I look at the WebServiceException responseDTO

        public TestResponse Post(Test request)
        {
            var response = new TestResponse() { Output = request.Input };

            throw new HttpError(response, (int)HttpStatusCode.BadRequest, "Some bad request");
        }

        public TestResponse Get(Test request)
        {
            try
            {
                using (var client = new JsonServiceClient("http://localhost:5000"))
                {
                    var response =  client.Post(request);
                    return response;
                }
            }
            catch (WebServiceException ex)
            {

                throw;
            }
        }

In general I was expecting that the responseDTO property in the WebServiceException will contain the endpoint's DTO as long as it's passed in when throwing the HttpError but that doesn't seem to be the case. I see only default values and nulls for each property in the responseDTO.

Upvotes: 1

Views: 141

Answers (1)

mythz
mythz

Reputation: 143284

When an Exception is thrown only the ResponseStatus is preserved, you can add any additional info to its Meta dictionary.

Alternatively you can return a failed HTTP Response:

public TestResponse Post(Test request)
{
    var response = new TestResponse() { Output = request.Input };

    base.Response.StatusCode = (int)HttpStatusCode.BadRequest;
    return response;
}

Upvotes: 2

Related Questions