MCPcopy Index your code
hub / github.com/anomalyco/opencode / errorFormat

Function errorFormat

packages/tui/src/util/error.ts:97–123  ·  view source on GitHub ↗
(error: unknown)

Source from the content-addressed store, hash-verified

95}
96
97export function errorFormat(error: unknown): string {
98 if (error instanceof Error) {
99 return error.stack ?? `${error.name}: ${error.message}`
100 }
101
102 if (typeof error === "object" && error !== null) {
103 try {
104 const json = JSON.stringify(error, null, 2)
105 // Plain objects whose own properties are all non-enumerable (or empty)
106 // serialize to "{}", which prints as a useless bare `{}` on stderr.
107 // Fall back to a custom toString first, then to ctor name + own prop names.
108 if (json === "{}") {
109 const str = String(error)
110 if (str && str !== "[object Object]") return str
111 const ctor = error.constructor?.name
112 const prefix = ctor && ctor !== "Object" ? ctor : "Error"
113 const names = Object.getOwnPropertyNames(error)
114 return names.length === 0 ? `${prefix} (no message)` : `${prefix} { ${names.join(", ")} }`
115 }
116 return json
117 } catch {
118 return "Unexpected error (unserializable)"
119 }
120 }
121
122 return String(error)
123}
124
125export function errorMessage(error: unknown): string {
126 if (error instanceof Error) {

Callers 5

error.test.tsFile · 0.90
app.tsxFile · 0.90
FormatUnknownErrorFunction · 0.90
errorMessageFunction · 0.85
errorDataFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected