MCPcopy Create free account
hub / github.com/bowencool/blog / error2String

Function error2String

src/utils/error2String.ts:3–19  ·  view source on GitHub ↗
(err: unknown)

Source from the content-addressed store, hash-verified

1import { isPlainObject } from "./plainObject";
2
3export function error2String(err: unknown): string {
4 if (!err) return "";
5 if (typeof err === "string") {
6 return err;
7 }
8 if (err instanceof Error) {
9 return `${err.name}: ${err.message}`;
10 }
11 if (isPlainObject(err)) {
12 const key = ["message", "msg", "error", "err_msg", "error_message"];
13 for (const k of key) {
14 const v = err[k];
15 if (typeof v === "string") return v;
16 }
17 }
18 return JSON.stringify(err);
19}

Callers

nothing calls this directly

Calls 1

isPlainObjectFunction · 0.90

Tested by

no test coverage detected