Source: Core/RequestErrorEvent.js

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