-
-
Notifications
You must be signed in to change notification settings - Fork 0
Error‐Handling
Peter Araujo edited this page Jul 28, 2024
·
1 revision
Ktor HTTP client throws HTTP exceptions when an error occurs depending on the error type. So you would have to catch those errors when making requests and filter them out as needed.
-
RedirectResponseException
3xx responses. -
ClientRequestException
: 4xx responses. -
ServerResponseException
: 5xx responses.
Note: Additionally, catching network connectivity exceptions is recommended.
Example:
val service = ArtworkService()
try {
val result = service.getItems(limit = 2)
} catch (e: ClientRequestException) {
// Error handling logic...
}
Besides from the general exceptions, Artic API Client provides some api-specific exceptions.
-
ItemNotFoundException
: The item you requested cannot be found. -
InvalidLimitException
: Invalid limit parameter. -
InvalidNumberOfResultsException
: Requested number of results is out of valid range.
Example:
val service = ArtworkService()
try {
val result = service.getById(id = 1)
} catch (e: ItemNotFoundException) {
// Error handling logic...
}
Note: Please keep in mind that the api-specific exceptions are set up from the provided HttpClient
's configuration.
If you create your own client, you can use the provided one as reference to implement these exceptions.