Skip to content

Commit 98e72bc

Browse files
fix(client): ensure error handling always occurs
1 parent be5c316 commit 98e72bc

File tree

58 files changed

+491
-365
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

+491
-365
lines changed

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

finch-java-core/src/main/kotlin/com/tryfinch/api/services/async/AccessTokenServiceAsyncImpl.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
package com.tryfinch.api.services.async
44

55
import com.tryfinch.api.core.ClientOptions
6-
import com.tryfinch.api.core.JsonValue
76
import com.tryfinch.api.core.RequestOptions
7+
import com.tryfinch.api.core.handlers.errorBodyHandler
88
import com.tryfinch.api.core.handlers.errorHandler
99
import com.tryfinch.api.core.handlers.jsonHandler
10-
import com.tryfinch.api.core.handlers.withErrorHandler
1110
import com.tryfinch.api.core.http.HttpMethod
1211
import com.tryfinch.api.core.http.HttpRequest
12+
import com.tryfinch.api.core.http.HttpResponse
1313
import com.tryfinch.api.core.http.HttpResponse.Handler
1414
import com.tryfinch.api.core.http.HttpResponseFor
1515
import com.tryfinch.api.core.http.json
@@ -44,7 +44,8 @@ class AccessTokenServiceAsyncImpl internal constructor(private val clientOptions
4444
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
4545
AccessTokenServiceAsync.WithRawResponse {
4646

47-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
47+
private val errorHandler: Handler<HttpResponse> =
48+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
4849

4950
override fun withOptions(
5051
modifier: Consumer<ClientOptions.Builder>
@@ -55,7 +56,6 @@ class AccessTokenServiceAsyncImpl internal constructor(private val clientOptions
5556

5657
private val createHandler: Handler<CreateAccessTokenResponse> =
5758
jsonHandler<CreateAccessTokenResponse>(clientOptions.jsonMapper)
58-
.withErrorHandler(errorHandler)
5959

6060
override fun create(
6161
params: AccessTokenCreateParams,
@@ -101,7 +101,7 @@ class AccessTokenServiceAsyncImpl internal constructor(private val clientOptions
101101
return request
102102
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
103103
.thenApply { response ->
104-
response.parseable {
104+
errorHandler.handle(response).parseable {
105105
response
106106
.use { createHandler.handle(it) }
107107
.also {

finch-java-core/src/main/kotlin/com/tryfinch/api/services/async/AccountServiceAsyncImpl.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
package com.tryfinch.api.services.async
44

55
import com.tryfinch.api.core.ClientOptions
6-
import com.tryfinch.api.core.JsonValue
76
import com.tryfinch.api.core.RequestOptions
7+
import com.tryfinch.api.core.handlers.errorBodyHandler
88
import com.tryfinch.api.core.handlers.errorHandler
99
import com.tryfinch.api.core.handlers.jsonHandler
10-
import com.tryfinch.api.core.handlers.withErrorHandler
1110
import com.tryfinch.api.core.http.HttpMethod
1211
import com.tryfinch.api.core.http.HttpRequest
12+
import com.tryfinch.api.core.http.HttpResponse
1313
import com.tryfinch.api.core.http.HttpResponse.Handler
1414
import com.tryfinch.api.core.http.HttpResponseFor
1515
import com.tryfinch.api.core.http.json
@@ -51,7 +51,8 @@ class AccountServiceAsyncImpl internal constructor(private val clientOptions: Cl
5151
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
5252
AccountServiceAsync.WithRawResponse {
5353

54-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
54+
private val errorHandler: Handler<HttpResponse> =
55+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
5556

5657
override fun withOptions(
5758
modifier: Consumer<ClientOptions.Builder>
@@ -61,7 +62,7 @@ class AccountServiceAsyncImpl internal constructor(private val clientOptions: Cl
6162
)
6263

6364
private val disconnectHandler: Handler<DisconnectResponse> =
64-
jsonHandler<DisconnectResponse>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
65+
jsonHandler<DisconnectResponse>(clientOptions.jsonMapper)
6566

6667
override fun disconnect(
6768
params: AccountDisconnectParams,
@@ -79,7 +80,7 @@ class AccountServiceAsyncImpl internal constructor(private val clientOptions: Cl
7980
return request
8081
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
8182
.thenApply { response ->
82-
response.parseable {
83+
errorHandler.handle(response).parseable {
8384
response
8485
.use { disconnectHandler.handle(it) }
8586
.also {
@@ -92,7 +93,7 @@ class AccountServiceAsyncImpl internal constructor(private val clientOptions: Cl
9293
}
9394

9495
private val introspectHandler: Handler<Introspection> =
95-
jsonHandler<Introspection>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
96+
jsonHandler<Introspection>(clientOptions.jsonMapper)
9697

9798
override fun introspect(
9899
params: AccountIntrospectParams,
@@ -109,7 +110,7 @@ class AccountServiceAsyncImpl internal constructor(private val clientOptions: Cl
109110
return request
110111
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
111112
.thenApply { response ->
112-
response.parseable {
113+
errorHandler.handle(response).parseable {
113114
response
114115
.use { introspectHandler.handle(it) }
115116
.also {

finch-java-core/src/main/kotlin/com/tryfinch/api/services/async/ProviderServiceAsyncImpl.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
package com.tryfinch.api.services.async
44

55
import com.tryfinch.api.core.ClientOptions
6-
import com.tryfinch.api.core.JsonValue
76
import com.tryfinch.api.core.RequestOptions
7+
import com.tryfinch.api.core.handlers.errorBodyHandler
88
import com.tryfinch.api.core.handlers.errorHandler
99
import com.tryfinch.api.core.handlers.jsonHandler
10-
import com.tryfinch.api.core.handlers.withErrorHandler
1110
import com.tryfinch.api.core.http.HttpMethod
1211
import com.tryfinch.api.core.http.HttpRequest
12+
import com.tryfinch.api.core.http.HttpResponse
1313
import com.tryfinch.api.core.http.HttpResponse.Handler
1414
import com.tryfinch.api.core.http.HttpResponseFor
1515
import com.tryfinch.api.core.http.parseable
@@ -42,7 +42,8 @@ class ProviderServiceAsyncImpl internal constructor(private val clientOptions: C
4242
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
4343
ProviderServiceAsync.WithRawResponse {
4444

45-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
45+
private val errorHandler: Handler<HttpResponse> =
46+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
4647

4748
override fun withOptions(
4849
modifier: Consumer<ClientOptions.Builder>
@@ -52,7 +53,7 @@ class ProviderServiceAsyncImpl internal constructor(private val clientOptions: C
5253
)
5354

5455
private val listHandler: Handler<List<Provider>> =
55-
jsonHandler<List<Provider>>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
56+
jsonHandler<List<Provider>>(clientOptions.jsonMapper)
5657

5758
override fun list(
5859
params: ProviderListParams,
@@ -69,7 +70,7 @@ class ProviderServiceAsyncImpl internal constructor(private val clientOptions: C
6970
return request
7071
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
7172
.thenApply { response ->
72-
response.parseable {
73+
errorHandler.handle(response).parseable {
7374
response
7475
.use { listHandler.handle(it) }
7576
.also {

finch-java-core/src/main/kotlin/com/tryfinch/api/services/async/RequestForwardingServiceAsyncImpl.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
package com.tryfinch.api.services.async
44

55
import com.tryfinch.api.core.ClientOptions
6-
import com.tryfinch.api.core.JsonValue
76
import com.tryfinch.api.core.RequestOptions
7+
import com.tryfinch.api.core.handlers.errorBodyHandler
88
import com.tryfinch.api.core.handlers.errorHandler
99
import com.tryfinch.api.core.handlers.jsonHandler
10-
import com.tryfinch.api.core.handlers.withErrorHandler
1110
import com.tryfinch.api.core.http.HttpMethod
1211
import com.tryfinch.api.core.http.HttpRequest
12+
import com.tryfinch.api.core.http.HttpResponse
1313
import com.tryfinch.api.core.http.HttpResponse.Handler
1414
import com.tryfinch.api.core.http.HttpResponseFor
1515
import com.tryfinch.api.core.http.json
@@ -44,7 +44,8 @@ internal constructor(private val clientOptions: ClientOptions) : RequestForwardi
4444
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
4545
RequestForwardingServiceAsync.WithRawResponse {
4646

47-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
47+
private val errorHandler: Handler<HttpResponse> =
48+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
4849

4950
override fun withOptions(
5051
modifier: Consumer<ClientOptions.Builder>
@@ -55,7 +56,6 @@ internal constructor(private val clientOptions: ClientOptions) : RequestForwardi
5556

5657
private val forwardHandler: Handler<RequestForwardingForwardResponse> =
5758
jsonHandler<RequestForwardingForwardResponse>(clientOptions.jsonMapper)
58-
.withErrorHandler(errorHandler)
5959

6060
override fun forward(
6161
params: RequestForwardingForwardParams,
@@ -73,7 +73,7 @@ internal constructor(private val clientOptions: ClientOptions) : RequestForwardi
7373
return request
7474
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
7575
.thenApply { response ->
76-
response.parseable {
76+
errorHandler.handle(response).parseable {
7777
response
7878
.use { forwardHandler.handle(it) }
7979
.also {

finch-java-core/src/main/kotlin/com/tryfinch/api/services/async/connect/SessionServiceAsyncImpl.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
package com.tryfinch.api.services.async.connect
44

55
import com.tryfinch.api.core.ClientOptions
6-
import com.tryfinch.api.core.JsonValue
76
import com.tryfinch.api.core.RequestOptions
7+
import com.tryfinch.api.core.handlers.errorBodyHandler
88
import com.tryfinch.api.core.handlers.errorHandler
99
import com.tryfinch.api.core.handlers.jsonHandler
10-
import com.tryfinch.api.core.handlers.withErrorHandler
1110
import com.tryfinch.api.core.http.HttpMethod
1211
import com.tryfinch.api.core.http.HttpRequest
12+
import com.tryfinch.api.core.http.HttpResponse
1313
import com.tryfinch.api.core.http.HttpResponse.Handler
1414
import com.tryfinch.api.core.http.HttpResponseFor
1515
import com.tryfinch.api.core.http.json
@@ -51,7 +51,8 @@ class SessionServiceAsyncImpl internal constructor(private val clientOptions: Cl
5151
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
5252
SessionServiceAsync.WithRawResponse {
5353

54-
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
54+
private val errorHandler: Handler<HttpResponse> =
55+
errorHandler(errorBodyHandler(clientOptions.jsonMapper))
5556

5657
override fun withOptions(
5758
modifier: Consumer<ClientOptions.Builder>
@@ -61,7 +62,7 @@ class SessionServiceAsyncImpl internal constructor(private val clientOptions: Cl
6162
)
6263

6364
private val newHandler: Handler<SessionNewResponse> =
64-
jsonHandler<SessionNewResponse>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
65+
jsonHandler<SessionNewResponse>(clientOptions.jsonMapper)
6566

6667
override fun new_(
6768
params: ConnectSessionNewParams,
@@ -79,7 +80,7 @@ class SessionServiceAsyncImpl internal constructor(private val clientOptions: Cl
7980
return request
8081
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
8182
.thenApply { response ->
82-
response.parseable {
83+
errorHandler.handle(response).parseable {
8384
response
8485
.use { newHandler.handle(it) }
8586
.also {
@@ -93,7 +94,6 @@ class SessionServiceAsyncImpl internal constructor(private val clientOptions: Cl
9394

9495
private val reauthenticateHandler: Handler<SessionReauthenticateResponse> =
9596
jsonHandler<SessionReauthenticateResponse>(clientOptions.jsonMapper)
96-
.withErrorHandler(errorHandler)
9797

9898
override fun reauthenticate(
9999
params: ConnectSessionReauthenticateParams,
@@ -111,7 +111,7 @@ class SessionServiceAsyncImpl internal constructor(private val clientOptions: Cl
111111
return request
112112
.thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
113113
.thenApply { response ->
114-
response.parseable {
114+
errorHandler.handle(response).parseable {
115115
response
116116
.use { reauthenticateHandler.handle(it) }
117117
.also {

0 commit comments

Comments
 (0)