Skip to content

fix: add structured request context to all API error logs#337

Open
YizukiAme wants to merge 3 commits intoTHU-MAIC:mainfrom
YizukiAme:fix/323-api-error-logging
Open

fix: add structured request context to all API error logs#337
YizukiAme wants to merge 3 commits intoTHU-MAIC:mainfrom
YizukiAme:fix/323-api-error-logging

Conversation

@YizukiAme
Copy link
Copy Markdown
Contributor

@YizukiAme YizukiAme commented Mar 30, 2026

Summary

Replace generic log.error('Error:', error) messages across 23 API routes with structured, context-rich error logs for faster incident triage.

Closes #323

Changes

Pattern applied uniformly

// Before
log.error('Error:', error);

// After
log.error(`TTS generation failed [provider=${ttsProviderId ?? 'unknown'}, voice=${ttsVoice ?? 'unknown'}]:`, error);

What's included

  • Hoist key request params (let x: T | undefined) above try blocks so they're accessible in catch
  • Add createLogger imports to 4 routes that previously had no logging (classroom, classroom-media, generate-classroom/*)
  • Truncate user-controlled strings (.substring(0, 60) / .substring(0, 100)) to prevent log flooding
  • No behavioral changesapiError() responses are untouched; only log messages improved

Affected routes (23 files)

Category Routes
Generation generate/image, generate/video, generate/tts, generate/agent-profiles, generate/scene-actions, generate/scene-content, generate/scene-outlines-stream
Classroom classroom, classroom-media, generate-classroom, generate-classroom/[jobId]
Chat chat, pbl/chat
Verification verify-model, verify-image-provider, verify-video-provider, verify-pdf-provider
Other azure-voices, parse-pdf, proxy-media, quiz-grade, transcription, web-search

Context logged per route

Each error log now includes the most relevant request parameters for that endpoint:

  • Provider/model identifiers — for provider-related routes
  • Input summaries — truncated requirement text, query strings, question snippets
  • Resource identifiers — audioId, classroomId, jobId, stageId

Testing

  • Purely observability changes — no runtime behavior modified
  • All existing error responses (apiError(...)) unchanged
  • Verified no new imports break the build

As the changes are largely mechanical, all code edits in this PR were made by Claude Opus 4.6, but have been manually audited and tested.

Replace generic log.error messages across 23 API routes with structured, context-rich error logs that include provider, model, input summary, and other request parameters for faster incident triage.

- Hoist key request params above try blocks for catch-block access

- Add createLogger imports to routes that lacked logging

- Truncate user-controlled strings (query, requirement) to prevent log flooding

- No behavioral changes — purely observability improvement

Closes THU-MAIC#323
@YizukiAme
Copy link
Copy Markdown
Contributor Author

YizukiAme commented Mar 30, 2026

Test Results — Before / After Log Comparison

Tested by sending deliberately invalid credentials/inputs to 5 API routes, capturing server-side log.error() output from the dev server.

Before (original generic messages)

[ERROR] [Verify Model] API test error: AI_APICallError: Incorrect API key provided ...
[ERROR] [Azure Voices] API error: TypeError: fetch failed
[ERROR] [WebSearch] [WebSearch] Error: Error: Tavily API error (401): { "error": "Unauthorized" }
[ERROR] [PBL Chat] Error: TypeError: Cannot read properties of undefined ...
[ERROR] [Transcription] Transcription error: TypeError: Content-Type was not one of ...

After (structured context logs from this PR)

[2026-03-30T21:08:23Z] [ERROR] [Verify Model] Model verification failed [model="gpt-999-turbo-fake"]: AI_APICallError: Incorrect API key provided: sk-INVAL**TING.
[2026-03-30T21:08:24Z] [ERROR] [Azure Voices] Azure voices fetch failed [baseUrl="https://bad-endpoint.invalid/cognitiveservices/v1"]: TypeError: fetch failed
[2026-03-30T21:08:26Z] [ERROR] [WebSearch] Web search failed [query="test query for error logging demonstration"]: Error: Tavily API error (401): { "error": "Unauthorized: missing or invalid API key." }
[2026-03-30T21:08:27Z] [ERROR] [PBL Chat] PBL chat failed [agent="TestAgent", type=question]: TypeError: Cannot read properties of undefined (reading 'length')
[2026-03-30T21:08:28Z] [ERROR] [Transcription] Transcription failed [provider=unknown, model=default]: TypeError: Content-Type was not one of "multipart/form-data" or "application/x-www-form-urlencoded".

Key improvements visible in logs

Route Before After
verify-model No model name model="gpt-999-turbo-fake"
azure-voices No URL baseUrl="https://bad-endpoint.invalid/..."
web-search Duplicate prefix, no query query="test query for error logging..."
pbl/chat Error: only agent="TestAgent", type=question
transcription Transcription error provider=unknown, model=default

Note: provider=unknown in Transcription is correct — the test intentionally sent JSON instead of multipart/form-data, so req.formData() threw before resolvedProviderId was ever assigned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve API error logging with request context

1 participant