Skip to content

Migrating 0.x to 1.x

Roman Shtylman edited this page Mar 13, 2015 · 4 revisions

Migrating from v0.x to v1.x

.end arity for errors removed.

The callback for .end() will always be passed err, res. Arity is no longer used to detect response only.

Before

superagent
.get('/foo')
.end(function(res) {
});

After

superagent
.get('/foo')
.end(function(err, res) {
});

Any non 2xx response is considered an error.

The err argument for the .end() callback will be present for any response which is not considered "successful".

Before

superagent
.get('/foo')
.end(function(err, res) {
  if (err) {
    // network error
    return;
  }
  if (res.status === 400) {
    // res.body... res.text.. etc
    return;
  }
});

After

superagent
.get('/foo')
.end(function(err, res) {
  if (err) {
    if (!err.response) {
      // network error or timeout, no response sent
      return;
    }

    // other HTTP error status and responses
    // err.response.body, etc can be used if you desire
    err.status === res.status;
    err.response === res;
    return;
  }

  // res.status was successful (2xx)
  // res.body, res.text .. etc
});