RequestErrorEvent.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import defined from "./defined.js";
  2. import parseResponseHeaders from "./parseResponseHeaders.js";
  3. /**
  4. * An event that is raised when a request encounters an error.
  5. *
  6. * @constructor
  7. * @alias RequestErrorEvent
  8. *
  9. * @param {Number} [statusCode] The HTTP error status code, such as 404.
  10. * @param {Object} [response] The response included along with the error.
  11. * @param {String|Object} [responseHeaders] The response headers, represented either as an object literal or as a
  12. * string in the format returned by XMLHttpRequest's getAllResponseHeaders() function.
  13. */
  14. function RequestErrorEvent(statusCode, response, responseHeaders) {
  15. /**
  16. * The HTTP error status code, such as 404. If the error does not have a particular
  17. * HTTP code, this property will be undefined.
  18. *
  19. * @type {Number}
  20. */
  21. this.statusCode = statusCode;
  22. /**
  23. * The response included along with the error. If the error does not include a response,
  24. * this property will be undefined.
  25. *
  26. * @type {Object}
  27. */
  28. this.response = response;
  29. /**
  30. * The headers included in the response, represented as an object literal of key/value pairs.
  31. * If the error does not include any headers, this property will be undefined.
  32. *
  33. * @type {Object}
  34. */
  35. this.responseHeaders = responseHeaders;
  36. if (typeof this.responseHeaders === "string") {
  37. this.responseHeaders = parseResponseHeaders(this.responseHeaders);
  38. }
  39. }
  40. /**
  41. * Creates a string representing this RequestErrorEvent.
  42. * @memberof RequestErrorEvent
  43. *
  44. * @returns {String} A string representing the provided RequestErrorEvent.
  45. */
  46. RequestErrorEvent.prototype.toString = function () {
  47. let str = "Request has failed.";
  48. if (defined(this.statusCode)) {
  49. str += ` Status Code: ${this.statusCode}`;
  50. }
  51. return str;
  52. };
  53. export default RequestErrorEvent;