Skip to content

Commit ef3bc1b

Browse files
committed
chore(Willhaben): handle request errors gracefully (#29)
Fixes #29
1 parent dcdf630 commit ef3bc1b

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "3.4.0-rc.2"
1+
version = "3.4.0-rc.3"

integration/willhaben/src/main/kotlin/me/snoty/integration/contrib/willhaben/api/WillhabenAPI.kt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ class WillhabenAPIImpl(private val httpClient: HttpClient) : WillhabenAPI, KoinC
8282

8383
logger.debug { "Redirected to $newUrl" }
8484
mappedUrl = newUrl
85+
} catch (ex: ClientRequestException) {
86+
when (ex.response.status) {
87+
HttpStatusCode.NotFound, HttpStatusCode.Gone -> {
88+
logger.warn { "Listing $url is expired" }
89+
return null
90+
}
91+
HttpStatusCode.Forbidden -> throw WillhabenBlockedException()
92+
else -> throw WillhabenRequestException(ex)
93+
}
8594
}
8695
}
8796

@@ -92,7 +101,14 @@ class WillhabenAPIImpl(private val httpClient: HttpClient) : WillhabenAPI, KoinC
92101
}
93102

94103
override suspend fun fetchWishlist(creds: WillhabenCredentials, cleanTitle: Boolean): WillhabenWishlist {
95-
val response = httpClient.getAuthenticated(WISHLIST_PATH, creds)
104+
val response = try {
105+
httpClient.getAuthenticated(WISHLIST_PATH, creds)
106+
} catch (ex: ClientRequestException) {
107+
when (ex.response.status) {
108+
HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden -> throw WillhabenBlockedException()
109+
else -> throw WillhabenRequestException(ex)
110+
}
111+
}
96112

97113
val props = response.parseNextPageProps(json)
98114
val listings = props
@@ -114,7 +130,14 @@ class WillhabenAPIImpl(private val httpClient: HttpClient) : WillhabenAPI, KoinC
114130
appendEncodedPathSegments("iad", query)
115131
}.build()
116132

117-
val response = httpClient.get(url)
133+
val response = try {
134+
httpClient.get(url)
135+
} catch (ex: ClientRequestException) {
136+
when (ex.response.status) {
137+
HttpStatusCode.Forbidden -> throw WillhabenBlockedException()
138+
else -> throw WillhabenRequestException(ex)
139+
}
140+
}
118141

119142
val props = response.parseNextPageProps(json)
120143
val searchResults = props
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package me.snoty.integration.contrib.willhaben.api
2+
3+
import io.ktor.client.plugins.*
4+
5+
class WillhabenBlockedException : Exception("Access to Willhaben was blocked. Try again later or use authentication.")
6+
class WillhabenRequestException(cause: ClientRequestException) : Exception("Error while accessing Willhaben: ${cause.response.status}", cause)

0 commit comments

Comments
 (0)