Skip to content

Commit 14ab485

Browse files
fix(client): ensure error handling always occurs
1 parent 830c9d9 commit 14ab485

58 files changed

Lines changed: 1351 additions & 723 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

orb-java-core/src/main/kotlin/com/withorb/api/core/handlers/ErrorHandler.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import com.withorb.api.errors.UnexpectedStatusCodeException
1919
import com.withorb.api.errors.UnprocessableEntityException
2020

2121
@JvmSynthetic
22-
internal fun errorHandler(jsonMapper: JsonMapper): Handler<JsonValue> {
22+
internal fun errorBodyHandler(jsonMapper: JsonMapper): Handler<JsonValue> {
2323
val handler = jsonHandler<JsonValue>(jsonMapper)
2424

2525
return object : Handler<JsonValue> {
@@ -33,52 +33,52 @@ internal fun errorHandler(jsonMapper: JsonMapper): Handler<JsonValue> {
3333
}
3434

3535
@JvmSynthetic
36-
internal fun <T> Handler<T>.withErrorHandler(errorHandler: Handler<JsonValue>): Handler<T> =
37-
object : Handler<T> {
38-
override fun handle(response: HttpResponse): T =
36+
internal fun errorHandler(errorBodyHandler: Handler<JsonValue>): Handler<HttpResponse> =
37+
object : Handler<HttpResponse> {
38+
override fun handle(response: HttpResponse): HttpResponse =
3939
when (val statusCode = response.statusCode()) {
40-
in 200..299 -> this@withErrorHandler.handle(response)
40+
in 200..299 -> response
4141
400 ->
4242
throw BadRequestException.builder()
4343
.headers(response.headers())
44-
.body(errorHandler.handle(response))
44+
.body(errorBodyHandler.handle(response))
4545
.build()
4646
401 ->
4747
throw UnauthorizedException.builder()
4848
.headers(response.headers())
49-
.body(errorHandler.handle(response))
49+
.body(errorBodyHandler.handle(response))
5050
.build()
5151
403 ->
5252
throw PermissionDeniedException.builder()
5353
.headers(response.headers())
54-
.body(errorHandler.handle(response))
54+
.body(errorBodyHandler.handle(response))
5555
.build()
5656
404 ->
5757
throw NotFoundException.builder()
5858
.headers(response.headers())
59-
.body(errorHandler.handle(response))
59+
.body(errorBodyHandler.handle(response))
6060
.build()
6161
422 ->
6262
throw UnprocessableEntityException.builder()
6363
.headers(response.headers())
64-
.body(errorHandler.handle(response))
64+
.body(errorBodyHandler.handle(response))
6565
.build()
6666
429 ->
6767
throw RateLimitException.builder()
6868
.headers(response.headers())
69-
.body(errorHandler.handle(response))
69+
.body(errorBodyHandler.handle(response))
7070
.build()
7171
in 500..599 ->
7272
throw InternalServerException.builder()
7373
.statusCode(statusCode)
7474
.headers(response.headers())
75-
.body(errorHandler.handle(response))
75+
.body(errorBodyHandler.handle(response))
7676
.build()
7777
else ->
7878
throw UnexpectedStatusCodeException.builder()
7979
.statusCode(statusCode)
8080
.headers(response.headers())
81-
.body(errorHandler.handle(response))
81+
.body(errorBodyHandler.handle(response))
8282
.build()
8383
}
8484
}

orb-java-core/src/main/kotlin/com/withorb/api/services/async/AlertServiceAsyncImpl.kt

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
package com.withorb.api.services.async
44

55
import com.withorb.api.core.ClientOptions
6-
import com.withorb.api.core.JsonValue
76
import com.withorb.api.core.RequestOptions
87
import com.withorb.api.core.checkRequired
8+
import com.withorb.api.core.handlers.errorBodyHandler
99
import com.withorb.api.core.handlers.errorHandler
1010
import com.withorb.api.core.handlers.jsonHandler
11-
import com.withorb.api.core.handlers.withErrorHandler
1211
import com.withorb.api.core.http.HttpMethod
1312
import com.withorb.api.core.http.HttpRequest
13+
import com.withorb.api.core.http.HttpResponse
1414
import com.withorb.api.core.http.HttpResponse.Handler
1515
import com.withorb.api.core.http.HttpResponseFor
1616
import com.withorb.api.core.http.json
@@ -102,7 +102,8 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
102102
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
103103
AlertServiceAsync.WithRawResponse {
104104

105-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
105+
private val errorHandler: Handler<HttpResponse> =
106+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
106107

107108
override fun withOptions(
108109
modifier: Consumer<ClientOptions.Builder>
@@ -111,8 +112,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
111112
clientOptions.toBuilder().apply(modifier::accept).build()
112113
)
113114

114-
private val retrieveHandler: Handler<Alert> =
115-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
115+
private val retrieveHandler: Handler<Alert> = jsonHandler<Alert>(clientOptions.jsonMapper)
116116

117117
override fun retrieve(
118118
params: AlertRetrieveParams,
@@ -132,7 +132,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
132132
return request
133133
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
134134
.thenApply { response ->
135-
response.parseable {
135+
errorHandler.handle(response).parseable {
136136
response
137137
.use { retrieveHandler.handle(it) }
138138
.also {
@@ -144,8 +144,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
144144
}
145145
}
146146

147-
private val updateHandler: Handler<Alert> =
148-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
147+
private val updateHandler: Handler<Alert> = jsonHandler<Alert>(clientOptions.jsonMapper)
149148

150149
override fun update(
151150
params: AlertUpdateParams,
@@ -166,7 +165,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
166165
return request
167166
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
168167
.thenApply { response ->
169-
response.parseable {
168+
errorHandler.handle(response).parseable {
170169
response
171170
.use { updateHandler.handle(it) }
172171
.also {
@@ -180,7 +179,6 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
180179

181180
private val listHandler: Handler<AlertListPageResponse> =
182181
jsonHandler<AlertListPageResponse>(clientOptions.jsonMapper)
183-
.withErrorHandler(errorHandler)
184182

185183
override fun list(
186184
params: AlertListParams,
@@ -197,7 +195,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
197195
return request
198196
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
199197
.thenApply { response ->
200-
response.parseable {
198+
errorHandler.handle(response).parseable {
201199
response
202200
.use { listHandler.handle(it) }
203201
.also {
@@ -218,7 +216,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
218216
}
219217

220218
private val createForCustomerHandler: Handler<Alert> =
221-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
219+
jsonHandler<Alert>(clientOptions.jsonMapper)
222220

223221
override fun createForCustomer(
224222
params: AlertCreateForCustomerParams,
@@ -239,7 +237,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
239237
return request
240238
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
241239
.thenApply { response ->
242-
response.parseable {
240+
errorHandler.handle(response).parseable {
243241
response
244242
.use { createForCustomerHandler.handle(it) }
245243
.also {
@@ -252,7 +250,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
252250
}
253251

254252
private val createForExternalCustomerHandler: Handler<Alert> =
255-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
253+
jsonHandler<Alert>(clientOptions.jsonMapper)
256254

257255
override fun createForExternalCustomer(
258256
params: AlertCreateForExternalCustomerParams,
@@ -273,7 +271,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
273271
return request
274272
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
275273
.thenApply { response ->
276-
response.parseable {
274+
errorHandler.handle(response).parseable {
277275
response
278276
.use { createForExternalCustomerHandler.handle(it) }
279277
.also {
@@ -286,7 +284,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
286284
}
287285

288286
private val createForSubscriptionHandler: Handler<Alert> =
289-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
287+
jsonHandler<Alert>(clientOptions.jsonMapper)
290288

291289
override fun createForSubscription(
292290
params: AlertCreateForSubscriptionParams,
@@ -307,7 +305,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
307305
return request
308306
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
309307
.thenApply { response ->
310-
response.parseable {
308+
errorHandler.handle(response).parseable {
311309
response
312310
.use { createForSubscriptionHandler.handle(it) }
313311
.also {
@@ -319,8 +317,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
319317
}
320318
}
321319

322-
private val disableHandler: Handler<Alert> =
323-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
320+
private val disableHandler: Handler<Alert> = jsonHandler<Alert>(clientOptions.jsonMapper)
324321

325322
override fun disable(
326323
params: AlertDisableParams,
@@ -341,7 +338,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
341338
return request
342339
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
343340
.thenApply { response ->
344-
response.parseable {
341+
errorHandler.handle(response).parseable {
345342
response
346343
.use { disableHandler.handle(it) }
347344
.also {
@@ -353,8 +350,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
353350
}
354351
}
355352

356-
private val enableHandler: Handler<Alert> =
357-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
353+
private val enableHandler: Handler<Alert> = jsonHandler<Alert>(clientOptions.jsonMapper)
358354

359355
override fun enable(
360356
params: AlertEnableParams,
@@ -375,7 +371,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
375371
return request
376372
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
377373
.thenApply { response ->
378-
response.parseable {
374+
errorHandler.handle(response).parseable {
379375
response
380376
.use { enableHandler.handle(it) }
381377
.also {

orb-java-core/src/main/kotlin/com/withorb/api/services/async/BetaServiceAsyncImpl.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
package com.withorb.api.services.async
44

55
import com.withorb.api.core.ClientOptions
6-
import com.withorb.api.core.JsonValue
76
import com.withorb.api.core.RequestOptions
87
import com.withorb.api.core.checkRequired
8+
import com.withorb.api.core.handlers.errorBodyHandler
99
import com.withorb.api.core.handlers.errorHandler
1010
import com.withorb.api.core.handlers.jsonHandler
11-
import com.withorb.api.core.handlers.withErrorHandler
1211
import com.withorb.api.core.http.HttpMethod
1312
import com.withorb.api.core.http.HttpRequest
13+
import com.withorb.api.core.http.HttpResponse
1414
import com.withorb.api.core.http.HttpResponse.Handler
1515
import com.withorb.api.core.http.HttpResponseFor
1616
import com.withorb.api.core.http.json
@@ -69,7 +69,8 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
6969
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
7070
BetaServiceAsync.WithRawResponse {
7171

72-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
72+
private val errorHandler: Handler<HttpResponse> =
73+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
7374

7475
private val externalPlanId: ExternalPlanIdServiceAsync.WithRawResponse by lazy {
7576
ExternalPlanIdServiceAsyncImpl.WithRawResponseImpl(clientOptions)
@@ -85,7 +86,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
8586
override fun externalPlanId(): ExternalPlanIdServiceAsync.WithRawResponse = externalPlanId
8687

8788
private val createPlanVersionHandler: Handler<PlanVersion> =
88-
jsonHandler<PlanVersion>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
89+
jsonHandler<PlanVersion>(clientOptions.jsonMapper)
8990

9091
override fun createPlanVersion(
9192
params: BetaCreatePlanVersionParams,
@@ -106,7 +107,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
106107
return request
107108
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
108109
.thenApply { response ->
109-
response.parseable {
110+
errorHandler.handle(response).parseable {
110111
response
111112
.use { createPlanVersionHandler.handle(it) }
112113
.also {
@@ -119,7 +120,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
119120
}
120121

121122
private val fetchPlanVersionHandler: Handler<PlanVersion> =
122-
jsonHandler<PlanVersion>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
123+
jsonHandler<PlanVersion>(clientOptions.jsonMapper)
123124

124125
override fun fetchPlanVersion(
125126
params: BetaFetchPlanVersionParams,
@@ -144,7 +145,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
144145
return request
145146
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
146147
.thenApply { response ->
147-
response.parseable {
148+
errorHandler.handle(response).parseable {
148149
response
149150
.use { fetchPlanVersionHandler.handle(it) }
150151
.also {
@@ -157,7 +158,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
157158
}
158159

159160
private val setDefaultPlanVersionHandler: Handler<Plan> =
160-
jsonHandler<Plan>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
161+
jsonHandler<Plan>(clientOptions.jsonMapper)
161162

162163
override fun setDefaultPlanVersion(
163164
params: BetaSetDefaultPlanVersionParams,
@@ -178,7 +179,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
178179
return request
179180
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
180181
.thenApply { response ->
181-
response.parseable {
182+
errorHandler.handle(response).parseable {
182183
response
183184
.use { setDefaultPlanVersionHandler.handle(it) }
184185
.also {

0 commit comments

Comments
 (0)