Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: implement validateStatus like axios? #19

Open
drawveloper opened this issue Aug 11, 2016 · 6 comments
Open

Discussion: implement validateStatus like axios? #19

drawveloper opened this issue Aug 11, 2016 · 6 comments

Comments

@drawveloper
Copy link

Hi! Have you thought about implementing something like validateStatus from axios?

https://github.com/mzabriskie/axios#request-config

  // `validateStatus` defines whether to resolve or reject the promise for a given
  // HTTP response status code. If `validateStatus` returns `true` (or is set to `null`
  // or `undefined`), the promise will be resolved; otherwise, the promise will be
  // rejected.
  validateStatus: function (status) {
    return status >= 200 && status < 300; // default
  },

I could maybe draft a PR if you think this fits the bill.

@jonathanong
Copy link
Contributor

i would prefer a property like .ok

@drawveloper
Copy link
Author

Sorry, I don't follow precisely what you mean. Where would that property be?

Also, what do you feel about custom Error objects for rejecting the promise?

I'm currently using this hack on top of requisition:

export function successful (status) {
  return status >= 200 && status < 300
}

export function StatusCodeError (statusCode, statusMessage, response) {
  this.name = 'StatusCodeError'
  this.status = this.statusCode = statusCode
  this.message = statusMessage
  this.res = this.response = response

  if (Error.captureStackTrace) {
    Error.captureStackTrace(this)
  }
}
StatusCodeError.prototype = Object.create(Error.prototype)
StatusCodeError.prototype.constructor = StatusCodeError

Request.prototype._then = Request.prototype.then

Request.prototype.then = function (resolve, reject) {
  return this._then(res => {
    if (successful(res.statusCode)) {
      return resolve(res)
    }
    const error = new StatusCodeError(res.statusCode, res.statusMessage, res)
    if (res.is('json')) {
      return res.json().then(body => {
        error.error = body
        throw error
      })
    }
    throw error
  }, reject)
}

@haoxins
Copy link
Member

haoxins commented Aug 12, 2016

Sorry, I don't follow precisely what you mean. Where would that property be?

I think it's just

  const status = res.statusCode;
  this.status =
  this.statusCode = status;
  this.ok = status >= 200 && status < 300;

or

memo(Response.prototype, 'ok', function () {
  return this.status >= 200 && this.status < 300;
})

Also, what do you feel about custom Error objects for rejecting the promise?

this module deps http-errors, I think it's easy to implement such a api.

@drawveloper
Copy link
Author

Ah, sorry, I don't think the point came across precisely: the idea of the validateStatus is that the user defines what's the criteria for the promise to be rejected. Most promise-based clients right now seem to prefer the idiom that a status code outside of the 2xx range represents an "error", or a rejection of the promise.

@haoxins
Copy link
Member

haoxins commented Aug 12, 2016

Oh, sorry!

It's looks like ?

const createError = require('http-errors')
const statuses = require('statuses')

...

Response.prototype.checkStatus = function () {
  const status = this.status
  const ok = status >= 200 && status < 300
  if (!ok) {
    throw createError(status, statuses[status])
  }

  ...
}

@drawveloper
Copy link
Author

Yes! But the point of having a user-configured function is that it allows the users of the lib to determine whether they want their promise to fail or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants