1

I am trying to understand how to get to the reject part of the .then() in angular.

C# Method

public int Foo(){
 int foo = 1;
 return foo;
}

I have tried throw HttpResponseException with 404 and 500, however even that response is caught and handled in the first part of the .then() promise. I just want to understand what sort of error needs to occur for it to go into the latter part.

I have used this as source to throw the response exception

Throw HttpResponseException or return Request.CreateErrorResponse?

Example angular method

foo.then(function(response){
  //success do something
}), function(error){
   // how to come here?
})

If I haven't explained well please do let me know, I am simply after getting to the reject part of the function.

8
  • 1
    Hey Harry, a 404 or 500 response is seen as a successful call. Because you asked and you received information. The error phase is triggered when the http call isn't successful. E.a. a timeout. You could test this by setting a very short timeout on the call, or just make a call to a wrong path or server. Commented Apr 18, 2016 at 13:38
  • Do you have any HTTP response interceptor? Commented Apr 18, 2016 at 13:38
  • @stevenca oh i see that makes sense thanks Commented Apr 18, 2016 at 13:41
  • 1
    I would do some research into how to handle exceptions in promises. Check out stackoverflow.com/q/36345046/215552 and stackoverflow.com/q/26534303/215552 Commented Apr 18, 2016 at 13:53
  • 1
    sure you had the right answer b4 yer man posted the example stackoverflow.com/questions/37286629/… thought you deserved an upvote! Commented May 17, 2016 at 21:47

2 Answers 2

0

Instead of returning int from your C# method, you should return IHttpActionResult.

[HttpGet]
public IHttpActionResult Foo()
{
    try 
    {
        int foo = 1;
        return Ok(foo);
    }
    catch(Exception exception)
    {
        // log exception or whatever you need to do
        return InternalServerError(exception);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks but wouldnt this still be caught in the success part of the .then()
The Ok() response will trigger your success callback, but the InternalServerError() response will trigger the error callback.
0

I think you messed up with one of your brackets. The error handler for a promise must be included INSIDE the ".then" method.

replace this:

foo.then(function(response){
  //success do something
}), function(error){
   // how to come here?
})

with

foo.then(function(response){
  //success do something
}, function(error){
   // how to come here?
});

Hope that helps.

1 Comment

Hi, thanks but the code is for demo purpose and this does not answer my question.

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.