Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 4x 4x 4x 4x 4x 4x 4x 4x 40048x 4x 4x 8x 8x 8x 8x 8x 4x 8x 8x 8x 8x 8x 8x | import axios, { AxiosError } from "axios";
import { z } from "zod";
import { loggerFactory } from "../logger";
import { getFromEnv } from "./env";
const logger = loggerFactory("utils/errors");
export function assert(value: unknown, errMsg: string): asserts value {
Iif (!value) {
throw new Error(`Assertion failed: ${errMsg}`);
}
}
export function assertThenReturn<T>(value: T | undefined, errMsg: string): T {
Iif (!value) {
throw new Error(`Assertion failed: ${errMsg}`);
}
return value;
}
export const assertWithLog = (condition: boolean, errMsg: string) => {
Iif (!condition) {
logger.error(`Assertion failed: ${errMsg}`);
}
};
const STACK_LENGTH = 200;
let debug: boolean | undefined;
const stringifyStack = (stack: string | undefined): string => {
Iif (!stack) {
return "";
}
debug ??= getFromEnv("DEBUG", z.boolean().default(false));
Iif (debug) {
return stack;
}
const suffix = stack.length > STACK_LENGTH ? "..." : "";
return stack.substring(0, STACK_LENGTH - suffix.length) + suffix;
};
export function stringifyError(e: unknown): string {
const error = e as
| AggregateError
| AxiosError
| undefined
| Error
| { toJSON: () => string };
Iif (error === undefined) {
return "undefined";
} else Iif (error instanceof AggregateError) {
const errorMessages: string[] = error.errors.map(stringifyError);
return `AggregateError: ${error.message}, errors: ${errorMessages.join(
"; "
)}`;
} else Iif (axios.isAxiosError(error)) {
return JSON.stringify(error.response?.data) + stringifyStack(error.stack);
} else if (error instanceof Error) {
return stringifyStack(error.stack);
} else Eif (typeof error.toJSON === "function") {
return JSON.stringify(error.toJSON());
} else {
return `Error couldn't be handled by the stringifyError function: ${String(
e
)}`;
}
}
|