test-500-error.html
8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试500错误处理</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.test-section {
margin: 20px 0;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
}
.result {
margin-top: 10px;
padding: 10px;
border-radius: 3px;
font-family: monospace;
white-space: pre-wrap;
}
.success { background-color: #d4edda; color: #155724; }
.error { background-color: #f8d7da; color: #721c24; }
.info { background-color: #d1ecf1; color: #0c5460; }
button {
padding: 10px 20px;
margin: 5px;
border: none;
border-radius: 3px;
cursor: pointer;
background-color: #007bff;
color: white;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>测试500错误处理</h1>
<p>这个页面用于测试修复后的HTTP状态码错误处理是否能正确触发error事件。</p>
<div class="test-section">
<h3>测试1: 500服务器错误(带重试)</h3>
<button onclick="test500Error()">发送500错误请求</button>
<div id="result500" class="result"></div>
</div>
<div class="test-section">
<h3>测试1B: 500服务器错误(无重试)</h3>
<button onclick="test500ErrorNoRetry()">发送500错误请求(无重试)</button>
<div id="result500NoRetry" class="result"></div>
</div>
<div class="test-section">
<h3>测试2: 404客户端错误</h3>
<button onclick="test404Error()">发送404错误请求</button>
<div id="result404" class="result"></div>
</div>
<div class="test-section">
<h3>测试3: 业务错误响应</h3>
<button onclick="testBusinessError()">发送业务错误请求</button>
<div id="resultBusiness" class="result"></div>
</div>
<div class="test-section">
<h3>测试4: 404错误响应</h3>
<button onclick="test404Error()">发送404错误请求</button>
<div id="result404" class="result"></div>
</div>
<div class="test-section">
<h3>测试5: 200成功响应</h3>
<button onclick="testSuccess()">发送成功请求</button>
<div id="resultSuccess" class="result"></div>
</div>
<div class="test-section">
<h3>事件监听日志</h3>
<button onclick="clearLogs()">清空日志</button>
<div id="eventLogs" class="result info"></div>
</div>
<script src="../dist/index.umd.js"></script>
<script>
// 创建请求实例
const request = new GlskToolkit.Request({
baseURL: 'http://localhost:3002',
timeout: 10000,
events: {
request: (data, config) => {
addLog('🚀 REQUEST事件触发', { url: config.url, method: config.method });
},
response: (data, config) => {
addLog('📥 RESPONSE事件触发', { status: data.status, url: config.url });
},
success: (data, config) => {
addLog('✅ SUCCESS事件触发', { data, url: config.url });
},
error: (data, config) => {
addLog('❌ ERROR事件触发', { data, url: config.url });
},
logout: (data, config) => {
addLog('🚪 LOGOUT事件触发', { data, url: config.url });
}
}
});
function addLog(title, data) {
const logs = document.getElementById('eventLogs');
const timestamp = new Date().toLocaleTimeString();
logs.textContent += `[${timestamp}] ${title}\n${JSON.stringify(data, null, 2)}\n\n`;
logs.scrollTop = logs.scrollHeight;
}
function clearLogs() {
document.getElementById('eventLogs').textContent = '';
}
function showResult(elementId, type, message, data = null) {
const element = document.getElementById(elementId);
element.className = `result ${type}`;
element.textContent = message;
if (data) {
element.textContent += '\n\n' + JSON.stringify(data, null, 2);
}
}
async function test500Error() {
showResult('result500', 'info', '正在发送500错误请求(带重试)...');
try {
const response = await request.get('/api/test-500', {
retry: {
retries: 2, // 重试2次
retryDelay: 500, // 重试延迟500ms
shouldResetTimeout: true
}
});
showResult('result500', 'success', '请求成功(这不应该发生)', response);
} catch (error) {
showResult('result500', 'error', `捕获到错误: ${error.message}`, {
code: error.code,
status: error.response?.status,
statusText: error.response?.statusText
});
}
}
async function test500ErrorNoRetry() {
showResult('result500NoRetry', 'info', '正在发送500错误请求(无重试)...');
try {
const response = await request.get('/api/test-500'); // 不设置retry
showResult('result500NoRetry', 'success', '请求成功(这不应该发生)', response);
} catch (error) {
showResult('result500NoRetry', 'error', `捕获到错误: ${error.message}`, {
code: error.code,
status: error.response?.status,
statusText: error.response?.statusText
});
}
}
async function test404Error() {
showResult('result404', 'info', '正在发送404错误请求...');
try {
const response = await request.get('/api/not-found');
showResult('result404', 'success', '请求成功(这不应该发生)', response);
} catch (error) {
showResult('result404', 'error', `捕获到错误: ${error.message}`, {
code: error.code,
status: error.response?.status,
statusText: error.response?.statusText
});
}
}
async function testSuccess() {
showResult('resultSuccess', 'info', '正在发送成功请求...');
try {
const response = await request.get('/api/test-success');
showResult('resultSuccess', 'success', '请求成功', response);
} catch (error) {
showResult('resultSuccess', 'error', `请求失败: ${error.message}`, error);
}
}
async function testBusinessError() {
showResult('resultBusiness', 'info', '正在发送业务错误请求...');
try {
const response = await request.get('/api/test-error');
showResult('resultBusiness', 'success', '请求成功(这不应该发生)', response);
} catch (error) {
showResult('resultBusiness', 'error', `捕获到错误: ${error.message}`, {
code: error.code,
status: error.response?.status,
statusText: error.response?.statusText
});
}
}
async function test404Error() {
showResult('result404', 'info', '正在发送404错误请求...');
try {
const response = await request.get('/api/not-found');
showResult('result404', 'success', '请求成功(这不应该发生)', response);
} catch (error) {
showResult('result404', 'error', `捕获到错误: ${error.message}`, {
code: error.code,
status: error.response?.status,
statusText: error.response?.statusText
});
}
}
// 页面加载时显示初始信息
addLog('📋 页面加载完成', { message: '准备测试HTTP状态码错误处理' });
</script>
</body>
</html>