2.1.0 • Published 4 days ago
keq-exception v2.1.0
keq-exception
The keq-exception module is a package that can be used to throw and catch exceptions in keq-request.
Usage
import { request } from 'keq'
import { throwException, catchException, RequestException } from 'keq-exception'
request
.use(catchException(err => {
if (err instanceof RequestException) {
context.redirect('/login')
}
context.redirect('/login')
throw err
}))
.use(throwException({
// there is the default options
// wether or not to redirect to throw request exception
condition: () => ctx => ctx.response && ctx.response.status >= 400,
// the status code of request exception
statusCode: ctx => ctx.response.status,
// the message of request exception
message: ctx => ctx.response.text(),
}))
The order of keq middleware is important(like an onion).
Contributing & Development
If there is any doubt, it is very welcome to discuss the issue together. Please read Contributor Covenant Code of Conduct and CONTRIBUTING.