Throwing an exception always gives 500 Internal Server Error

4,109 views
Skip to first unread message

JP

unread,
Nov 24, 2011, 10:39:12 AM11/24/11
to ServiceStack .NET Open Source REST Web Services Framework
Hi,

Suppose I want to raise an Exception in, say, an OnPost in my DTO,
because the data supplied by the client was invalid.

Right now, this always produces a 500 Internal Server Error.
Preferably I would have more control over the HTTP status code. I did
notice that ServiceBase<TRequest>.HandleException should generate a
HttpStatusCode.BadRequest (400) when the exception is an
ArgumentException, but I'm still getting 500 when I throw an
ArgumentException.

Does anyone have an example of how to throw an exception, resulting in
specific HTTP status codes? Thanks.

Jon Canning

unread,
Nov 24, 2011, 12:29:41 PM11/24/11
to servic...@googlegroups.com
You could catch then return an HttpResult and set the StatusCode?

Demis Bellot

unread,
Nov 24, 2011, 12:56:00 PM11/24/11
to servic...@googlegroups.com
Yes catch the exception and return a HttpResult to customize the response or alternatively throw an ArgumentException (or something inheriting it) which automatically send a 400 Bad Request, see:

Cheers

JP

unread,
Nov 24, 2011, 1:00:17 PM11/24/11
to ServiceStack .NET Open Source REST Web Services Framework
Hi Jon,

Good point, and that does work - partially. That approach does allow
me to set the HttpStatusCode to the required value, but the body
content that I'd like to return (a short error description which could
simply be "Bad Request" or a more functional error description,
depending on the situation) is still rendered by ServiceStack as HTML
containing "Snapshot of MyObject generated by ServiceStack..." etc.,
even if I was requesting json or xml data. While that does get the
message across, I have the feeling that it's not the cleanest way of
doing it.

Any ideas? Cheers.


On Nov 24, 6:29 pm, Jon Canning <jon.cann...@gmail.com> wrote:
> You could catch then return an HttpResult and set the StatusCode?
>

> On 24 November 2011 15:39, JP <jpvandere...@gmail.com> wrote:
>
>
>
> > Hi,
>
> > Suppose I want to raise an Exception in, say, an OnPost in my DTO,
> > because the data supplied by the client was invalid.
>
> > Right now, this always produces a 500 Internal Server Error.
> > Preferably I would have more control over the HTTP status code. I did
> > notice that ServiceBase<TRequest>.HandleException should generate a
> > HttpStatusCode.BadRequest (400) when the exception is an
> > ArgumentException, but I'm still getting 500 when I throw an
> > ArgumentException.
>
> > Does anyone have an example of how to throw an exception, resulting in

> > specific HTTP status codes? Thanks.- Hide quoted text -
>
> - Show quoted text -

Demis Bellot

unread,
Nov 24, 2011, 1:03:45 PM11/24/11
to servic...@googlegroups.com
"Snapshot of MyObject generated by ServiceStack..."

Is the HTML View / aka requested Response Type, ask for your preferred ContentType with Accept: json/xml/etc or ?format=[json|xml|etc]

Cheers,

JP

unread,
Nov 24, 2011, 1:19:34 PM11/24/11
to ServiceStack .NET Open Source REST Web Services Framework
Hi guys, thanks for responding so quickly.

> ask for your preferred ContentType with Accept: json/xml/etc or ?format=[json|xml|etc]

Well, it seems it doesn't quite work that transparently. There are two
constructors for HttpResult that allow a HttpStatusCode:

a. statusCode, statusDescription: this one doesn't return the
specified description in the result
b. response, contentType, statusCode: this one does work, but then I
need to figure out the contentType myself.


> alternatively throw an ArgumentException which automatically send a 400 Bad Request
As I mentioned in my original post, I'm getting a 500 when I throw an
ArgumentException. The "ErrorCode" member in the response then has a
value "ArgumentException".

Just to be clear: I'm throwing the Exception (or returning the
HttpResult) in:

public override object OnGet(MyObject request)
{
// throw new ArgumentException("My exception message"); //
Returns a 500

// return new HttpResult(System.Net.HttpStatusCode.BadRequest,
"My exception message"); // My custom message gets lost

// return new HttpResult("My exception message", "application/
json", System.Net.HttpStatusCode.BadRequest); // Creates dependency on
requested contenttype

}

> http://twitter.com/demisbellothttp://www.servicestack.net/mythz_blog- Hide quoted text -

Demis Bellot

unread,
Nov 24, 2011, 2:15:23 PM11/24/11
to servic...@googlegroups.com
You can get the requested Content type with:

var httpReq = base.RequextContext.Get<IHttpRequest>();
var ct = httpReq.ResponseContentType;

But it should work if you don't set a ContentType (i.e. null), otherwise it's a bug. Let me know if it doesn't work as expected.

Cheers,

JP

unread,
Nov 24, 2011, 3:45:12 PM11/24/11
to ServiceStack .NET Open Source REST Web Services Framework
Hi Demis,

Setting the ContentType explicitly to null works, thanks!

-JP

> > >http://twitter.com/demisbellothttp://www.servicestack.net/mythz_blog-...quoted text -

Reply all
Reply to author
Forward
0 new messages