-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathlogErrorMessage.ts
More file actions
54 lines (47 loc) · 1.39 KB
/
logErrorMessage.ts
File metadata and controls
54 lines (47 loc) · 1.39 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
import { Logger, LogLevel } from "@fern-api/logger";
import { TaskAbortSignal } from "@fern-api/task-context";
import chalk from "chalk";
const USE_NODE_18_OR_ABOVE_MESSAGE = "The Fern CLI requires Node 18+ or above.";
export function logErrorMessage({
message,
error,
logger,
logLevel = LogLevel.Error
}: {
message: string | undefined;
error: unknown;
logger: Logger;
logLevel?: LogLevel;
}): void {
if (message != null) {
logger.log(logLevel, message);
} else if (error == null) {
return;
}
// thrower is responsible for logging, so we don't need to log the error's message too
if (error instanceof TaskAbortSignal) {
return;
}
if (error != null) {
const errorMessage = convertErrorToString(error);
if (errorMessage != null) {
logger.log(logLevel, errorMessage);
}
}
const stack = error instanceof Error ? error.stack : new Error(JSON.stringify(error)).stack;
if (stack != null) {
logger.debug(chalk.red(stack));
}
}
function convertErrorToString(error: unknown): string | undefined {
if (typeof error === "string") {
return error;
}
if (error instanceof Error) {
if ((error as Error)?.message?.includes("globalThis")) {
return USE_NODE_18_OR_ABOVE_MESSAGE;
}
return error.message;
}
return undefined;
}