Skip to content

Troubleshooting 500 errors ​

When the API returns a 500 (or 503/504), every response carries an errorId that is logged server-side on the line with the stack trace. Use it to jump from a client failure to the exact server log entry.

The envelope ​

Every 5xx JSON response includes:

json
{
  "message": "internal server error",
  "errorId": "7c1f9f34-0f2e-4c5b-9f2d-2dabc5f1bf6e",
  "traceId": "a3e5e2...optional..."
}

The same identifier is echoed back on the response headers:

  • X-Request-Id — always set
  • X-Trace-Id — set when an upstream x-cloud-trace-context or traceparent was present

Finding the errorId ​

  • Browser: open DevTools → Network → click the failing request → Response tab shows the JSON body. errorId is also under the Headers tab as X-Request-Id.

  • Client code: a 500/503/504 surfaces as RpcServerError:

    ts
    import { RpcServerError } from '@iwentsourcing/core/error/rpc-server-error.js';
    
    try {
      await client['event.push'].call(args, signal);
    } catch (err) {
      if (err instanceof RpcServerError) {
        console.error(`server error ${err.statusCode}: ${err.errorId}`);
      }
      throw err;
    }
  • curl: curl -i prints the X-Request-Id header and JSON body.

Local ​

The server auto-switches to JSON logs when stdout is not a TTY or when LOG_FORMAT=json is set. For a human-readable view locally:

sh
# pretty output (default when run from a terminal)
npx aion serve src/export.ts

# structured output piped through jq
LOG_FORMAT=json npx aion serve src/export.ts | jq

Find the log line that produced a specific error:

sh
LOG_FORMAT=json npx aion serve src/export.ts \
  | jq -c 'select(.requestId == "7c1f9f34-0f2e-4c5b-9f2d-2dabc5f1bf6e")'

In pretty mode the request id is appended to the prefix as [req=<first-8-chars>].

Cloud Run ​

Cloud Run sets K_SERVICE automatically. When K_SERVICE or GOOGLE_CLOUD_PROJECT is set, api/src/index.ts registers:

  • The Google Cloud trace parser (reads x-cloud-trace-context).
  • The Google Cloud log enricher (adds logging.googleapis.com/trace).

For a different host (Azure, AWS, …), register a different TraceContextParser and LogEnricher instead. The core logger and request-context middleware have no infra-specific code.

Cloud Logging will pick up:

  • severity — ERROR, WARNING, INFO, DEBUG
  • logging.googleapis.com/trace — request trace, when the GCP log enricher is enabled and an upstream x-cloud-trace-context header was received
  • jsonPayload.requestId / jsonPayload.errorId

Logs Explorer ​

resource.type="cloud_run_revision"
jsonPayload.errorId="<paste errorId>"

gcloud ​

sh
gcloud logging read \
  'resource.type="cloud_run_revision" AND jsonPayload.errorId="<id>"' \
  --limit=50 --format=json

Worked example ​

  1. Client sees 500 — response body {"message":"internal server error","errorId":"7c1f...","traceId":"..."}.
  2. Copy errorId.
  3. In Logs Explorer: jsonPayload.errorId="7c1f..." → one line with severity=ERROR, data.stack containing the full stack.
  4. Click the request trace icon to see every log line emitted during that request.

OpenTelemetry ​

Tracing is off by default. Set OTEL_EXPORTER_OTLP_ENDPOINT to enable:

sh
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318 \
  npx aion serve src/export.ts

Optional overrides:

  • OTEL_EXPORTER_OTLP_TRACES_ENDPOINT / OTEL_EXPORTER_OTLP_METRICS_ENDPOINT if the collector routes traces and metrics to different endpoints.
  • OTEL_SERVICE_NAME to override the default iwentsourcing-api.