Commit 228a250e by liimin

refactor(sentry): 将sentryDsn从必填改为可选配置

修改Request类和请求方法,使sentryDsn变为可选参数
更新类型定义和文档,推荐在初始化时设置sentryDsn
1 parent 2578afad
......@@ -13,6 +13,7 @@
- 🔧 可扩展:支持自定义签名算法
- 📋 数组参数:支持发送包含数组的请求参数
- 🔨 配置类:提供 Defaults 类用于更灵活地管理配置
- 🔍 错误监控:集成 Sentry 错误上报,自动捕获网络和业务错误
## 安装
......@@ -321,6 +322,52 @@ request.get('/users').then(response => {
});
```
### Sentry 错误上报
glsk-fetch 集成了 Sentry 错误上报功能,可以自动捕获网络错误和业务逻辑错误:
```javascript
import Request, { reportBusinessError } from 'glsk-fetch';
// 创建请求实例时配置 Sentry
const request = new Request({
baseURL: 'https://api.example.com',
appId: 'your_app_id',
appSecret: 'your_app_secret',
// Sentry 配置
sentryDsn: 'https://your-sentry-dsn@sentry.io/project-id',
sentryOptions: {
environment: 'production',
release: 'v1.0.0'
}
});
// 网络错误自动上报
try {
await request.get('/api/users');
} catch (error) {
console.error('请求失败:', error.message);
// 错误已自动上报到 Sentry,无需手动处理
}
// 业务逻辑错误自动上报
// 假设 API 返回: { code: 40001, message: "用户未授权", data: null }
const response = await request.get('/api/user/profile');
// 虽然 HTTP 状态码是 200,但业务错误码非零
// 系统会自动检测并上报到 Sentry
// 手动上报业务错误
reportBusinessError(
'支付处理错误',
{ code: 50001, message: '服务暂时不可用' },
{ requestContext: { orderId: 'order123' } },
{ level: 'error', tags: { module: 'payment' } }
);
```
更多详细信息,请参阅 [Sentry 错误上报文档](./docs/sentry-error-reporting.md)。
## API
### Request 构造函数
......@@ -332,7 +379,10 @@ const request = new Request({
appId: '', // 应用ID(用于签名)
appSecret: '', // 应用密钥(用于签名)
headers: {}, // 默认请求头
signatureFunc: null // 自定义签名函数
signatureFunc: null, // 自定义签名函数
sentryDsn: '', // Sentry DSN(用于错误上报)
sentryOptions: {}, // Sentry 初始化选项
disableBusinessErrorReport: false // 是否禁用业务错误自动上报
});
```
......@@ -361,7 +411,10 @@ const request = new Request({
appId: '', // 应用ID
appSecret: '', // 应用密钥
signal: AbortSignal, // 用于取消请求的信号
signatureFunc: Function // 自定义签名函数
signatureFunc: Function, // 自定义签名函数
sentryDsn: '', // Sentry DSN(用于错误上报)
sentryOptions: {}, // Sentry 初始化选项
disableBusinessErrorReport: false // 是否禁用业务错误自动上报
}
```
......
......@@ -94,7 +94,7 @@
const request = new RequestConstructor({
baseURL: 'http://192.168.1.117:9898/facade',
appId: 'dj_zhixin',
appSecret: 'test-app-secret',
appSecret: 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b',
sentryDsn: 'https://be4fb79a912b4c34a5d1c539a1618978@sentry.glsx.com.cn/1',
});
......@@ -106,10 +106,10 @@
// 我们只需要在请求参数中添加method参数
console.log('发送请求,SDK将自动检查白名单并添加sdctag参数');
const result = await request.post('/dict/sign/test', {
const result = await request.post('/dict/sign/test1', {
params: {
id:0,
sessionId:"8b476c7d-6dbb-4059-aab3-5eb75988d2b4"
id:10,
objs:[1,2,3]
// method: 'clue.api.collision.info.exportCollisionInfo' // 白名单内的method
}
});
......
......@@ -11,7 +11,7 @@ export function addMethodAliases(instance: any): void {
// 对于GET、DELETE方法
instance[method] = function (this: any,
url: string,
config: RequestConfig = { sentryDsn: this.defaults.sentryDsn }
config: RequestConfig = {}
) {
return this.request({
url,
......@@ -24,7 +24,7 @@ export function addMethodAliases(instance: any): void {
instance[method] = function (this: any,
url: string,
data?: any,
config: RequestConfig = { sentryDsn: this.defaults.sentryDsn }
config: RequestConfig = {}
) {
return this.request({
url,
......
......@@ -80,7 +80,7 @@ class Request {
* @returns 可取消的请求Promise
*/
request<T = any>(
options: RequestConfig = { sentryDsn: this.defaults.sentryDsn }
options: RequestConfig = {}
): Promise<ResponseData<T>> & { abort?: () => void } {
// 合并配置
const config = this.defaults.merge(options);
......
......@@ -38,7 +38,7 @@ export interface RequestConfig extends RetryConfig {
events?: Record<string, Function>; // 事件订阅配置
httpOptions?: any; // HTTP选项
returnAll?: boolean; // 是否返回完整响应
sentryDsn: string; // Sentry DSN 配置(必填)
sentryDsn?: string; // Sentry DSN 配置(可选,推荐在初始化时设置)
sentryOptions?: Record<string, any>; // Sentry 初始化选项
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!