Skip to content

Commit 31c1298

Browse files
feat(client): more robust error parsing
1 parent 766609c commit 31c1298

8 files changed

Lines changed: 42 additions & 8 deletions

finch-java-core/src/main/kotlin/com/tryfinch/api/errors/BadRequestException.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ package com.tryfinch.api.errors
55
import com.tryfinch.api.core.JsonValue
66
import com.tryfinch.api.core.checkRequired
77
import com.tryfinch.api.core.http.Headers
8+
import com.tryfinch.api.core.jsonMapper
89
import java.util.Optional
910
import kotlin.jvm.optionals.getOrNull
1011

1112
class BadRequestException
1213
private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) :
13-
FinchServiceException("400: $body", cause) {
14+
FinchServiceException(
15+
"400: ${if (body.isMissing()) "Unknown" else jsonMapper().writeValueAsString(body)}",
16+
cause,
17+
) {
1418

1519
override fun statusCode(): Int = 400
1620

finch-java-core/src/main/kotlin/com/tryfinch/api/errors/InternalServerException.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package com.tryfinch.api.errors
55
import com.tryfinch.api.core.JsonValue
66
import com.tryfinch.api.core.checkRequired
77
import com.tryfinch.api.core.http.Headers
8+
import com.tryfinch.api.core.jsonMapper
89
import java.util.Optional
910
import kotlin.jvm.optionals.getOrNull
1011

@@ -14,7 +15,11 @@ private constructor(
1415
private val headers: Headers,
1516
private val body: JsonValue,
1617
cause: Throwable?,
17-
) : FinchServiceException("$statusCode: $body", cause) {
18+
) :
19+
FinchServiceException(
20+
"$statusCode: ${if (body.isMissing()) "Unknown" else jsonMapper().writeValueAsString(body)}",
21+
cause,
22+
) {
1823

1924
override fun statusCode(): Int = statusCode
2025

finch-java-core/src/main/kotlin/com/tryfinch/api/errors/NotFoundException.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ package com.tryfinch.api.errors
55
import com.tryfinch.api.core.JsonValue
66
import com.tryfinch.api.core.checkRequired
77
import com.tryfinch.api.core.http.Headers
8+
import com.tryfinch.api.core.jsonMapper
89
import java.util.Optional
910
import kotlin.jvm.optionals.getOrNull
1011

1112
class NotFoundException
1213
private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) :
13-
FinchServiceException("404: $body", cause) {
14+
FinchServiceException(
15+
"404: ${if (body.isMissing()) "Unknown" else jsonMapper().writeValueAsString(body)}",
16+
cause,
17+
) {
1418

1519
override fun statusCode(): Int = 404
1620

finch-java-core/src/main/kotlin/com/tryfinch/api/errors/PermissionDeniedException.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ package com.tryfinch.api.errors
55
import com.tryfinch.api.core.JsonValue
66
import com.tryfinch.api.core.checkRequired
77
import com.tryfinch.api.core.http.Headers
8+
import com.tryfinch.api.core.jsonMapper
89
import java.util.Optional
910
import kotlin.jvm.optionals.getOrNull
1011

1112
class PermissionDeniedException
1213
private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) :
13-
FinchServiceException("403: $body", cause) {
14+
FinchServiceException(
15+
"403: ${if (body.isMissing()) "Unknown" else jsonMapper().writeValueAsString(body)}",
16+
cause,
17+
) {
1418

1519
override fun statusCode(): Int = 403
1620

finch-java-core/src/main/kotlin/com/tryfinch/api/errors/RateLimitException.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ package com.tryfinch.api.errors
55
import com.tryfinch.api.core.JsonValue
66
import com.tryfinch.api.core.checkRequired
77
import com.tryfinch.api.core.http.Headers
8+
import com.tryfinch.api.core.jsonMapper
89
import java.util.Optional
910
import kotlin.jvm.optionals.getOrNull
1011

1112
class RateLimitException
1213
private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) :
13-
FinchServiceException("429: $body", cause) {
14+
FinchServiceException(
15+
"429: ${if (body.isMissing()) "Unknown" else jsonMapper().writeValueAsString(body)}",
16+
cause,
17+
) {
1418

1519
override fun statusCode(): Int = 429
1620

finch-java-core/src/main/kotlin/com/tryfinch/api/errors/UnauthorizedException.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ package com.tryfinch.api.errors
55
import com.tryfinch.api.core.JsonValue
66
import com.tryfinch.api.core.checkRequired
77
import com.tryfinch.api.core.http.Headers
8+
import com.tryfinch.api.core.jsonMapper
89
import java.util.Optional
910
import kotlin.jvm.optionals.getOrNull
1011

1112
class UnauthorizedException
1213
private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) :
13-
FinchServiceException("401: $body", cause) {
14+
FinchServiceException(
15+
"401: ${if (body.isMissing()) "Unknown" else jsonMapper().writeValueAsString(body)}",
16+
cause,
17+
) {
1418

1519
override fun statusCode(): Int = 401
1620

finch-java-core/src/main/kotlin/com/tryfinch/api/errors/UnexpectedStatusCodeException.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package com.tryfinch.api.errors
55
import com.tryfinch.api.core.JsonValue
66
import com.tryfinch.api.core.checkRequired
77
import com.tryfinch.api.core.http.Headers
8+
import com.tryfinch.api.core.jsonMapper
89
import java.util.Optional
910
import kotlin.jvm.optionals.getOrNull
1011

@@ -14,7 +15,11 @@ private constructor(
1415
private val headers: Headers,
1516
private val body: JsonValue,
1617
cause: Throwable?,
17-
) : FinchServiceException("$statusCode: $body", cause) {
18+
) :
19+
FinchServiceException(
20+
"$statusCode: ${if (body.isMissing()) "Unknown" else jsonMapper().writeValueAsString(body)}",
21+
cause,
22+
) {
1823

1924
override fun statusCode(): Int = statusCode
2025

finch-java-core/src/main/kotlin/com/tryfinch/api/errors/UnprocessableEntityException.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ package com.tryfinch.api.errors
55
import com.tryfinch.api.core.JsonValue
66
import com.tryfinch.api.core.checkRequired
77
import com.tryfinch.api.core.http.Headers
8+
import com.tryfinch.api.core.jsonMapper
89
import java.util.Optional
910
import kotlin.jvm.optionals.getOrNull
1011

1112
class UnprocessableEntityException
1213
private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) :
13-
FinchServiceException("422: $body", cause) {
14+
FinchServiceException(
15+
"422: ${if (body.isMissing()) "Unknown" else jsonMapper().writeValueAsString(body)}",
16+
cause,
17+
) {
1418

1519
override fun statusCode(): Int = 422
1620

0 commit comments

Comments
 (0)