Skip to content

Commit d867584

Browse files
committed
Fix text/plain response handling and improve code quality and code maintainability
1 parent 7922d34 commit d867584

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

console/atest-ui/src/views/TestCase.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,17 @@ const parseResponseBody = (body: any) => {
112112
}
113113
}
114114
115-
const handleTestResult = (e: any) => {
115+
/**
116+
* Handles test result data from API response
117+
*
118+
* Processes the test response with proper error handling and content type detection:
119+
* - For JSON responses: Parses and makes it available for filtering/display
120+
* - For plain text responses: Displays as raw text without JSON parsing
121+
* - For file responses: Handles as downloadable content
122+
*
123+
* @param e The test result data from API
124+
*/
125+
const handleTestResult = (e: any): void => {
116126
testResult.value = e;
117127
118128
if (!isHistoryTestCase.value) {
@@ -125,10 +135,12 @@ const handleTestResult = (e: any) => {
125135
} else if(e.body !== ''){
126136
testResult.value.bodyLength = e.body.length
127137
try {
138+
// Try to parse as JSON, fallback to plain text if parsing fails
128139
testResult.value.bodyObject = JSON.parse(e.body);
129140
testResult.value.originBodyObject = JSON.parse(e.body);
130141
responseBodyFilter()
131142
} catch (error) {
143+
// JSON parsing failed, display as plain text
132144
testResult.value.bodyText = e.body;
133145
testResult.value.bodyObject = null;
134146
testResult.value.originBodyObject = null;

console/atest-ui/src/views/net.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ limitations under the License.
1515
*/
1616
import { Cache } from './cache'
1717

18-
async function DefaultResponseProcess(response: any) {
18+
/**
19+
* Process HTTP response with proper content type handling
20+
*
21+
* This function handles both JSON and non-JSON responses:
22+
* 1. For JSON responses, parses and returns the JSON object
23+
* 2. For non-JSON responses (like text/plain), returns the raw text
24+
*
25+
* @param response The fetch API response object
26+
* @returns Parsed JSON object or raw text content
27+
*/
28+
async function DefaultResponseProcess(response: any): Promise<any> {
1929
if (!response.ok) {
2030
switch (response.status) {
2131
case 401:
@@ -31,14 +41,14 @@ async function DefaultResponseProcess(response: any) {
3141
}
3242
}
3343

34-
// 先获取响应文本
44+
// Get the complete response text first
3545
const responseText = await response.text();
3646

37-
// 尝试解析为JSON,如果失败则直接返回原始文本
47+
// Try parsing as JSON, fallback to raw text if failed
3848
try {
3949
return JSON.parse(responseText);
4050
} catch (e) {
41-
// 不是有效的JSON,直接返回原始文本
51+
// Not valid JSON, return the raw text directly
4252
return responseText;
4353
}
4454
}

0 commit comments

Comments
 (0)