What's the best way to return exceptions to the calling client?

Topics: Web Api
Oct 3, 2011 at 9:23 AM

Regardless of what exceptions I throw, the calling client only receives a System.Net.WebException with no inner exception and no information beyond "The remote server returned an error: (500) Internal Server Error."

Is there a better way of returning exceptions so that I can know what actually occurred?

My current resources do something along these lines:

[WebInvoke(UriTemplate = "", Method = "POST")]
public HttpResponseMessage<Models.Payload> Post(Models.Payload newPayload)
{
  try
  {
    var data = AutoMapper.Mapper.Map<Models.Payload, Data.Payload>(newPayload);
    var createdPayload = _payloadService.AddPayload(data);
    var retVal = AutoMapper.Mapper.Map<Data.Payload, Models.Payload>(createdPayload);
    var response = new HttpResponseMessage<Models.Payload>(retVal, HttpStatusCode.OK);
    return response;
  }
  catch (Exception ex)
  {
    throw new WebException(ex.Message,ex);
  }
}

Cheers,
Nick

 

PS If anyone is having trouble with the Insert Code Snippet window here, the NoScript plugin for Firefox is cleansing the request back to www.codeplex.com as an XSS attack  :\  Add ^http://www.codeplex.com to your XSS white list and it works ok.

Coordinator
Oct 7, 2011 at 6:14 AM

Set IncludeExceptionDetail to true on your HttpConfiguration instance and see if that gives you the information you are looking for on the client.

Daniel Roth

From: GoldenHornet [email removed]
Sent: Monday, October 03, 2011 2:24 AM
To: Daniel Roth
Subject: What's the best way to return exceptions to the calling client? [wcf:274575]

From: GoldenHornet

Regardless of what exceptions I throw, the calling client only receives a System.Net.WebException with no inner exception and no information beyond "The remote server returned an error: (500) Internal Server Error."

Is there a better way of returning exceptions so that I can know what actually occurred?

My current resources do something along these lines:

[WebInvoke(UriTemplate = "", Method = "POST")]
public HttpResponseMessage<Models.Payload> Post(Models.Payload newPayload)
{
  try
  {
    var data = AutoMapper.Mapper.Map<Models.Payload, Data.Payload>(newPayload);
    var createdPayload = _payloadService.AddPayload(data);
    var retVal = AutoMapper.Mapper.Map<Data.Payload, Models.Payload>(createdPayload);
    var response = new HttpResponseMessage<Models.Payload>(retVal, HttpStatusCode.OK);
    return response;
  }
  catch (Exception ex)
  {
    throw new WebException(ex.Message,ex);
  }
}

Cheers,
Nick

PS If anyone is having trouble with the Insert Code Snippet window here, the NoScript plugin for Firefox is cleansing the request back to www.codeplex.com as an XSS attack :\ Add ^http://www.codeplex.com to your XSS white list and it works ok.