This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy patherrorUtils.ts
More file actions
100 lines (83 loc) · 3.24 KB
/
errorUtils.ts
File metadata and controls
100 lines (83 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
export enum RequestErrorType {
NETWORK_ERROR,
UNEXPECTED_RESPONSE_STATUS,
REQUEST_TIMED_OUT,
UNABLE_TO_PARSE_RESPONSE
}
export interface GenericError {
error: string;
}
export interface RequestError extends GenericError {
timeout?: number;
statusCode: number;
response: string;
responseHeaders: { [key: string]: string };
}
export class ErrorUtils {
public static createRequestErrorObject(request: XMLHttpRequest, errorType: RequestErrorType): RequestError {
if (!request) {
return null;
}
return ErrorUtils.createRequestErrorObjectInternal(request.status, request.readyState, request.response, request.getAllResponseHeaders(), request.timeout, errorType);
}
/**
* Split out for unit testing purposes.
* Meant only to be called by ErrorUtils.createRequestErrorObject and UTs.
*/
public static createRequestErrorObjectInternal(status: number, readyState: number, response: any, responseHeaders: string, timeout: number, errorType: RequestErrorType): RequestError {
let errorMessage: string = ErrorUtils.formatRequestErrorTypeAsString(errorType);
if (errorType === RequestErrorType.NETWORK_ERROR) {
errorMessage += ErrorUtils.getAdditionalNetworkErrorInfo(readyState);
}
if (errorType === RequestErrorType.REQUEST_TIMED_OUT) {
status = 408;
}
let requestErrorObject: RequestError = {
error: errorMessage,
statusCode: status,
responseHeaders: ErrorUtils.convertResponseHeadersToJsonInternal(responseHeaders),
response: response
};
// add the timeout property iff
// 1) timeout is greater than 0ms
// 2) status code is not in the 2XX range
if (timeout > 0 && !(status >= 200 && status < 300)) {
requestErrorObject.timeout = timeout;
}
return requestErrorObject;
}
public static convertResponseHeadersToJson(request: XMLHttpRequest): { [key: string]: string } {
if (request === undefined || request === null) {
return null;
}
let responseHeaders: string = request.getAllResponseHeaders();
return ErrorUtils.convertResponseHeadersToJsonInternal(responseHeaders);
}
/**
* Split out for unit testing purposes.
* Meant only to be called by ErrorUtils.convertResponseHeadersToJson, ErrorUtils.createRequestErrorObject, and UTs.
*/
public static convertResponseHeadersToJsonInternal(responseHeaders: string): { [key: string]: string } {
// matches a string of form [key]:[value] or [key]: [value]
// g = global modifier, returns all matches found in string and not just the first
let responseHeadersRegex = /([^:]+):\s?(.*)/g;
let responseHeadersJson: { [key: string]: string } = {};
let m: RegExpExecArray;
while (m = responseHeadersRegex.exec(responseHeaders)) {
if (m.index === responseHeadersRegex.lastIndex) {
responseHeadersRegex.lastIndex++;
}
let headerKey = m[1].trim();
let headerValue = m[2].trim();
responseHeadersJson[headerKey] = headerValue;
}
return responseHeadersJson;
}
private static getAdditionalNetworkErrorInfo(readyState: number): string {
return ": " + JSON.stringify({ readyState: readyState });
}
private static formatRequestErrorTypeAsString(errorType: RequestErrorType): string {
let errorTypeString = RequestErrorType[errorType];
return errorTypeString.charAt(0).toUpperCase() + errorTypeString.replace(/_/g, " ").toLowerCase().slice(1);
}
}