Skip to content

Commit 037c1ff

Browse files
fix(client): ensure error handling always occurs
1 parent 59ee949 commit 037c1ff

96 files changed

Lines changed: 1397 additions & 1007 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.

lithic-java-core/src/main/kotlin/com/lithic/api/client/LithicClientAsyncImpl.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
package com.lithic.api.client
44

55
import com.lithic.api.core.ClientOptions
6-
import com.lithic.api.core.JsonValue
76
import com.lithic.api.core.RequestOptions
87
import com.lithic.api.core.getPackageVersion
8+
import com.lithic.api.core.handlers.errorBodyHandler
99
import com.lithic.api.core.handlers.errorHandler
1010
import com.lithic.api.core.handlers.jsonHandler
11-
import com.lithic.api.core.handlers.withErrorHandler
1211
import com.lithic.api.core.http.HttpMethod
1312
import com.lithic.api.core.http.HttpRequest
13+
import com.lithic.api.core.http.HttpResponse
1414
import com.lithic.api.core.http.HttpResponse.Handler
1515
import com.lithic.api.core.http.HttpResponseFor
1616
import com.lithic.api.core.http.parseable
@@ -285,7 +285,8 @@ class LithicClientAsyncImpl(private val clientOptions: ClientOptions) : LithicCl
285285
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
286286
LithicClientAsync.WithRawResponse {
287287

288-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
288+
private val errorHandler: Handler<HttpResponse> =
289+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
289290

290291
private val accounts: AccountServiceAsync.WithRawResponse by lazy {
291292
AccountServiceAsyncImpl.WithRawResponseImpl(clientOptions)
@@ -472,7 +473,7 @@ class LithicClientAsyncImpl(private val clientOptions: ClientOptions) : LithicCl
472473
override fun networkPrograms(): NetworkProgramServiceAsync.WithRawResponse = networkPrograms
473474

474475
private val apiStatusHandler: Handler<ApiStatus> =
475-
jsonHandler<ApiStatus>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
476+
jsonHandler<ApiStatus>(clientOptions.jsonMapper)
476477

477478
override fun apiStatus(
478479
params: ClientApiStatusParams,
@@ -489,7 +490,7 @@ class LithicClientAsyncImpl(private val clientOptions: ClientOptions) : LithicCl
489490
return request
490491
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
491492
.thenApply { response ->
492-
response.parseable {
493+
errorHandler.handle(response).parseable {
493494
response
494495
.use { apiStatusHandler.handle(it) }
495496
.also {

lithic-java-core/src/main/kotlin/com/lithic/api/client/LithicClientImpl.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
package com.lithic.api.client
44

55
import com.lithic.api.core.ClientOptions
6-
import com.lithic.api.core.JsonValue
76
import com.lithic.api.core.RequestOptions
87
import com.lithic.api.core.getPackageVersion
8+
import com.lithic.api.core.handlers.errorBodyHandler
99
import com.lithic.api.core.handlers.errorHandler
1010
import com.lithic.api.core.handlers.jsonHandler
11-
import com.lithic.api.core.handlers.withErrorHandler
1211
import com.lithic.api.core.http.HttpMethod
1312
import com.lithic.api.core.http.HttpRequest
13+
import com.lithic.api.core.http.HttpResponse
1414
import com.lithic.api.core.http.HttpResponse.Handler
1515
import com.lithic.api.core.http.HttpResponseFor
1616
import com.lithic.api.core.http.parseable
@@ -267,7 +267,8 @@ class LithicClientImpl(private val clientOptions: ClientOptions) : LithicClient
267267
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
268268
LithicClient.WithRawResponse {
269269

270-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
270+
private val errorHandler: Handler<HttpResponse> =
271+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
271272

272273
private val accounts: AccountService.WithRawResponse by lazy {
273274
AccountServiceImpl.WithRawResponseImpl(clientOptions)
@@ -453,7 +454,7 @@ class LithicClientImpl(private val clientOptions: ClientOptions) : LithicClient
453454
override fun networkPrograms(): NetworkProgramService.WithRawResponse = networkPrograms
454455

455456
private val apiStatusHandler: Handler<ApiStatus> =
456-
jsonHandler<ApiStatus>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
457+
jsonHandler<ApiStatus>(clientOptions.jsonMapper)
457458

458459
override fun apiStatus(
459460
params: ClientApiStatusParams,
@@ -468,7 +469,7 @@ class LithicClientImpl(private val clientOptions: ClientOptions) : LithicClient
468469
.prepare(clientOptions, params)
469470
val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
470471
val response = clientOptions.httpClient.execute(request, requestOptions)
471-
return response.parseable {
472+
return errorHandler.handle(response).parseable {
472473
response
473474
.use { apiStatusHandler.handle(it) }
474475
.also {

lithic-java-core/src/main/kotlin/com/lithic/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.lithic.api.errors.UnexpectedStatusCodeException
1919
import com.lithic.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
}

lithic-java-core/src/main/kotlin/com/lithic/api/services/async/AccountHolderServiceAsyncImpl.kt

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

55
import com.lithic.api.core.ClientOptions
6-
import com.lithic.api.core.JsonValue
76
import com.lithic.api.core.RequestOptions
87
import com.lithic.api.core.checkRequired
8+
import com.lithic.api.core.handlers.errorBodyHandler
99
import com.lithic.api.core.handlers.errorHandler
1010
import com.lithic.api.core.handlers.jsonHandler
11-
import com.lithic.api.core.handlers.withErrorHandler
1211
import com.lithic.api.core.http.HttpMethod
1312
import com.lithic.api.core.http.HttpRequest
13+
import com.lithic.api.core.http.HttpResponse
1414
import com.lithic.api.core.http.HttpResponse.Handler
1515
import com.lithic.api.core.http.HttpResponseFor
1616
import com.lithic.api.core.http.json
@@ -118,7 +118,8 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
118118
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
119119
AccountHolderServiceAsync.WithRawResponse {
120120

121-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
121+
private val errorHandler: Handler<HttpResponse> =
122+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
122123

123124
override fun withOptions(
124125
modifier: Consumer<ClientOptions.Builder>
@@ -129,7 +130,6 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
129130

130131
private val createHandler: Handler<AccountHolderCreateResponse> =
131132
jsonHandler<AccountHolderCreateResponse>(clientOptions.jsonMapper)
132-
.withErrorHandler(errorHandler)
133133

134134
override fun create(
135135
params: AccountHolderCreateParams,
@@ -150,7 +150,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
150150
return request
151151
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
152152
.thenApply { response ->
153-
response.parseable {
153+
errorHandler.handle(response).parseable {
154154
response
155155
.use { createHandler.handle(it) }
156156
.also {
@@ -163,7 +163,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
163163
}
164164

165165
private val retrieveHandler: Handler<AccountHolder> =
166-
jsonHandler<AccountHolder>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
166+
jsonHandler<AccountHolder>(clientOptions.jsonMapper)
167167

168168
override fun retrieve(
169169
params: AccountHolderRetrieveParams,
@@ -183,7 +183,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
183183
return request
184184
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
185185
.thenApply { response ->
186-
response.parseable {
186+
errorHandler.handle(response).parseable {
187187
response
188188
.use { retrieveHandler.handle(it) }
189189
.also {
@@ -197,7 +197,6 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
197197

198198
private val updateHandler: Handler<AccountHolderUpdateResponse> =
199199
jsonHandler<AccountHolderUpdateResponse>(clientOptions.jsonMapper)
200-
.withErrorHandler(errorHandler)
201200

202201
override fun update(
203202
params: AccountHolderUpdateParams,
@@ -218,7 +217,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
218217
return request
219218
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
220219
.thenApply { response ->
221-
response.parseable {
220+
errorHandler.handle(response).parseable {
222221
response
223222
.use { updateHandler.handle(it) }
224223
.also {
@@ -232,7 +231,6 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
232231

233232
private val listHandler: Handler<AccountHolderListPageResponse> =
234233
jsonHandler<AccountHolderListPageResponse>(clientOptions.jsonMapper)
235-
.withErrorHandler(errorHandler)
236234

237235
override fun list(
238236
params: AccountHolderListParams,
@@ -249,7 +247,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
249247
return request
250248
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
251249
.thenApply { response ->
252-
response.parseable {
250+
errorHandler.handle(response).parseable {
253251
response
254252
.use { listHandler.handle(it) }
255253
.also {
@@ -271,7 +269,6 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
271269

272270
private val listDocumentsHandler: Handler<AccountHolderListDocumentsResponse> =
273271
jsonHandler<AccountHolderListDocumentsResponse>(clientOptions.jsonMapper)
274-
.withErrorHandler(errorHandler)
275272

276273
override fun listDocuments(
277274
params: AccountHolderListDocumentsParams,
@@ -291,7 +288,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
291288
return request
292289
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
293290
.thenApply { response ->
294-
response.parseable {
291+
errorHandler.handle(response).parseable {
295292
response
296293
.use { listDocumentsHandler.handle(it) }
297294
.also {
@@ -304,7 +301,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
304301
}
305302

306303
private val retrieveDocumentHandler: Handler<Document> =
307-
jsonHandler<Document>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
304+
jsonHandler<Document>(clientOptions.jsonMapper)
308305

309306
override fun retrieveDocument(
310307
params: AccountHolderRetrieveDocumentParams,
@@ -330,7 +327,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
330327
return request
331328
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
332329
.thenApply { response ->
333-
response.parseable {
330+
errorHandler.handle(response).parseable {
334331
response
335332
.use { retrieveDocumentHandler.handle(it) }
336333
.also {
@@ -343,7 +340,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
343340
}
344341

345342
private val simulateEnrollmentDocumentReviewHandler: Handler<Document> =
346-
jsonHandler<Document>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
343+
jsonHandler<Document>(clientOptions.jsonMapper)
347344

348345
override fun simulateEnrollmentDocumentReview(
349346
params: AccountHolderSimulateEnrollmentDocumentReviewParams,
@@ -366,7 +363,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
366363
return request
367364
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
368365
.thenApply { response ->
369-
response.parseable {
366+
errorHandler.handle(response).parseable {
370367
response
371368
.use { simulateEnrollmentDocumentReviewHandler.handle(it) }
372369
.also {
@@ -381,7 +378,6 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
381378
private val simulateEnrollmentReviewHandler:
382379
Handler<AccountHolderSimulateEnrollmentReviewResponse> =
383380
jsonHandler<AccountHolderSimulateEnrollmentReviewResponse>(clientOptions.jsonMapper)
384-
.withErrorHandler(errorHandler)
385381

386382
override fun simulateEnrollmentReview(
387383
params: AccountHolderSimulateEnrollmentReviewParams,
@@ -399,7 +395,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
399395
return request
400396
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
401397
.thenApply { response ->
402-
response.parseable {
398+
errorHandler.handle(response).parseable {
403399
response
404400
.use { simulateEnrollmentReviewHandler.handle(it) }
405401
.also {
@@ -412,7 +408,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
412408
}
413409

414410
private val uploadDocumentHandler: Handler<Document> =
415-
jsonHandler<Document>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
411+
jsonHandler<Document>(clientOptions.jsonMapper)
416412

417413
override fun uploadDocument(
418414
params: AccountHolderUploadDocumentParams,
@@ -433,7 +429,7 @@ class AccountHolderServiceAsyncImpl internal constructor(private val clientOptio
433429
return request
434430
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
435431
.thenApply { response ->
436-
response.parseable {
432+
errorHandler.handle(response).parseable {
437433
response
438434
.use { uploadDocumentHandler.handle(it) }
439435
.also {

0 commit comments

Comments
 (0)