3
votes

Information of what I have and what I am trying to achieve

I am using ServiceStack and have it up and running for what I need, however I am unable to find out how to disable the Body/Content for uncaught exceptions.

I have ServiceStack handling ALL routes.

If I navigate to a route which is not mapped to ServiceStack, I get a StatusCode of 404 (perfect) and content in the Body of the response of "Handler for Request not found: ...."

If my code throws an uncaught exception, ServiceStack will kindly return a relevant StatusCode, however it also returns a ResponseStatus with ErrorCode and Message populated.

I have DebugMode turned off, this disables the StackTrace, however I want to completely mute the entire Body of the response for exceptions.

What I have tried

I have tried a Response Filter of the following:

ResponseFilters.Add((req, res, dto)) =>
{
    if (dto is Exception) res.Close();
});

it unfortunately did not work.

What I want to avoid

try{
    return service.GetResponse();
} catch (Exception) {
    return new HttpResult(.....);
}

My Question

How do I disable the response body for all uncaught exceptions, but still return the StatusCode? I would like null returned in the body, and StatusCodes to remain in tact.

I've tried to make my question clear, but if I have been a bit vague in any way, please ask me questions.

1

1 Answers

1
votes

You should be able to override the default exception handling behavior by overriding HandleException of ServiceBase.

You can inspect the default implementation here: https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.ServiceInterface/ServiceBase.cs#L228.