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.
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 -
"Snapshot of MyObject generated by ServiceStack..."
> 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 -
var httpReq = base.RequextContext.Get<IHttpRequest>();
var ct = httpReq.ResponseContentType;
Setting the ContentType explicitly to null works, thanks!
-JP
> > >http://twitter.com/demisbellothttp://www.servicestack.net/mythz_blog-...quoted text -