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 statusCodefor 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:
Type Safety: The sealed interface ensures that all possible response types are handled.
Separation of Concerns: Clearly separates successful responses, API errors, and exceptions.
Flexibility: The extension functions allow for easy chaining of response handling logic.
Coroutine Support: Designed to work seamlessly with Kotlin coroutines.
Improved Error Handling: Provides detailed information about different types of failures.
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.
Error Handling Improvement
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:
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:
ApiResponse
.ApiResponse.Failure.Exception
3. Extension Functions for HttpResponse:
getStatusCode()
: Maps HTTP status codes to our custom StatusCode enum.payloadResponse
andstatusCode
for easier access to error details.4. HttpClient Extensions:
getApiResponse
: Simplifies making GET requests and handling responses.5. ApiResponse Extension Functions:
onSuccess
,onError
,onException
, etc.6. Error Handling:
Resource
class to use the newErrors
typeErrors
types.Benefits of This Approach:
One of Usage Example::
Important Note:
Thank you for your review and feedback.
205