mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
5107ee69ae
* fix(core): prevent undefined error codes in settle for non-4xx/5xx status codes Replace array indexing logic with conditional check to ensure proper error code assignment for all HTTP status code ranges. Previously, the error code selection used: [ERR_BAD_REQUEST, ERR_BAD_RESPONSE][Math.floor(status / 100) - 4] This produced undefined for status codes outside 4xx/5xx ranges (1xx, 2xx, 3xx, 6xx+), which could break error handling when users customize validateStatus to reject non-standard status codes. Now uses: status >= 400 && status < 500 ? ERR_BAD_REQUEST : ERR_BAD_RESPONSE This ensures: - 4xx codes → ERR_BAD_REQUEST - All other error codes → ERR_BAD_RESPONSE * test(core): add unit tests for settle error code assignment Cover the boundary conditions that were previously untested: - 4xx status codes → ERR_BAD_REQUEST - 5xx status codes → ERR_BAD_RESPONSE - Non-4xx/5xx codes rejected via custom validateStatus → ERR_BAD_RESPONSE (defined, not undefined) These tests would have caught the original array-indexing bug where Math.floor(status/100) - 4 returned negative/out-of-range indices for 1xx, 2xx, 3xx, and 6xx+ status codes, resulting in undefined error.code. * test(core): describe behavior, not PR history, in settle tests --------- Co-authored-by: VeerShah41 <https://github.com/VeerShah41> Co-authored-by: Jason Saayman <jasonsaayman@gmail.com>
28 lines
846 B
JavaScript
28 lines
846 B
JavaScript
'use strict';
|
|
|
|
import AxiosError from './AxiosError.js';
|
|
|
|
/**
|
|
* Resolve or reject a Promise based on response status.
|
|
*
|
|
* @param {Function} resolve A function that resolves the promise.
|
|
* @param {Function} reject A function that rejects the promise.
|
|
* @param {object} response The response.
|
|
*
|
|
* @returns {object} The response.
|
|
*/
|
|
export default function settle(resolve, reject, response) {
|
|
const validateStatus = response.config.validateStatus;
|
|
if (!response.status || !validateStatus || validateStatus(response.status)) {
|
|
resolve(response);
|
|
} else {
|
|
reject(new AxiosError(
|
|
'Request failed with status code ' + response.status,
|
|
response.status >= 400 && response.status < 500 ? AxiosError.ERR_BAD_REQUEST : AxiosError.ERR_BAD_RESPONSE,
|
|
response.config,
|
|
response.request,
|
|
response
|
|
));
|
|
}
|
|
}
|