MCPcopy Create free account
hub / github.com/anomalyco/opencode / formatErrorChain

Function formatErrorChain

packages/app/src/pages/error.tsx:148–212  ·  view source on GitHub ↗
(error: unknown, t: Translator, depth = 0, parentMessage?: string)

Source from the content-addressed store, hash-verified

146}
147
148function formatErrorChain(error: unknown, t: Translator, depth = 0, parentMessage?: string): string {
149 const json = (value: unknown) => safeJson(value, t("error.page.circular"))
150 if (!error) return t("error.chain.unknown")
151
152 if (isInitError(error)) {
153 const message = formatInitError(error, t)
154 if (depth > 0 && parentMessage === message) return ""
155 const indent = depth > 0 ? `\n${CHAIN_SEPARATOR}${t("error.chain.causedBy")}\n` : ""
156 return indent + `${error.name}\n${message}`
157 }
158
159 if (error instanceof Error) {
160 const isDuplicate = depth > 0 && parentMessage === error.message
161 const parts: string[] = []
162 const indent = depth > 0 ? `\n${CHAIN_SEPARATOR}${t("error.chain.causedBy")}\n` : ""
163
164 const header = `${error.name}${error.message ? `: ${error.message}` : ""}`
165 const stack = error.stack?.trim()
166
167 if (stack) {
168 const startsWithHeader = stack.startsWith(header)
169
170 if (isDuplicate && startsWithHeader) {
171 const trace = stack.split("\n").slice(1).join("\n").trim()
172 if (trace) {
173 parts.push(indent + trace)
174 }
175 }
176
177 if (isDuplicate && !startsWithHeader) {
178 parts.push(indent + stack)
179 }
180
181 if (!isDuplicate && startsWithHeader) {
182 parts.push(indent + stack)
183 }
184
185 if (!isDuplicate && !startsWithHeader) {
186 parts.push(indent + `${header}\n${stack}`)
187 }
188 }
189
190 if (!stack && !isDuplicate) {
191 parts.push(indent + header)
192 }
193
194 if (error.cause) {
195 const causeResult = formatErrorChain(error.cause, t, depth + 1, error.message)
196 if (causeResult) {
197 parts.push(causeResult)
198 }
199 }
200
201 return parts.join("\n\n")
202 }
203
204 if (typeof error === "string") {
205 if (depth > 0 && parentMessage === error) return ""

Callers 1

formatErrorFunction · 0.85

Calls 5

isInitErrorFunction · 0.85
formatInitErrorFunction · 0.85
pushMethod · 0.80
jsonFunction · 0.70
tFunction · 0.50

Tested by

no test coverage detected