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.

Community
  • 1
  • 1
Harry
  • 3,031
  • 7
  • 42
  • 67
  • 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. – therebelcoder Apr 18 '16 at 13:38
  • Do you have any HTTP response interceptor? – Hamid Pourjam Apr 18 '16 at 13:38
  • @stevenca oh i see that makes sense thanks – Harry Apr 18 '16 at 13:41
  • @dotctor no I do not have them – Harry Apr 18 '16 at 13:42
  • Well, not sure if it's just a transcription error, but you have an end parenthesis before your error function... Also, you can look at using the `.catch()` handler instead of the overload on `.then()`. – Heretic Monkey Apr 18 '16 at 13:46
  • @MikeMcCaughan sorry if there errors are there, it is just for demonstration. if i implement .catch() you are suggesting this will capture the error? but what kind of error do i need to generate? – Harry Apr 18 '16 at 13:47
  • 1
    I would do some research into how to handle exceptions in promises. Check out http://stackoverflow.com/q/36345046/215552 and http://stackoverflow.com/q/26534303/215552 – Heretic Monkey Apr 18 '16 at 13:53
  • 1
    sure you had the right answer b4 yer man posted the example http://stackoverflow.com/questions/37286629/toggle-checkbox-using-javascript thought you deserved an upvote! – Rachel Gallen May 17 '16 at 21:47

2 Answers2

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);
    }
}
Lex
  • 6,758
  • 2
  • 27
  • 42
  • Hi, thanks but wouldnt this still be caught in the success part of the .then() – Harry Apr 19 '16 at 10:11
  • The `Ok()` response will trigger your success callback, but the `InternalServerError()` response will trigger the error callback. – Lex Apr 19 '16 at 13:51
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.

FvB
  • 713
  • 6
  • 15