Skip to content

Commit 54cb075

Browse files
fix(client): ensure error handling always occurs
1 parent 54037dc commit 54cb075

File tree

58 files changed

+1351
-725
lines changed

Some content is hidden

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

58 files changed

+1351
-725
lines changed

orb-kotlin-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
@@ -16,7 +16,7 @@ import com.withorb.api.errors.UnauthorizedException
1616
import com.withorb.api.errors.UnexpectedStatusCodeException
1717
import com.withorb.api.errors.UnprocessableEntityException
1818

19-
internal fun errorHandler(jsonMapper: JsonMapper): Handler<JsonValue> {
19+
internal fun errorBodyHandler(jsonMapper: JsonMapper): Handler<JsonValue> {
2020
val handler = jsonHandler<JsonValue>(jsonMapper)
2121

2222
return object : Handler<JsonValue> {
@@ -29,52 +29,52 @@ internal fun errorHandler(jsonMapper: JsonMapper): Handler<JsonValue> {
2929
}
3030
}
3131

32-
internal fun <T> Handler<T>.withErrorHandler(errorHandler: Handler<JsonValue>): Handler<T> =
33-
object : Handler<T> {
34-
override fun handle(response: HttpResponse): T =
32+
internal fun errorHandler(errorBodyHandler: Handler<JsonValue>): Handler<HttpResponse> =
33+
object : Handler<HttpResponse> {
34+
override fun handle(response: HttpResponse): HttpResponse =
3535
when (val statusCode = response.statusCode()) {
36-
in 200..299 -> this@withErrorHandler.handle(response)
36+
in 200..299 -> response
3737
400 ->
3838
throw BadRequestException.builder()
3939
.headers(response.headers())
40-
.body(errorHandler.handle(response))
40+
.body(errorBodyHandler.handle(response))
4141
.build()
4242
401 ->
4343
throw UnauthorizedException.builder()
4444
.headers(response.headers())
45-
.body(errorHandler.handle(response))
45+
.body(errorBodyHandler.handle(response))
4646
.build()
4747
403 ->
4848
throw PermissionDeniedException.builder()
4949
.headers(response.headers())
50-
.body(errorHandler.handle(response))
50+
.body(errorBodyHandler.handle(response))
5151
.build()
5252
404 ->
5353
throw NotFoundException.builder()
5454
.headers(response.headers())
55-
.body(errorHandler.handle(response))
55+
.body(errorBodyHandler.handle(response))
5656
.build()
5757
422 ->
5858
throw UnprocessableEntityException.builder()
5959
.headers(response.headers())
60-
.body(errorHandler.handle(response))
60+
.body(errorBodyHandler.handle(response))
6161
.build()
6262
429 ->
6363
throw RateLimitException.builder()
6464
.headers(response.headers())
65-
.body(errorHandler.handle(response))
65+
.body(errorBodyHandler.handle(response))
6666
.build()
6767
in 500..599 ->
6868
throw InternalServerException.builder()
6969
.statusCode(statusCode)
7070
.headers(response.headers())
71-
.body(errorHandler.handle(response))
71+
.body(errorBodyHandler.handle(response))
7272
.build()
7373
else ->
7474
throw UnexpectedStatusCodeException.builder()
7575
.statusCode(statusCode)
7676
.headers(response.headers())
77-
.body(errorHandler.handle(response))
77+
.body(errorBodyHandler.handle(response))
7878
.build()
7979
}
8080
}

orb-kotlin-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
@@ -93,7 +93,8 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
9393
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
9494
AlertServiceAsync.WithRawResponse {
9595

96-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
96+
private val errorHandler: Handler<HttpResponse> =
97+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
9798

9899
override fun withOptions(
99100
modifier: (ClientOptions.Builder) -> Unit
@@ -102,8 +103,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
102103
clientOptions.toBuilder().apply(modifier).build()
103104
)
104105

105-
private val retrieveHandler: Handler<Alert> =
106-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
106+
private val retrieveHandler: Handler<Alert> = jsonHandler<Alert>(clientOptions.jsonMapper)
107107

108108
override suspend fun retrieve(
109109
params: AlertRetrieveParams,
@@ -121,7 +121,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
121121
.prepareAsync(clientOptions, params)
122122
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
123123
val response = clientOptions.httpClient.executeAsync(request, requestOptions)
124-
return response.parseable {
124+
return errorHandler.handle(response).parseable {
125125
response
126126
.use { retrieveHandler.handle(it) }
127127
.also {
@@ -132,8 +132,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
132132
}
133133
}
134134

135-
private val updateHandler: Handler<Alert> =
136-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
135+
private val updateHandler: Handler<Alert> = jsonHandler<Alert>(clientOptions.jsonMapper)
137136

138137
override suspend fun update(
139138
params: AlertUpdateParams,
@@ -152,7 +151,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
152151
.prepareAsync(clientOptions, params)
153152
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
154153
val response = clientOptions.httpClient.executeAsync(request, requestOptions)
155-
return response.parseable {
154+
return errorHandler.handle(response).parseable {
156155
response
157156
.use { updateHandler.handle(it) }
158157
.also {
@@ -165,7 +164,6 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
165164

166165
private val listHandler: Handler<AlertListPageResponse> =
167166
jsonHandler<AlertListPageResponse>(clientOptions.jsonMapper)
168-
.withErrorHandler(errorHandler)
169167

170168
override suspend fun list(
171169
params: AlertListParams,
@@ -180,7 +178,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
180178
.prepareAsync(clientOptions, params)
181179
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
182180
val response = clientOptions.httpClient.executeAsync(request, requestOptions)
183-
return response.parseable {
181+
return errorHandler.handle(response).parseable {
184182
response
185183
.use { listHandler.handle(it) }
186184
.also {
@@ -199,7 +197,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
199197
}
200198

201199
private val createForCustomerHandler: Handler<Alert> =
202-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
200+
jsonHandler<Alert>(clientOptions.jsonMapper)
203201

204202
override suspend fun createForCustomer(
205203
params: AlertCreateForCustomerParams,
@@ -218,7 +216,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
218216
.prepareAsync(clientOptions, params)
219217
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
220218
val response = clientOptions.httpClient.executeAsync(request, requestOptions)
221-
return response.parseable {
219+
return errorHandler.handle(response).parseable {
222220
response
223221
.use { createForCustomerHandler.handle(it) }
224222
.also {
@@ -230,7 +228,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
230228
}
231229

232230
private val createForExternalCustomerHandler: Handler<Alert> =
233-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
231+
jsonHandler<Alert>(clientOptions.jsonMapper)
234232

235233
override suspend fun createForExternalCustomer(
236234
params: AlertCreateForExternalCustomerParams,
@@ -249,7 +247,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
249247
.prepareAsync(clientOptions, params)
250248
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
251249
val response = clientOptions.httpClient.executeAsync(request, requestOptions)
252-
return response.parseable {
250+
return errorHandler.handle(response).parseable {
253251
response
254252
.use { createForExternalCustomerHandler.handle(it) }
255253
.also {
@@ -261,7 +259,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
261259
}
262260

263261
private val createForSubscriptionHandler: Handler<Alert> =
264-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
262+
jsonHandler<Alert>(clientOptions.jsonMapper)
265263

266264
override suspend fun createForSubscription(
267265
params: AlertCreateForSubscriptionParams,
@@ -280,7 +278,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
280278
.prepareAsync(clientOptions, params)
281279
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
282280
val response = clientOptions.httpClient.executeAsync(request, requestOptions)
283-
return response.parseable {
281+
return errorHandler.handle(response).parseable {
284282
response
285283
.use { createForSubscriptionHandler.handle(it) }
286284
.also {
@@ -291,8 +289,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
291289
}
292290
}
293291

294-
private val disableHandler: Handler<Alert> =
295-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
292+
private val disableHandler: Handler<Alert> = jsonHandler<Alert>(clientOptions.jsonMapper)
296293

297294
override suspend fun disable(
298295
params: AlertDisableParams,
@@ -311,7 +308,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
311308
.prepareAsync(clientOptions, params)
312309
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
313310
val response = clientOptions.httpClient.executeAsync(request, requestOptions)
314-
return response.parseable {
311+
return errorHandler.handle(response).parseable {
315312
response
316313
.use { disableHandler.handle(it) }
317314
.also {
@@ -322,8 +319,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
322319
}
323320
}
324321

325-
private val enableHandler: Handler<Alert> =
326-
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
322+
private val enableHandler: Handler<Alert> = jsonHandler<Alert>(clientOptions.jsonMapper)
327323

328324
override suspend fun enable(
329325
params: AlertEnableParams,
@@ -342,7 +338,7 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
342338
.prepareAsync(clientOptions, params)
343339
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
344340
val response = clientOptions.httpClient.executeAsync(request, requestOptions)
345-
return response.parseable {
341+
return errorHandler.handle(response).parseable {
346342
response
347343
.use { enableHandler.handle(it) }
348344
.also {

orb-kotlin-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
@@ -66,7 +66,8 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
6666
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
6767
BetaServiceAsync.WithRawResponse {
6868

69-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
69+
private val errorHandler: Handler<HttpResponse> =
70+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
7071

7172
private val externalPlanId: ExternalPlanIdServiceAsync.WithRawResponse by lazy {
7273
ExternalPlanIdServiceAsyncImpl.WithRawResponseImpl(clientOptions)
@@ -82,7 +83,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
8283
override fun externalPlanId(): ExternalPlanIdServiceAsync.WithRawResponse = externalPlanId
8384

8485
private val createPlanVersionHandler: Handler<PlanVersion> =
85-
jsonHandler<PlanVersion>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
86+
jsonHandler<PlanVersion>(clientOptions.jsonMapper)
8687

8788
override suspend fun createPlanVersion(
8889
params: BetaCreatePlanVersionParams,
@@ -101,7 +102,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
101102
.prepareAsync(clientOptions, params)
102103
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
103104
val response = clientOptions.httpClient.executeAsync(request, requestOptions)
104-
return response.parseable {
105+
return errorHandler.handle(response).parseable {
105106
response
106107
.use { createPlanVersionHandler.handle(it) }
107108
.also {
@@ -113,7 +114,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
113114
}
114115

115116
private val fetchPlanVersionHandler: Handler<PlanVersion> =
116-
jsonHandler<PlanVersion>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
117+
jsonHandler<PlanVersion>(clientOptions.jsonMapper)
117118

118119
override suspend fun fetchPlanVersion(
119120
params: BetaFetchPlanVersionParams,
@@ -136,7 +137,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
136137
.prepareAsync(clientOptions, params)
137138
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
138139
val response = clientOptions.httpClient.executeAsync(request, requestOptions)
139-
return response.parseable {
140+
return errorHandler.handle(response).parseable {
140141
response
141142
.use { fetchPlanVersionHandler.handle(it) }
142143
.also {
@@ -148,7 +149,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
148149
}
149150

150151
private val setDefaultPlanVersionHandler: Handler<Plan> =
151-
jsonHandler<Plan>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
152+
jsonHandler<Plan>(clientOptions.jsonMapper)
152153

153154
override suspend fun setDefaultPlanVersion(
154155
params: BetaSetDefaultPlanVersionParams,
@@ -167,7 +168,7 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
167168
.prepareAsync(clientOptions, params)
168169
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
169170
val response = clientOptions.httpClient.executeAsync(request, requestOptions)
170-
return response.parseable {
171+
return errorHandler.handle(response).parseable {
171172
response
172173
.use { setDefaultPlanVersionHandler.handle(it) }
173174
.also {

0 commit comments

Comments
 (0)