Skip to content

Commit b45ee94

Browse files
chore(client)!: refactor exception structure and methods (#294)
# Migration Previously you would access error JSON on an exception via `exception.error()._additionalProperties()`, which would return `Map<String, JsonValue>`. Now you would access this via `exception.body()`, which returns `JsonValue`. You should no longer assume that the returned error JSON is an object. You can check via `exception.body().asObject()`.
1 parent 57b1869 commit b45ee94

File tree

68 files changed

+787
-405
lines changed

Some content is hidden

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

68 files changed

+787
-405
lines changed
Lines changed: 53 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,80 @@
1-
@file:JvmName("ErrorHandler")
1+
// File generated from our OpenAPI spec by Stainless.
22

33
package org.onebusaway.core.handlers
44

55
import com.fasterxml.jackson.databind.json.JsonMapper
6-
import java.io.ByteArrayInputStream
7-
import java.io.InputStream
8-
import org.onebusaway.core.http.Headers
6+
import org.onebusaway.core.JsonMissing
7+
import org.onebusaway.core.JsonValue
98
import org.onebusaway.core.http.HttpResponse
109
import org.onebusaway.core.http.HttpResponse.Handler
1110
import org.onebusaway.errors.BadRequestException
1211
import org.onebusaway.errors.InternalServerException
1312
import org.onebusaway.errors.NotFoundException
14-
import org.onebusaway.errors.OnebusawaySdkError
1513
import org.onebusaway.errors.PermissionDeniedException
1614
import org.onebusaway.errors.RateLimitException
1715
import org.onebusaway.errors.UnauthorizedException
1816
import org.onebusaway.errors.UnexpectedStatusCodeException
1917
import org.onebusaway.errors.UnprocessableEntityException
2018

21-
internal fun errorHandler(jsonMapper: JsonMapper): Handler<OnebusawaySdkError> {
22-
val handler = jsonHandler<OnebusawaySdkError>(jsonMapper)
19+
internal fun errorHandler(jsonMapper: JsonMapper): Handler<JsonValue> {
20+
val handler = jsonHandler<JsonValue>(jsonMapper)
2321

24-
return object : Handler<OnebusawaySdkError> {
25-
override fun handle(response: HttpResponse): OnebusawaySdkError =
22+
return object : Handler<JsonValue> {
23+
override fun handle(response: HttpResponse): JsonValue =
2624
try {
2725
handler.handle(response)
2826
} catch (e: Exception) {
29-
OnebusawaySdkError.builder().build()
27+
JsonMissing.of()
3028
}
3129
}
3230
}
3331

34-
internal fun <T> Handler<T>.withErrorHandler(
35-
errorHandler: Handler<OnebusawaySdkError>
36-
): Handler<T> =
32+
internal fun <T> Handler<T>.withErrorHandler(errorHandler: Handler<JsonValue>): Handler<T> =
3733
object : Handler<T> {
38-
override fun handle(response: HttpResponse): T {
34+
override fun handle(response: HttpResponse): T =
3935
when (val statusCode = response.statusCode()) {
40-
in 200..299 -> {
41-
return this@withErrorHandler.handle(response)
42-
}
43-
400 -> {
44-
val buffered = response.buffered()
45-
throw BadRequestException(
46-
buffered.headers(),
47-
stringHandler().handle(buffered),
48-
errorHandler.handle(buffered),
49-
)
50-
}
51-
401 -> {
52-
val buffered = response.buffered()
53-
throw UnauthorizedException(
54-
buffered.headers(),
55-
stringHandler().handle(buffered),
56-
errorHandler.handle(buffered),
57-
)
58-
}
59-
403 -> {
60-
val buffered = response.buffered()
61-
throw PermissionDeniedException(
62-
buffered.headers(),
63-
stringHandler().handle(buffered),
64-
errorHandler.handle(buffered),
65-
)
66-
}
67-
404 -> {
68-
val buffered = response.buffered()
69-
throw NotFoundException(
70-
buffered.headers(),
71-
stringHandler().handle(buffered),
72-
errorHandler.handle(buffered),
73-
)
74-
}
75-
422 -> {
76-
val buffered = response.buffered()
77-
throw UnprocessableEntityException(
78-
buffered.headers(),
79-
stringHandler().handle(buffered),
80-
errorHandler.handle(buffered),
81-
)
82-
}
83-
429 -> {
84-
val buffered = response.buffered()
85-
throw RateLimitException(
86-
buffered.headers(),
87-
stringHandler().handle(buffered),
88-
errorHandler.handle(buffered),
89-
)
90-
}
91-
in 500..599 -> {
92-
val buffered = response.buffered()
93-
throw InternalServerException(
94-
statusCode,
95-
buffered.headers(),
96-
stringHandler().handle(buffered),
97-
errorHandler.handle(buffered),
98-
)
99-
}
100-
else -> {
101-
val buffered = response.buffered()
102-
throw UnexpectedStatusCodeException(
103-
statusCode,
104-
buffered.headers(),
105-
stringHandler().handle(buffered),
106-
errorHandler.handle(buffered),
107-
)
108-
}
36+
in 200..299 -> this@withErrorHandler.handle(response)
37+
400 ->
38+
throw BadRequestException.builder()
39+
.headers(response.headers())
40+
.body(errorHandler.handle(response))
41+
.build()
42+
401 ->
43+
throw UnauthorizedException.builder()
44+
.headers(response.headers())
45+
.body(errorHandler.handle(response))
46+
.build()
47+
403 ->
48+
throw PermissionDeniedException.builder()
49+
.headers(response.headers())
50+
.body(errorHandler.handle(response))
51+
.build()
52+
404 ->
53+
throw NotFoundException.builder()
54+
.headers(response.headers())
55+
.body(errorHandler.handle(response))
56+
.build()
57+
422 ->
58+
throw UnprocessableEntityException.builder()
59+
.headers(response.headers())
60+
.body(errorHandler.handle(response))
61+
.build()
62+
429 ->
63+
throw RateLimitException.builder()
64+
.headers(response.headers())
65+
.body(errorHandler.handle(response))
66+
.build()
67+
in 500..599 ->
68+
throw InternalServerException.builder()
69+
.statusCode(statusCode)
70+
.headers(response.headers())
71+
.body(errorHandler.handle(response))
72+
.build()
73+
else ->
74+
throw UnexpectedStatusCodeException.builder()
75+
.statusCode(statusCode)
76+
.headers(response.headers())
77+
.body(errorHandler.handle(response))
78+
.build()
10979
}
110-
}
11180
}
112-
113-
private fun HttpResponse.buffered(): HttpResponse {
114-
val body = body().readBytes()
115-
116-
return object : HttpResponse {
117-
override fun statusCode(): Int = this@buffered.statusCode()
118-
119-
override fun headers(): Headers = this@buffered.headers()
120-
121-
override fun body(): InputStream = ByteArrayInputStream(body)
122-
123-
override fun close() = this@buffered.close()
124-
}
125-
}
Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,74 @@
1+
// File generated from our OpenAPI spec by Stainless.
2+
13
package org.onebusaway.errors
24

5+
import org.onebusaway.core.JsonValue
6+
import org.onebusaway.core.checkRequired
37
import org.onebusaway.core.http.Headers
48

5-
class BadRequestException(headers: Headers, body: String, error: OnebusawaySdkError) :
6-
OnebusawaySdkServiceException(400, headers, body, error)
9+
class BadRequestException
10+
private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) :
11+
OnebusawaySdkServiceException("400: $body", cause) {
12+
13+
override fun headers(): Headers = headers
14+
15+
override fun body(): JsonValue = body
16+
17+
override fun statusCode(): Int = 400
18+
19+
fun toBuilder() = Builder().from(this)
20+
21+
companion object {
22+
23+
/**
24+
* Returns a mutable builder for constructing an instance of [BadRequestException].
25+
*
26+
* The following fields are required:
27+
* ```kotlin
28+
* .headers()
29+
* .body()
30+
* ```
31+
*/
32+
fun builder() = Builder()
33+
}
34+
35+
/** A builder for [BadRequestException]. */
36+
class Builder internal constructor() {
37+
38+
private var headers: Headers? = null
39+
private var body: JsonValue? = null
40+
private var cause: Throwable? = null
41+
42+
internal fun from(badRequestException: BadRequestException) = apply {
43+
headers = badRequestException.headers
44+
body = badRequestException.body
45+
cause = badRequestException.cause
46+
}
47+
48+
fun headers(headers: Headers) = apply { this.headers = headers }
49+
50+
fun body(body: JsonValue) = apply { this.body = body }
51+
52+
fun cause(cause: Throwable?) = apply { this.cause = cause }
53+
54+
/**
55+
* Returns an immutable instance of [BadRequestException].
56+
*
57+
* Further updates to this [Builder] will not mutate the returned instance.
58+
*
59+
* The following fields are required:
60+
* ```kotlin
61+
* .headers()
62+
* .body()
63+
* ```
64+
*
65+
* @throws IllegalStateException if any required field is unset.
66+
*/
67+
fun build(): BadRequestException =
68+
BadRequestException(
69+
checkRequired("headers", headers),
70+
checkRequired("body", body),
71+
cause,
72+
)
73+
}
74+
}
Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,85 @@
1+
// File generated from our OpenAPI spec by Stainless.
2+
13
package org.onebusaway.errors
24

5+
import org.onebusaway.core.JsonValue
6+
import org.onebusaway.core.checkRequired
37
import org.onebusaway.core.http.Headers
48

5-
class InternalServerException(
6-
statusCode: Int,
7-
headers: Headers,
8-
body: String,
9-
error: OnebusawaySdkError,
10-
) : OnebusawaySdkServiceException(statusCode, headers, body, error)
9+
class InternalServerException
10+
private constructor(
11+
private val statusCode: Int,
12+
private val headers: Headers,
13+
private val body: JsonValue,
14+
cause: Throwable?,
15+
) : OnebusawaySdkServiceException("$statusCode: $body", cause) {
16+
17+
override fun statusCode(): Int = statusCode
18+
19+
override fun headers(): Headers = headers
20+
21+
override fun body(): JsonValue = body
22+
23+
fun toBuilder() = Builder().from(this)
24+
25+
companion object {
26+
27+
/**
28+
* Returns a mutable builder for constructing an instance of [InternalServerException].
29+
*
30+
* The following fields are required:
31+
* ```kotlin
32+
* .statusCode()
33+
* .headers()
34+
* .body()
35+
* ```
36+
*/
37+
fun builder() = Builder()
38+
}
39+
40+
/** A builder for [InternalServerException]. */
41+
class Builder internal constructor() {
42+
43+
private var statusCode: Int? = null
44+
private var headers: Headers? = null
45+
private var body: JsonValue? = null
46+
private var cause: Throwable? = null
47+
48+
internal fun from(internalServerException: InternalServerException) = apply {
49+
statusCode = internalServerException.statusCode
50+
headers = internalServerException.headers
51+
body = internalServerException.body
52+
cause = internalServerException.cause
53+
}
54+
55+
fun statusCode(statusCode: Int) = apply { this.statusCode = statusCode }
56+
57+
fun headers(headers: Headers) = apply { this.headers = headers }
58+
59+
fun body(body: JsonValue) = apply { this.body = body }
60+
61+
fun cause(cause: Throwable?) = apply { this.cause = cause }
62+
63+
/**
64+
* Returns an immutable instance of [InternalServerException].
65+
*
66+
* Further updates to this [Builder] will not mutate the returned instance.
67+
*
68+
* The following fields are required:
69+
* ```kotlin
70+
* .statusCode()
71+
* .headers()
72+
* .body()
73+
* ```
74+
*
75+
* @throws IllegalStateException if any required field is unset.
76+
*/
77+
fun build(): InternalServerException =
78+
InternalServerException(
79+
checkRequired("statusCode", statusCode),
80+
checkRequired("headers", headers),
81+
checkRequired("body", body),
82+
cause,
83+
)
84+
}
85+
}

0 commit comments

Comments
 (0)