0

I'm executing an $http.get over websockets and somehow the .error doesn't populate any of the parameters. I'm using angular 1.3.5 and latest Chrome on OSX and local host is aliased to mywebsite.com.

$http({method: 'GET', 'ws://mywebsite.com/resource'})
.success(function(response){ ... })
.error(function(err, status){
   console.log(err); <<< here err and status are null respectively 0 even the XHR Response logs 401 in console.
});

Any clues why this is doing so? No mater what error code it is, it doesn't get passed to error callback. For 2xx response i do get the data and all is fine.

Just to clarify, the .error callback gets called as normal, but err and status re not populated.

3
  • is that a typo? you put two dots headers}). .success Commented Dec 13, 2014 at 1:22
  • 1
    sorry copied paste my headers variable from real code Commented Dec 13, 2014 at 1:27
  • if you try it with an http request instead of the ws do you get the callback populated? Commented Dec 13, 2014 at 1:47

2 Answers 2

1

Well, well, I discovered what was happening. A combo of things.

First I'm using KOA and the above angular hits the KOA as REST API. I'm also using koa-jwt to auth the users and generate a token.

Now the api runs on a subdomain and even i set the CORS via koa-cors to allow * access.

The issue is that koa-jwt when the token is expired, they simply do a this.throw(401). The way KOA handles that it so immediately terminate subsequent middleware and exit with that error. THAT, didn't allow koa-cors headers to be set, regardless where I put that middleware (before or after koa-jwt).

Hence, the elegant fix was to wrap my top level yield next in a try catch and avoid allowing koa-jwt ctx.throw to propagate.

On Angular side, the browser was refusing to convey the 401 to the .error complaining it didn't find a suitable CORS to allow it to process and hand over the response :).

app.use(cors({origin:"*", methods:'GET,HEAD,PUT,POST,DELETE,PATCH'}));  //allow all origins to the API. 
app.use ( function *(next){
    try {
      yield next;
    } catch (err) {
      this.status = err.status || 500;
      this.body = err.message;
      this.app.emit('error', err, this);
    }
});

... more middleware
// middleware below this line is only reached if jwt token is valid
app.use(jwt({secret: config.app.secret}));

Allowing a this.trow(401) from koa-jwt will ruin your day.

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

Comments

0

I think you just make a typo, just write like this:

$http({method: 'GET', 'ws://mywebsite.com/resource', headers: headers})
.success(function(response){ ... })
.error(function(err, status){
   console.log(err);
});

However as a best practice I would do as

var request = {
   method: 'GET',
   url: 'ws://mywebsite.com/resource',
   headers: {//insert here header you want, put Origin header only for example purposes
      Origin: 'ws://mywebsite.com/resource'
   }
}

$http(request)
.success(function(response){ ... })
.error(function(err, status){
   console.log(err);
});

If the typo is not the error (you may copied the code) just look at this AngularJS $http error function never called

5 Comments

That's not the issue of typo, in your code, the err is null when in fact th fires the .error callback when status code is not 2xx. You can omit headers from my code... the issue still remains.
How are you defining controller? Maybe you need a closure
Yes, just did, the "response" object in the error function callback conveys same info as using .error/.success. status=0 and statusText="". Must be lower level inside angular. Hate to start debugging that. Thanks for suggestions.
@Biba let me know if you find something. Have you tried to fiddler that calls?
did, see my full explanation in the solution stuff. big gotcha with CORS and 401

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.