-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy patherrorhandler.ts
More file actions
96 lines (87 loc) · 2.62 KB
/
Copy patherrorhandler.ts
File metadata and controls
96 lines (87 loc) · 2.62 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
import trace = require("./trace");
/**
* Formats any error type into a readable string message.
* Handles AggregateError, Error, strings, objects, and other types.
*/
function formatError(err: any): string {
// Handle AggregateError (from Promise.all/Promise.any failures)
if (err && err.name === "AggregateError" && Array.isArray(err.errors)) {
const messages = err.errors.map((e: any, index: number) =>
` [${index + 1}] ${formatError(e)}`
);
return `Multiple errors occurred:\n${messages.join("\n")}`;
}
// Handle plain strings
if (typeof err === "string") {
return err;
}
// Handle Error instances - use toString() to preserve "Error: message" format
if (err instanceof Error) {
return err.toString();
}
// Handle objects with a custom toString method (not the default Object.prototype.toString)
if (err !== null && typeof err === "object" && typeof err.toString === "function" && err.toString !== Object.prototype.toString) {
const result = err.toString();
// Make sure it's not returning "[object Object]" (the default)
if (result !== "[object Object]") {
return result;
}
}
// Handle objects with a message property (error-like objects)
if (typeof err?.message === "string") {
return err.message;
}
// Handle plain objects - try JSON serialization
if (typeof err === "object" && err !== null) {
try {
return JSON.stringify(err, null, 2);
} catch (e) {
return String(err);
}
}
// Fallback for any other type
return String(err);
}
export function httpErr(obj): any {
let errorAsObj = obj;
if (typeof errorAsObj === "string") {
try {
errorAsObj = JSON.parse(errorAsObj);
} catch (parseError) {
throw errorAsObj;
}
}
let statusCode: number = errorAsObj.statusCode;
if (statusCode === 401) {
throw "Received response 401 (Not Authorized). Check that your credentials are correct and that any access token hasn't expired.";
}
if (statusCode === 403) {
throw "Received response 403 (Forbidden). Check that you have access to this resource. Message from server: " +
errorAsObj.message;
}
let errorBodyObj = errorAsObj.body;
if (errorBodyObj) {
if (typeof errorBodyObj === "string") {
try {
errorBodyObj = JSON.parse(errorBodyObj);
} catch (parseError) {
throw errorBodyObj;
}
}
if (errorBodyObj.message) {
let message = errorBodyObj.message;
if (message) {
throw message;
} else {
throw errorBodyObj;
}
}
} else {
throw errorAsObj.message || "Encountered an unknown failure issuing an HTTP request.";
}
}
export function errLog(arg: any): void {
trace.debug(arg?.stack);
trace.error(formatError(arg));
process.exit(-1);
}