Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance/error handling #219

Merged
merged 24 commits into from
Sep 2, 2024
Merged

Conversation

mhmd-android
Copy link
Contributor

@mhmd-android mhmd-android commented Aug 31, 2024

Error Handling Improvement

Hi, I'm back with a new PR :). This PR primarily aims to enhance error handling across the project, but it also introduces significant improvements and refactoring to the overall code structure.

One of the most significant improvements in this PR is the introduction of a robust and flexible API response handling system using Ktor. This new system provides a more structured and type-safe way to manage API responses and errors.

Key Components:

1. ApiResponse Sealed Interface:

  • This is the core of our new response handling system.
  • It defines three main types of responses: a. Success<T>: Represents successful API calls with data. b. Failure.Error: Represents API errors (e.g., server errors). c. Failure.Exception: Represents client-side exceptions (e.g., network issues).

2. apiResponseOf Function:

  • A suspend function that wraps API calls and returns an ApiResponse.
  • Automatically categorizes responses based on status codes.
  • Catches exceptions and wraps them in ApiResponse.Failure.Exception

3. Extension Functions for HttpResponse:

  • getStatusCode(): Maps HTTP status codes to our custom StatusCode enum.
  • Properties like payloadResponseand statusCode for easier access to error details.

4. HttpClient Extensions:

  • getApiResponse: Simplifies making GET requests and handling responses.

5. ApiResponse Extension Functions:

  • A set of functions like onSuccess, onError, onException, etc.
  • Allows for more functional and readable API response handling.
  • Includes both synchronous and suspend versions for flexibility.

6. Error Handling:

  • Created a new Errors sealed class to categorize different types of errors.
  • Updated Resource class to use the new Errors type
  • Implemented error mapping in data layer to convert exceptions to appropriate Errors types.

Benefits of This Approach:

  1. Type Safety: The sealed interface ensures that all possible response types are handled.
  2. Separation of Concerns: Clearly separates successful responses, API errors, and exceptions.
  3. Flexibility: The extension functions allow for easy chaining of response handling logic.
  4. Coroutine Support: Designed to work seamlessly with Kotlin coroutines.
  5. Improved Error Handling: Provides detailed information about different types of failures.

One of Usage Example::

client.getApiResponse<UserData> {
    url("https://api.example.com/user")
}.onSuccess { 
    // Handle successful response
}.onError { 
    // Handle API errors
}.onException {
    // Handle exceptions
}

This new system replaces the previous approach of handling Ktor responses with individual functions, providing a more cohesive and maintainable solution. It significantly improves error handling capabilities and makes API interactions more predictable and easier to manage throughout the application.

Important Note:

  • Despite substantial improvements, more work is needed on the UI side for proper error display.

Thank you for your review and feedback.
#205

…lass

This commit includes significant changes to error handling and API response management:

1. Removed Resource class from domain.model
2. Introduced a new Resource sealed class in the ktor module:
   - Generic type parameters for both success (T) and error (E) types
   - Success and Error as data classes for better pattern matching
3. Added a new Errors sealed class in the ktor module:
   - ApiError for API-specific errors with message and code
   - ExceptionError for general exceptions with message and optional throwable
…`MarketsApiImpl` to use ApiResponse and getApiResponse
@Kaaveh Kaaveh self-requested a review September 1, 2024 01:57
core/ktor/src/main/AndroidManifest.xml Outdated Show resolved Hide resolved
core/ktor/build.gradle.kts Outdated Show resolved Hide resolved
@Kaaveh Kaaveh added the enhancement New feature or request label Sep 1, 2024
core/base/build.gradle.kts Outdated Show resolved Hide resolved
core/ktor/build.gradle.kts Outdated Show resolved Hide resolved
core/ktor/src/main/java/ir/composenews/ktor/ApiResponse.kt Outdated Show resolved Hide resolved
@Kaaveh
Copy link
Owner

Kaaveh commented Sep 2, 2024

@VahidGarousi are we ready to merge the PR?

@VahidGarousi VahidGarousi merged commit ff93337 into Kaaveh:kmp Sep 2, 2024
1 of 2 checks passed
@mhmd-android mhmd-android deleted the enhance/error-handling branch September 6, 2024 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants