1b98e729d0e1415b009bdbf9fbea3afab80b1458ba2bdf775079215fcd356feac118a2c92eeeb04f24c38d7890493f3999f07a1585b9c97f7be1769c0b9142 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { AjaxRequest } from './types';
  2. import { getXHRResponse } from './getXHRResponse';
  3. import { createErrorClass } from '../util/createErrorClass';
  4. /**
  5. * A normalized AJAX error.
  6. *
  7. * @see {@link ajax}
  8. */
  9. export interface AjaxError extends Error {
  10. /**
  11. * The XHR instance associated with the error.
  12. */
  13. xhr: XMLHttpRequest;
  14. /**
  15. * The AjaxRequest associated with the error.
  16. */
  17. request: AjaxRequest;
  18. /**
  19. * The HTTP status code, if the request has completed. If not,
  20. * it is set to `0`.
  21. */
  22. status: number;
  23. /**
  24. * The responseType (e.g. 'json', 'arraybuffer', or 'xml').
  25. */
  26. responseType: XMLHttpRequestResponseType;
  27. /**
  28. * The response data.
  29. */
  30. response: any;
  31. }
  32. export interface AjaxErrorCtor {
  33. /**
  34. * @deprecated Internal implementation detail. Do not construct error instances.
  35. * Cannot be tagged as internal: https://github.com/ReactiveX/rxjs/issues/6269
  36. */
  37. new (message: string, xhr: XMLHttpRequest, request: AjaxRequest): AjaxError;
  38. }
  39. /**
  40. * Thrown when an error occurs during an AJAX request.
  41. * This is only exported because it is useful for checking to see if an error
  42. * is an `instanceof AjaxError`. DO NOT create new instances of `AjaxError` with
  43. * the constructor.
  44. *
  45. * @see {@link ajax}
  46. */
  47. export const AjaxError: AjaxErrorCtor = createErrorClass(
  48. (_super) =>
  49. function AjaxErrorImpl(this: any, message: string, xhr: XMLHttpRequest, request: AjaxRequest) {
  50. this.message = message;
  51. this.name = 'AjaxError';
  52. this.xhr = xhr;
  53. this.request = request;
  54. this.status = xhr.status;
  55. this.responseType = xhr.responseType;
  56. let response: any;
  57. try {
  58. // This can throw in IE, because we have to do a JSON.parse of
  59. // the response in some cases to get the expected response property.
  60. response = getXHRResponse(xhr);
  61. } catch (err) {
  62. response = xhr.responseText;
  63. }
  64. this.response = response;
  65. }
  66. );
  67. export interface AjaxTimeoutError extends AjaxError {}
  68. export interface AjaxTimeoutErrorCtor {
  69. /**
  70. * @deprecated Internal implementation detail. Do not construct error instances.
  71. * Cannot be tagged as internal: https://github.com/ReactiveX/rxjs/issues/6269
  72. */
  73. new (xhr: XMLHttpRequest, request: AjaxRequest): AjaxTimeoutError;
  74. }
  75. /**
  76. * Thrown when an AJAX request times out. Not to be confused with {@link TimeoutError}.
  77. *
  78. * This is exported only because it is useful for checking to see if errors are an
  79. * `instanceof AjaxTimeoutError`. DO NOT use the constructor to create an instance of
  80. * this type.
  81. *
  82. * @see {@link ajax}
  83. */
  84. export const AjaxTimeoutError: AjaxTimeoutErrorCtor = (() => {
  85. function AjaxTimeoutErrorImpl(this: any, xhr: XMLHttpRequest, request: AjaxRequest) {
  86. AjaxError.call(this, 'ajax timeout', xhr, request);
  87. this.name = 'AjaxTimeoutError';
  88. return this;
  89. }
  90. AjaxTimeoutErrorImpl.prototype = Object.create(AjaxError.prototype);
  91. return AjaxTimeoutErrorImpl;
  92. })() as any;