Request exception for keq
npm install @keq-request/exception





[Document EN]: https://keq-request.github.io/guide/libraries/keq-exception
[Document CN]: https://keq-request.github.io/zh/guide/libraries/keq-exception
[Document][Document EN] | [中文文档][Document CN]
Middleware for throwing/catching exceptions. And it can control whether the exception trigger retry.
``typescript
import { request } from "keq"
import {
throwException,
catchException,
RequestException,
} from "keq-exception"
request
.use(
catchException((err) => {
if (err instanceof RequestException && err.code === 401) {
context.redirect("/login")
return
}
throw err
})
)
// Callback will run after await next().`
// This way you can throw errors based on the response body.
.use(
throwException(async (ctx) => {
if (ctx.response && ctx.response.status >= 400) {
const body = await ctx.response.json()
throw new RequestException(ctx.response.status, body.message)
}
})
)
| Parameter | Default | Description |
| :------------ | :---------- | -------------------------------------------- |
| statusCode | - | Error code |
| message | '' | Error message |true` | Whether the thrown error can trigger a retry |
| retry |