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:
{
"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 setX-Trace-Id— set when an upstreamx-cloud-trace-contextortraceparentwas present
Finding the errorId
Browser: open DevTools → Network → click the failing request → Response tab shows the JSON body.
errorIdis also under the Headers tab asX-Request-Id.Client code: a 500/503/504 surfaces as
RpcServerError:tsimport { 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 -iprints theX-Request-Idheader 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:
# 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 | jqFind the log line that produced a specific error:
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,DEBUGlogging.googleapis.com/trace— request trace, when the GCP log enricher is enabled and an upstreamx-cloud-trace-contextheader was receivedjsonPayload.requestId/jsonPayload.errorId
Logs Explorer
resource.type="cloud_run_revision"
jsonPayload.errorId="<paste errorId>"gcloud
gcloud logging read \
'resource.type="cloud_run_revision" AND jsonPayload.errorId="<id>"' \
--limit=50 --format=jsonWorked example
- Client sees
500— response body{"message":"internal server error","errorId":"7c1f...","traceId":"..."}. - Copy
errorId. - In Logs Explorer:
jsonPayload.errorId="7c1f..."→ one line withseverity=ERROR,data.stackcontaining the full stack. - 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:
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318 \
npx aion serve src/export.tsOptional overrides:
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT/OTEL_EXPORTER_OTLP_METRICS_ENDPOINTif the collector routes traces and metrics to different endpoints.OTEL_SERVICE_NAMEto override the defaultiwentsourcing-api.