Skip to content
22 changes: 18 additions & 4 deletions console/atest-ui/src/views/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,24 @@ async function DefaultResponseProcess(response: any) {
throw new Error("Unauthenticated")
}

const message = await response.json().then((data: any) => data.message)
throw new Error(message)
} else {
return response.json()
try {
const message = await response.json().then((data: any) => data.message)
throw new Error(message)
} catch {
const text = await response.text()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code will always run to here due to line 37.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

throw new Error(text)
}
}

const contentType = response.headers.get('Content-Type') || '';
if (contentType.startsWith('text/plain')) {
return response.text();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it might always return in JSON format. The response is from atest itself instead of the target API. For example, when sending an HTTP GET request of https://gitee.com/linuxsuren/api-testing/raw/master/README.md, you need to check the response of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯

}
try {
return await response.json();
} catch (e) {
// If JSON parsing fails, return as text
return response.text();
}
}

Expand Down