settle.js 836 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. import AxiosError from './AxiosError.js';
  3. /**
  4. * Resolve or reject a Promise based on response status.
  5. *
  6. * @param {Function} resolve A function that resolves the promise.
  7. * @param {Function} reject A function that rejects the promise.
  8. * @param {object} response The response.
  9. *
  10. * @returns {object} The response.
  11. */
  12. export default function settle(resolve, reject, response) {
  13. const validateStatus = response.config.validateStatus;
  14. if (!response.status || !validateStatus || validateStatus(response.status)) {
  15. resolve(response);
  16. } else {
  17. reject(new AxiosError(
  18. 'Request failed with status code ' + response.status,
  19. [AxiosError.ERR_BAD_REQUEST, AxiosError.ERR_BAD_RESPONSE][Math.floor(response.status / 100) - 4],
  20. response.config,
  21. response.request,
  22. response
  23. ));
  24. }
  25. }