Kebin Maharjan - 191 - Jan 13, 2015

Hi there,
I’m throwing an exception as below:
throw HttpError.NotFound(“document with given id doesn’t exist.”);

However, this shows up in elmah log as Type: HttpError, Code: 0. Should this not be set to 404? Am I missing something here?


The type name of the Exception is HttpError, but there is no Code property on Exception or HttpError types.

The HttpError.StatusCode is what’s set to 404 which you can see in the HTTP Response Status code.

Kebin Maharjan:

I see. If I were to throw HttpError exception from mvc controller, whats your recommendation on how that is logged in iis as http 404. I’m a bit confused on this.

MVC has their own way of returning HTTP 404 responses:

Kebin Maharjan:

Hmm. So I’d need to catch HttpError & throw mvc http exception?

Depends what you want to to do, but propagating a ServiceStack HttpError outside an MVC Controller isn’t going to have the desired effect.

You should be able to handle it generically with a custom MVC exception filter that basically detects a ServiceStack IHttpError and sets the Response Status Code to the response:

Otherwise It might just be easier the Response Status in ServiceStack Service:

base.Response.StatusCode = 404;
base.Response.StatusDescription = “document with given id doesn’t exist.”;

Since it shares the same ASP.NET Request pipeline, anything you write to the response in ServiceStack should also apply to the MVC HTTP Response.

Kebin Maharjan:

Awesome. Thank you for pointing to the right direction!