3

I have below exception contract.

public class AdminBusinessException : Exception
{
    /// <summary>
    /// Gets or Sets Exception Code.
    /// </summary>
    public string ExceptionCode { get; set; }

    /// <summary>
    /// Gets or Sets Description.
    /// </summary>
    public string Description { get; set; }

    /// <summary>
    /// Initiates here.
    /// </summary>
    /// <param name="exceptionCode"></param>
    /// <param name="description"></param>
    public AdminBusinessException(string exceptionCode, string   description)
    {
        this.ExceptionCode = exceptionCode;
        this.Description = description;
    }
 }

So i want to throw exception code as well as description to my Http Client. Any one please help me on this.

I am strucking on this :( :(

Power Star
  • 1,724
  • 2
  • 13
  • 25
  • Possible duplicate of [Web API throw custom exception back to client](http://stackoverflow.com/questions/26718868/web-api-throw-custom-exception-back-to-client) – Wim Ombelets Sep 20 '16 at 06:02

1 Answers1

0

Your Web APIs should handle the response using the HTTP conventions and you can include a message in the body.

ex:

Public IHttpActionResult DoSomethingBusiness(int id)
{
    if(some error from client)
      return BadRequest("some error message here")

     return Ok("Everything seems good")
}
Haitham Shaddad
  • 4,336
  • 2
  • 14
  • 19
  • Is there any way to throw exception object to client? – Power Star Sep 23 '16 at 07:33
  • An exception object means a server error, in this case you have to return a status code like 500, and include an error message that inidcates what happened. If the error was due to an issue in the client data, then you have to return (Bad Request) and also include the exception details in the message body, something like this; return BadRequest(exception.ToString()) – Haitham Shaddad Sep 23 '16 at 11:01