Skip to content

fix: include error response body and status code in FalException#7

Open
otopba wants to merge 1 commit intofal-ai:mainfrom
otopba:main
Open

fix: include error response body and status code in FalException#7
otopba wants to merge 1 commit intofal-ai:mainfrom
otopba:main

Conversation

@otopba
Copy link
Copy Markdown

@otopba otopba commented Apr 3, 2026

Summary

responseToException() in HttpClient parsed the JSON error body but silently discarded it, always creating FalException with only "Request failed with code: NNN". This lost valuable error details returned by the fal.ai API.

Before:

FalException: Request failed with code: 422

After:

FalException: Request failed with code: 422: The prompt may contain intellectual property references that cannot be processed.

Changes

  • FalException — added statusCode (int) and body (String) fields with backward-compatible constructors
  • HttpClient.responseToException() — now reads the response body and extracts human-readable messages from common error formats:
    • {"detail": [{"msg": "...", ...}]} — validation errors (messages joined with ; )
    • {"detail": "..."} — string detail
    • {"message": "..."} — generic error message
    • {"error": "..."} — error string
  • HttpClient.extractDetailMessage() — safe JSON parsing with fallback (never throws)
  • 23 unit tests covering all parsing paths, edge cases (invalid JSON, null fields, non-string primitives, empty arrays), backward compatibility, and requestId preservation

Backward Compatibility

  • All existing FalException constructors preserved unchanged
  • getMessage() still starts with "Request failed with code: NNN" — detail is appended after ": "
  • New fields default to -1 / null when using legacy constructors
  • No changes to client-async or client-kotlin modules (they use HttpClient internally)

Test plan

  • All 23 unit tests pass
  • All three modules (client, client-async, client-kotlin) compile successfully
  • Verified with JDK 17 (Corretto)

🤖 Generated with Claude Code

Previously, responseToException() parsed the JSON error body but
discarded it, creating FalException with only "Request failed with
code: NNN". This lost valuable error details from the API (validation
messages, error descriptions, etc.).

Changes:
- Extract human-readable messages from common error response formats:
  {"detail": [{"msg": "..."}]}, {"message": "..."}, {"error": "..."}
- Add statusCode and body fields to FalException for programmatic access
- Maintain full backward compatibility with existing constructors
- Add 23 unit tests covering all parsing paths and edge cases

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

1 participant