3

I am using Web API 2.0 to create my own project. My Api contain ability to Add , Get and book product.

I Want to handle exception but there is some issue I little bit confused.

I have a controller: ProdctController with action method AddProdct.

   [Route("~/CKService.svc/author/add")]
    [HttpPost]
    public IHttpActionResult AddProduct(ProductRequest request)
    {
        ProductManager.AddProduct(request.ProductId, request.ProductName, 
        request.Price);

        return Ok(new AddProductResponse()
        {
            Status = AddProductStatus.Success
        };)
    }

Now, if the product already exists, so the data access layer will throw DuplicateKeyException

I want to handle this excpetion and return response like this:

  return Ok(new AddProductResponse()
    {
        Status = AddProductStatus.AlreadyExists
    };)
  1. Is this reasonable to return HTTP status code 200?
  2. How can I do this without add catch DuplicateKeyException to my controller because i think this is not the right way.

Thanks

9
  • 3
    If it is an error you should never return HTTP 200. Commented May 12, 2017 at 10:36
  • 2
    And there is a lot on exception handling in ASP.NET Web API. Commented May 12, 2017 at 10:37
  • 409 Conflict - would be a resonable HTTP Code reference Commented May 12, 2017 at 10:46
  • 1
    you should return a HTTP status which reflects the error, not an arbitrary code of your own defining within a "success" method. Make use of the existing, well-defined standard codes that exist. Then your response has intrinsic semantic meaning which any generic API client can work with. Returning "OK" when there's a problem is effectively misleading the client application, even if you document it, and not how RESTful APIs are intended to work. Commented May 12, 2017 at 10:52
  • @ADyson thank you , I still confused, for example if I have book_seat action method but airplane is already full in this case what is the right status code to be returned? Commented May 12, 2017 at 11:16

2 Answers 2

1

Here is one suggestion. (and I emphasis that it is just one of the many options you have available)

Refactor the manager to handle and return the status of adding the product and then have the action return the appropriate response code based on the status.

[Route("~/CKService.svc/author/add")]
[HttpPost]
public IHttpActionResult AddProduct(ProductRequest request) {
    var status = ProductManager.AddProduct(request.ProductId, request.ProductName, request.Price);
    var response = new AddProductResponse() {
        Status = status
    };
    if(status == AddProductStatus.Success) {
        return Ok(response);
    }
    return BadRequest(response);
}

Avoid return 200 OK for requests that have not completed as intended. That will mislead the client making the request.

Sign up to request clarification or add additional context in comments.

1 Comment

can you explain why to avoid use HTTP 200? look at this: stackoverflow.com/questions/27921537/…
1

HTTP 200 is not resonable - it indicates a successfull request. Use a 4xx (Error) Code instead. 409 (conflict) fits your situation of an already existing object.

See: HTTP response code when resource creation POST fails due to existing matching resource

To catch the exception is totally fine. An unhandled exception would result in a 500 error (internal server error) which is not meaningful by any means. Instead you should catch the Exception and return a HTTP Error like this (Exception Handling ASP.NET) :

 throw new HttpResponseException(
        Request.CreateErrorResponse(HttpStatusCode.Conflict, message))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.