(
c: Context<TEnv>,
error: Error,
status: number,
timestamp: string,
)
| 402 | } |
| 403 | |
| 404 | async function createServerErrorEnvelope<TEnv extends ErrorHandlerEnv>( |
| 405 | c: Context<TEnv>, |
| 406 | error: Error, |
| 407 | status: number, |
| 408 | timestamp: string, |
| 409 | ): Promise<ServerErrorEnvelope> { |
| 410 | const vars = c.var as Partial<{ |
| 411 | auth: { |
| 412 | user?: { id?: string; tier?: string }; |
| 413 | apiKey?: { id?: string }; |
| 414 | }; |
| 415 | model: { |
| 416 | requested: string; |
| 417 | resolved: string; |
| 418 | }; |
| 419 | requestStartedAt: number; |
| 420 | }>; |
| 421 | const message = |
| 422 | truncateString( |
| 423 | error.message || getDefaultErrorMessage(status), |
| 424 | MAX_ERROR_MESSAGE_LENGTH, |
| 425 | ) || getDefaultErrorMessage(status); |
| 426 | const stack = truncateString(error.stack, MAX_STACK_LENGTH); |
| 427 | const resolvedRoutePath = getRoutePath(c); |
| 428 | |
| 429 | return { |
| 430 | kind: "server_error", |
| 431 | severity: "error", |
| 432 | timestamp, |
| 433 | requestId: c.get("requestId"), |
| 434 | environment: c.env.ENVIRONMENT, |
| 435 | routePath: resolvedRoutePath, |
| 436 | method: c.req.method, |
| 437 | status, |
| 438 | durationMs: |
| 439 | vars.requestStartedAt === undefined |
| 440 | ? undefined |
| 441 | : Date.now() - vars.requestStartedAt, |
| 442 | errorCode: getErrorCode(status), |
| 443 | errorClass: error.name, |
| 444 | message, |
| 445 | stack, |
| 446 | upstreamHost: |
| 447 | error instanceof UpstreamError |
| 448 | ? error.requestUrl?.hostname |
| 449 | : undefined, |
| 450 | upstreamStatus: |
| 451 | error instanceof UpstreamError ? error.upstreamStatus : undefined, |
| 452 | upstreamBody: |
| 453 | error instanceof UpstreamError |
| 454 | ? truncateString(error.responseBody, MAX_UPSTREAM_BODY_LENGTH) |
| 455 | : undefined, |
| 456 | modelRequested: vars.model?.requested, |
| 457 | resolvedModelRequested: vars.model?.resolved, |
| 458 | requestInputs: await collectRequestInputs(c), |
| 459 | userId: vars.auth?.user?.id, |
| 460 | userTier: vars.auth?.user?.tier, |
| 461 | apiKeyId: vars.auth?.apiKey?.id, |
no test coverage detected