2
public IHttpActionResult Save(item)
{
    try
    {
        result = MyRepository.Save(item);
        return Ok(result);
    }
    catch
    {
        // What should I do here?
        // I wish to return an error response how can i do that? 
    }
}

If it has no exception,I can return Ok.

But if there is an exception what should I do?

Note that I have javascript client

2

4 Answers 4

1

In Web API 2, you can use BadRequest to return an error message.

public IHttpActionResult Save(item)
{
    try
    {
        result = MyRepository.Save(item);
        return Ok(result);
    }
    catch
    {
        return BadRequest("Error message");
    }
}

FYI: Do not use try catch block just to swallow an exception. I personally do not like using try catch block inside Action method. Instead, I let Web API 2 Global Exception Handler handles exceptions. It is the out of the scope of your original question.

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

Comments

1

Create a model to hold what error information you want to return and pass that.

public IHttpActionResult Save(item) {
    try {
        result = MyRepository.Save(item);
        return Ok(result);
    } catch {
        // What should I do here? Create a model to hold the data you want to return
        var myErrorModel = new {
            code = "My custom error code or 500 server error code",
            message = "some friendly error message"
        };

        // I wish to return an error response how can i do that?
        var response = Request.CreateResponse(HttpStatusCode.InternalServerError, myErrorModel);
        return ResponseMessage(response);
    }
}

In your javascript client in the error handler you can then access the model properties and handle the error as you see fit.

var handleResponse = function (data) {
    var code = data.code;
    var message = data.message
};

UPDATE:

Agreeing with @Win, I personally don't like doing this in the controller actions as it is a Cross-cutting concern and have basically moved everything from within the catch block into a global error handler.

Comments

0

You can use HttpResponseException to send an error message.

public IHttpActionResult Save(item)
{
    try
    {
        result = MyRepository.Save(item);
        return Ok(result);
    }
    catch
    {
       var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
        {
            Content = new StringContent(string.Format("Error Message!!"),
            ReasonPhrase = "Error Reason phrase"
        };
        throw new HttpResponseException(resp);
 
    }
}

Comments

0

Use HttpResponseException as below:

throw new HttpResponseException(HttpStatusCode.InternalServerError)

Or another way is:

var response = new HttpResponseMessage(HttpStatusCode.NotFound)  
    {  
        Content = new StringContent("some error"),  
            ReasonPhrase = "Not Found"  
    };  

    throw new HttpResponseException(response);

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.