Open drawveloper opened 8 years ago
i would prefer a property like .ok
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)
}
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.
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.
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])
}
...
}
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.
Hi! Have you thought about implementing something like
validateStatus
fromaxios
?https://github.com/mzabriskie/axios#request-config
I could maybe draft a PR if you think this fits the bill.