Skip to content

Commit c14519d

Browse files
committed
docs: http exceptions
1 parent 2ccf3ee commit c14519d

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

docs/3.exception.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ request.use(async (context, next) => {
4747
})
4848
```
4949

50+
Keq 提供了常见的 HTTP Exception,均继承自 `RequestException`
51+
52+
- `BadRequestException` (400)
53+
- `UnauthorizedException` (401)
54+
- `ForbiddenException` (403)
55+
- `NotFoundException` (404)
56+
- `MethodNotAllowedException` (405)
57+
- `NotAcceptableException` (406)
58+
- `ProxyAuthenticationRequiredException` (407)
59+
- `RequestTimeoutException` (408)
60+
- `ConflictException` (409)
61+
- `PreconditionFailedException` (412)
62+
- `ContentTooLargeException` (413)
63+
- `UriTooLongException` (414)
64+
- `UnsupportedMediaTypeException` (415)
65+
- `ImATeapotException` (418)
66+
- `TooManyRequestsException` (429)
67+
- `InternalServerErrorException` (500)
68+
- `NotImplementedException` (501)
69+
- `BadGatewayException` (502)
70+
- `ServiceUnavailableException` (503)
71+
- `GatewayTimeoutException` (504)
72+
73+
5074
### AbortException
5175

5276
继承自 `DOMException`,当请求被主动中止时抛出。

docs/7.libraries/2.exception.mdx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,70 @@ request
4949
| statusCode | - | 错误码 |
5050
| message | `''` | 错误信息 |
5151
| retry | `true` | 异常是否会触发`retry` |
52+
53+
## Middleware
54+
55+
### validateStatusCode
56+
57+
验证 `Response``status`,并抛出对应的 `RequestException`
58+
59+
- `status``200-399` 范围内时,不抛出异常
60+
- `status = 400` 时,抛出 `BadRequestException(statusText)`
61+
- `status = 401` 时,抛出 `UnauthorizedException(statusText)`
62+
- `status = 403` 时,抛出 `ForbiddenException(statusText)`
63+
- `status = 404` 时,抛出 `NotFoundException(statusText)`
64+
- `status = 405` 时,抛出 `MethodNotAllowedException(statusText)`
65+
- `status = 406` 时,抛出 `NotAcceptableException(statusText)`
66+
- `status = 407` 时,抛出 `ProxyAuthenticationRequiredException(statusText)`
67+
- `status = 408` 时,抛出 `RequestTimeoutException(statusText)`
68+
- `status = 409` 时,抛出 `ConflictException(statusText)`
69+
- `status = 412` 时,抛出 `PreconditionFailedException(statusText)`
70+
- `status = 413` 时,抛出 `ContentTooLargeException(statusText)`
71+
- `status = 414` 时,抛出 `UriTooLongException(statusText)`
72+
- `status = 415` 时,抛出 `UnsupportedMediaTypeException(statusText)`
73+
- `status = 418` 时,抛出 `ImATeapotException(statusText)`
74+
- `status = 429` 时,抛出 `TooManyRequestsException(statusText)`
75+
- `status` 为其他 `400-499` 的数值时,抛出 `RequestException(status, statusText, false)`
76+
- `status = 500` 时,抛出 `InternalServerErrorException(statusText
77+
- `status = 501` 时,抛出 `NotImplementedException(statusText)`
78+
- `status = 502` 时,抛出 `BadGatewayException(statusText)`
79+
- `status = 503` 时,抛出 `ServiceUnavailableException(statusText)`
80+
- `status = 504` 时,抛出 `GatewayTimeoutException(statusText)`
81+
- `status` 为其他 `500-599` 的数值时,抛出 `RequestException(status, statusText, true)`
82+
83+
**示例**
84+
85+
```typescript
86+
import { request } from "keq"
87+
import { validateStatusCode } from "@keq-request/exception"
88+
89+
request.use(validateStatusCode())
90+
```
91+
92+
## Plugin
93+
94+
### ValidateStatusCode
95+
96+
用于 `@keq-request/cli` 的 Plugin,其主要功能有:
97+
98+
1. 删除 `openapi` / `swagger` 文档中的异常响应定义
99+
2. 添加 `validateStatusCode` Middleware
100+
101+
**Options**
102+
103+
| **参数** | **是否必填** | **类型** | **默认值** | **描述** |
104+
| :------- | :----------- | :------- | :--------- | ---------------------- |
105+
| modules || string[] | `undefined` | 生效的 `module` 名称列表 |
106+
107+
**示例**
108+
109+
```typescript title=".keqrc.ts"
110+
import { DefineKeqConfig } from "@keq-request/cli"
111+
import { ValidateStatusCode } from "@keq-request/exception/plugins"
112+
113+
const config: DefineKeqConfig = {
114+
// ...
115+
plugins: [
116+
new ValidateStatusCode(/* { modules: [...] } */)
117+
],
118+
}

0 commit comments

Comments
 (0)