(error: unknown, interceptorOptions?: unknown)
| 298 | } |
| 299 | |
| 300 | export function formatOrpcError(error: unknown, interceptorOptions?: unknown): FormattedOrpcError { |
| 301 | try { |
| 302 | const requestContext = extractRequestContext(interceptorOptions); |
| 303 | const where = |
| 304 | requestContext.method && requestContext.path |
| 305 | ? `${requestContext.method} ${requestContext.path}` |
| 306 | : (requestContext.path ?? requestContext.url); |
| 307 | |
| 308 | const whereSuffix = where ? ` ${where}` : ""; |
| 309 | |
| 310 | const debugDump: Record<string, unknown> = { |
| 311 | request: requestContext, |
| 312 | }; |
| 313 | |
| 314 | if (error instanceof ORPCError) { |
| 315 | const code = typeof error.code === "string" ? error.code : String(error.code); |
| 316 | const status = typeof error.status === "number" ? error.status : undefined; |
| 317 | |
| 318 | debugDump.error = { |
| 319 | type: "ORPCError", |
| 320 | code, |
| 321 | status, |
| 322 | message: error.message, |
| 323 | data: toJsonSafe(error.data, { depth: 6, seen: new WeakSet() }), |
| 324 | stack: error.stack, |
| 325 | }; |
| 326 | |
| 327 | const validation = getValidationErrorInfo(error.cause); |
| 328 | if (validation) { |
| 329 | debugDump.cause = { |
| 330 | type: "ValidationError", |
| 331 | message: validation.message, |
| 332 | issues: toJsonSafe(validation.issues, { depth: 6, seen: new WeakSet() }), |
| 333 | data: toJsonSafe(validation.data, { depth: 6, seen: new WeakSet() }), |
| 334 | }; |
| 335 | |
| 336 | const lines: string[] = []; |
| 337 | lines.push( |
| 338 | `ORPC${whereSuffix}: ${code} ${error.message}${ |
| 339 | status !== undefined ? ` (status ${status})` : "" |
| 340 | }` |
| 341 | ); |
| 342 | |
| 343 | if (validation.issues.length > 0) { |
| 344 | const maxIssues = 5; |
| 345 | lines.push(`Validation issues (${validation.issues.length}):`); |
| 346 | |
| 347 | for (const issue of validation.issues.slice(0, maxIssues)) { |
| 348 | const path = formatIssuePath(issue.path); |
| 349 | lines.push(` - ${path}: ${issue.message}`); |
| 350 | } |
| 351 | |
| 352 | if (validation.issues.length > maxIssues) { |
| 353 | lines.push(` (+${validation.issues.length - maxIssues} more)`); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | lines.push(`Data: ${summarizeForLogs(validation.data, 1)}`); |
no test coverage detected