Superagent: Double callback and error on basic GET request

Created on 24 Apr 2015  Â·  11Comments  Â·  Source: visionmedia/superagent

_Summary:_
When calling a GET request, I get a strange minimal error and a "double callback!" warning. Setting the exact same headers works just fine with no errors using curl or HTTPie.

_Version:_
1.2.0

_Error:_

{ [SyntaxError: Unexpected token I] response: undefined }
double callback!

_Code:_

        request
            .get(url)
            .set('Accept', 'application/json')
            .set('X-ApiTokenId', 'abc-123')
            .set('X-SessionId', 'def-456')
            .end((err, res) => {
                if (err) {
                    cb(err, res);
                }
                else {
                    cb(null, []);
                }
            });

All 11 comments

As an update to this, for some reason removing one of the custom headers will cause superagent to work (removing either will work, as both are valid headers for my app), but adding both breaks superagent. I can add both headers using curl and HTTPie just fine, and the server handles them both. Not sure why adding both would cause a problem in superagent and cause a "double callback!" message.

As a final update, I can verify that the syntax error was due to a null value, but the double callback still is a problem when an error occurs. I believe it is happening when non-JSON content is returned and it tries to parse the expected JSON response. Not sure if this double callback warning is important or not...

https://github.com/visionmedia/superagent/issues/464#issuecomment-111522076

I am experiencing the same issue. If there is a parse error, then you see the double callback error. I think it is due to error handling again in the response parsing:

https://github.com/visionmedia/superagent/blob/master/lib/node/index.js#L955

It calls the callback and processing continues, which means that later an end listener is registered and that calls the callback again.

I can confirm this, what @englercj says completely makes sense

Also, a quick glance at the various parsers shows that they never throw and instead always return errors via the callback. Custom parsers _should_ conform to this behavior as well.

for me this is a blocker atm. It happens randomly once a day.

my node backend is requesting some information from a service via superagent. In this call to the service I use just one header entry .set('Authorization', 'Bearer ' + access_token).
The final result from this service is logged as a nice JS object like this: {name: "mr.x", roles: ["user"]} just before resolve(user) is called with that object.

My frontend is requesting my node backend without any header set and the response in the frontend is a broken json.

see the missing closing brace in the rawResponse.

double callback!
LOGIN_FAIL { [SyntaxError: Unexpected end of input]
                        rawResponse: '{"name":"mr.x","roles":["user"]',
                        response: undefined }

any update on this ?

Experimenting the same problem on a very basic GET.

could you send something to reproduce this very simple get? What versions do you have of superagent, node, express ? I cant reproduce this behavior deterministically. In our case this happens after like 48h of heavy load. Also in our case we COULD have problems with the header entry "content-lenth" but thats not verified yet.

Looking forward to your "simple get".

On 20.03.2016, at 08:44, Maxime Gir [email protected] wrote:

Experimenting the same problem on a very basic GET.

—
You are receiving this because you commented.
Reply to this email directly or view it on GitHub

    chai.request(server)
    .get('/')
    .set('Accept', 'application/json')
    .end( (err, res) => {
        res.should.be.json;
        done();
    });

Uncaught AssertionError: expected 'text/html; charset=utf-8' to include 'application/json'

  "devDependencies": {
    "chai": "^4.1.1",
    "chai-as-promised": "^7.1.1",
    "chai-http": "^3.0.0",
    "chai-url": "^1.0.4",
    "mocha": "^3.5.0",
    "nyc": "^11.1.0"
  }

Ignore. Looking at the wrong terminal.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vlinder picture vlinder  Â·  8Comments

pszabop picture pszabop  Â·  3Comments

kornelski picture kornelski  Â·  9Comments

t3hmrman picture t3hmrman  Â·  9Comments

tj picture tj  Â·  9Comments