Skip to content

Commit 6f56350

Browse files
author
延枚
committed
优化错误输出
Change-Id: I06900d50cf44fb7dfa46529c27599102f7d49ece
1 parent 6b6b820 commit 6f56350

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

common/errors.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ export class YunxiaoError extends Error {
22
constructor(
33
message: string,
44
public readonly status: number,
5-
public readonly response: unknown
5+
public readonly response: unknown,
6+
public readonly url?: string,
7+
public readonly method?: string,
8+
public readonly requestHeaders?: Record<string, string>,
9+
public readonly requestBody?: unknown
610
) {
711
super(message);
812
this.name = "YunxiaoError";
@@ -58,7 +62,14 @@ export function isYunxiaoError(error: unknown): error is YunxiaoError {
5862
return error instanceof YunxiaoError;
5963
}
6064

61-
export function createYunxiaoError(status: number, response: any): YunxiaoError {
65+
export function createYunxiaoError(
66+
status: number,
67+
response: any,
68+
url?: string,
69+
method?: string,
70+
requestHeaders?: Record<string, string>,
71+
requestBody?: unknown
72+
): YunxiaoError {
6273
switch (status) {
6374
case 401:
6475
return new YunxiaoAuthenticationError(response?.message);
@@ -83,7 +94,11 @@ export function createYunxiaoError(status: number, response: any): YunxiaoError
8394
return new YunxiaoError(
8495
response?.message || "Yunxiao API error",
8596
status,
86-
response
97+
response,
98+
url,
99+
method,
100+
requestHeaders,
101+
requestBody
87102
);
88103
}
89104
}

common/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,14 @@ export async function yunxiaoRequest(
136136
debug(`Response status:`, response.ok)
137137

138138
if (!response.ok) {
139-
throw createYunxiaoError(response.status, responseBody);
139+
throw createYunxiaoError(
140+
response.status,
141+
responseBody,
142+
url,
143+
options.method || "GET",
144+
requestHeaders,
145+
options.body
146+
);
140147
}
141148

142149
return responseBody;

index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,23 @@ const server = new Server(
5151
function formatYunxiaoError(error: YunxiaoError): string {
5252
let message = `Yunxiao API Error: ${error.message}`;
5353

54+
// 添加请求上下文信息
55+
if (error.method || error.url) {
56+
message += `\n Request: ${error.method || 'GET'} ${error.url || 'unknown'}`;
57+
}
58+
59+
if (error.requestHeaders) {
60+
message += `\n Request Headers: ${JSON.stringify(error.requestHeaders, null, 2)}`;
61+
}
62+
63+
if (error.requestBody) {
64+
message += `\n Request Body: ${JSON.stringify(error.requestBody, null, 2)}`;
65+
}
66+
5467
if (error instanceof YunxiaoValidationError) {
5568
message = `Parameter validation failed: ${error.message}`;
5669
if (error.response) {
57-
message += `\n errorMessage: ${JSON.stringify(error.response, null, 2)}`;
70+
message += `\n Response: ${JSON.stringify(error.response, null, 2)}`;
5871
}
5972
// 添加常见参数错误的提示
6073
if (error.message.includes('name')) {
@@ -77,6 +90,11 @@ function formatYunxiaoError(error: YunxiaoError): string {
7790
if (response.data && typeof response.data === 'object') {
7891
message += `\n data: ${JSON.stringify(response.data, null, 2)}`;
7992
}
93+
94+
// 如果响应体中有更多详细信息,也一并显示
95+
if (Object.keys(response).length > 0) {
96+
message += `\n Full Response: ${JSON.stringify(response, null, 2)}`;
97+
}
8098
}
8199

82100
// 根据状态码提供通用建议

0 commit comments

Comments
 (0)