Copilot commented on code in PR #3305:
URL: https://github.com/apache/apisix-dashboard/pull/3305#discussion_r2896904114
##########
e2e/utils/req.ts:
##########
@@ -27,30 +27,61 @@ export const getPlaywrightRequestAdapter = (
ctx: APIRequestContext
): AxiosAdapter => {
return async (config) => {
- const { url, data, baseURL } = config;
+ const { url, data, baseURL, method } = config;
if (typeof url === 'undefined') {
throw new Error('Need to provide a url');
}
type Payload = Parameters<APIRequestContext['fetch']>[1];
const payload: Payload = {
headers: config.headers,
- method: config.method,
- failOnStatusCode: true,
+ method,
+ failOnStatusCode: false,
data,
};
const urlWithBase = `${baseURL}${url}`;
const res = await ctx.fetch(urlWithBase, payload);
+ const status = res.status();
- try {
+ // Idempotent DELETE: Treat 404 as 200 OK
+ if (method?.toLowerCase() === 'delete' && status === 404) {
+ console.warn(`[e2eReq] Ignored 404 on DELETE for ${urlWithBase},
treating as 200 OK.`);
return {
+ data: {},
+ status: 200,
+ statusText: 'OK',
+ headers: {},
+ config,
+ };
+ }
+
+ try {
+ let responseData = {};
+ try {
+ responseData = await res.json();
+ } catch {
+ // ignore JSON parse errors on empty or text responses
+ }
+ const response = {
...res,
- data: await res.json(),
+ data: responseData,
config,
- status: res.status(),
+ status,
statusText: res.statusText(),
headers: res.headers(),
};
+
+ if (config.validateStatus && !config.validateStatus(status)) {
+ throw new AxiosError(
+ `Request failed with status code ${status}`,
+ AxiosError.ERR_BAD_REQUEST,
+ config as unknown as InternalAxiosRequestConfig,
+ undefined,
+ response as unknown as AxiosResponse
+ );
Review Comment:
In the adapter's validateStatus failure path, the thrown AxiosError always
uses `AxiosError.ERR_BAD_REQUEST`. Axios distinguishes 4xx vs 5xx
(`ERR_BAD_REQUEST` for 4xx, `ERR_BAD_RESPONSE` for 5xx). Using the wrong code
can break assertions/logic that relies on `error.code`. Consider selecting the
error code based on `status` to match Axios behavior more closely.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]