Debian Codegraph @

node/rxjs/src/internal/ajax/errors.ts

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