Skip to content

feat(network): enable HTTP error validation in Ktor HttpClient#122

Open
therajanmaurya wants to merge 1 commit intodevfrom
feat/enable-http-error-validation
Open

feat(network): enable HTTP error validation in Ktor HttpClient#122
therajanmaurya wants to merge 1 commit intodevfrom
feat/enable-http-error-validation

Conversation

@therajanmaurya
Copy link
Member

Summary

  • Add expectSuccess = true to the default HttpClient configuration
  • This enables Ktor's response validation which throws exceptions for non-2xx responses

Changes

  • ClientRequestException is thrown for 4xx responses
  • ServerResponseException is thrown for 5xx responses

Why

Without this setting, non-2xx responses are silently returned as successful responses, making proper error handling impossible. The response body would be attempted to be parsed as the success type, often leading to confusing deserialization errors instead of proper HTTP error handling.

Test plan

  • Verify 4xx responses throw ClientRequestException
  • Verify 5xx responses throw ServerResponseException
  • Existing API calls continue to work for 2xx responses

🤖 Generated with Claude Code

Add `expectSuccess = true` to the default HttpClient configuration.
This enables Ktor's response validation which throws:
- ClientRequestException for 4xx responses
- ServerResponseException for 5xx responses

Without this, non-2xx responses are silently returned as successful,
making proper error handling impossible.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
install(ContentNegotiation) {
json(jsonConfig)
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent application crashes when the API fails for reasons such as network issues or server problems, we must implement client-side error handling, preferably by using a try-catch block or a custom interceptor or a sealed interface/class with custom errors data classes with serializable fields

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.

2 participants