Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,125 +1,80 @@
@file:JvmName("ErrorHandler")
// File generated from our OpenAPI spec by Stainless.

package org.onebusaway.core.handlers

import com.fasterxml.jackson.databind.json.JsonMapper
import java.io.ByteArrayInputStream
import java.io.InputStream
import org.onebusaway.core.http.Headers
import org.onebusaway.core.JsonMissing
import org.onebusaway.core.JsonValue
import org.onebusaway.core.http.HttpResponse
import org.onebusaway.core.http.HttpResponse.Handler
import org.onebusaway.errors.BadRequestException
import org.onebusaway.errors.InternalServerException
import org.onebusaway.errors.NotFoundException
import org.onebusaway.errors.OnebusawaySdkError
import org.onebusaway.errors.PermissionDeniedException
import org.onebusaway.errors.RateLimitException
import org.onebusaway.errors.UnauthorizedException
import org.onebusaway.errors.UnexpectedStatusCodeException
import org.onebusaway.errors.UnprocessableEntityException

internal fun errorHandler(jsonMapper: JsonMapper): Handler<OnebusawaySdkError> {
val handler = jsonHandler<OnebusawaySdkError>(jsonMapper)
internal fun errorHandler(jsonMapper: JsonMapper): Handler<JsonValue> {
val handler = jsonHandler<JsonValue>(jsonMapper)

return object : Handler<OnebusawaySdkError> {
override fun handle(response: HttpResponse): OnebusawaySdkError =
return object : Handler<JsonValue> {
override fun handle(response: HttpResponse): JsonValue =
try {
handler.handle(response)
} catch (e: Exception) {
OnebusawaySdkError.builder().build()
JsonMissing.of()
}
}
}

internal fun <T> Handler<T>.withErrorHandler(
errorHandler: Handler<OnebusawaySdkError>
): Handler<T> =
internal fun <T> Handler<T>.withErrorHandler(errorHandler: Handler<JsonValue>): Handler<T> =
object : Handler<T> {
override fun handle(response: HttpResponse): T {
override fun handle(response: HttpResponse): T =
when (val statusCode = response.statusCode()) {
in 200..299 -> {
return [email protected](response)
}
400 -> {
val buffered = response.buffered()
throw BadRequestException(
buffered.headers(),
stringHandler().handle(buffered),
errorHandler.handle(buffered),
)
}
401 -> {
val buffered = response.buffered()
throw UnauthorizedException(
buffered.headers(),
stringHandler().handle(buffered),
errorHandler.handle(buffered),
)
}
403 -> {
val buffered = response.buffered()
throw PermissionDeniedException(
buffered.headers(),
stringHandler().handle(buffered),
errorHandler.handle(buffered),
)
}
404 -> {
val buffered = response.buffered()
throw NotFoundException(
buffered.headers(),
stringHandler().handle(buffered),
errorHandler.handle(buffered),
)
}
422 -> {
val buffered = response.buffered()
throw UnprocessableEntityException(
buffered.headers(),
stringHandler().handle(buffered),
errorHandler.handle(buffered),
)
}
429 -> {
val buffered = response.buffered()
throw RateLimitException(
buffered.headers(),
stringHandler().handle(buffered),
errorHandler.handle(buffered),
)
}
in 500..599 -> {
val buffered = response.buffered()
throw InternalServerException(
statusCode,
buffered.headers(),
stringHandler().handle(buffered),
errorHandler.handle(buffered),
)
}
else -> {
val buffered = response.buffered()
throw UnexpectedStatusCodeException(
statusCode,
buffered.headers(),
stringHandler().handle(buffered),
errorHandler.handle(buffered),
)
}
in 200..299 -> [email protected](response)
400 ->
throw BadRequestException.builder()
.headers(response.headers())
.body(errorHandler.handle(response))
.build()
401 ->
throw UnauthorizedException.builder()
.headers(response.headers())
.body(errorHandler.handle(response))
.build()
403 ->
throw PermissionDeniedException.builder()
.headers(response.headers())
.body(errorHandler.handle(response))
.build()
404 ->
throw NotFoundException.builder()
.headers(response.headers())
.body(errorHandler.handle(response))
.build()
422 ->
throw UnprocessableEntityException.builder()
.headers(response.headers())
.body(errorHandler.handle(response))
.build()
429 ->
throw RateLimitException.builder()
.headers(response.headers())
.body(errorHandler.handle(response))
.build()
in 500..599 ->
throw InternalServerException.builder()
.statusCode(statusCode)
.headers(response.headers())
.body(errorHandler.handle(response))
.build()
else ->
throw UnexpectedStatusCodeException.builder()
.statusCode(statusCode)
.headers(response.headers())
.body(errorHandler.handle(response))
.build()
}
}
}

private fun HttpResponse.buffered(): HttpResponse {
val body = body().readBytes()

return object : HttpResponse {
override fun statusCode(): Int = [email protected]()

override fun headers(): Headers = [email protected]()

override fun body(): InputStream = ByteArrayInputStream(body)

override fun close() = [email protected]()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,74 @@
// File generated from our OpenAPI spec by Stainless.

package org.onebusaway.errors

import org.onebusaway.core.JsonValue
import org.onebusaway.core.checkRequired
import org.onebusaway.core.http.Headers

class BadRequestException(headers: Headers, body: String, error: OnebusawaySdkError) :
OnebusawaySdkServiceException(400, headers, body, error)
class BadRequestException
private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) :
OnebusawaySdkServiceException("400: $body", cause) {

override fun headers(): Headers = headers

override fun body(): JsonValue = body

override fun statusCode(): Int = 400

fun toBuilder() = Builder().from(this)

companion object {

/**
* Returns a mutable builder for constructing an instance of [BadRequestException].
*
* The following fields are required:
* ```kotlin
* .headers()
* .body()
* ```
*/
fun builder() = Builder()
}

/** A builder for [BadRequestException]. */
class Builder internal constructor() {

private var headers: Headers? = null
private var body: JsonValue? = null
private var cause: Throwable? = null

internal fun from(badRequestException: BadRequestException) = apply {
headers = badRequestException.headers
body = badRequestException.body
cause = badRequestException.cause
}

fun headers(headers: Headers) = apply { this.headers = headers }

fun body(body: JsonValue) = apply { this.body = body }

fun cause(cause: Throwable?) = apply { this.cause = cause }

/**
* Returns an immutable instance of [BadRequestException].
*
* Further updates to this [Builder] will not mutate the returned instance.
*
* The following fields are required:
* ```kotlin
* .headers()
* .body()
* ```
*
* @throws IllegalStateException if any required field is unset.
*/
fun build(): BadRequestException =
BadRequestException(
checkRequired("headers", headers),
checkRequired("body", body),
cause,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,85 @@
// File generated from our OpenAPI spec by Stainless.

package org.onebusaway.errors

import org.onebusaway.core.JsonValue
import org.onebusaway.core.checkRequired
import org.onebusaway.core.http.Headers

class InternalServerException(
statusCode: Int,
headers: Headers,
body: String,
error: OnebusawaySdkError,
) : OnebusawaySdkServiceException(statusCode, headers, body, error)
class InternalServerException
private constructor(
private val statusCode: Int,
private val headers: Headers,
private val body: JsonValue,
cause: Throwable?,
) : OnebusawaySdkServiceException("$statusCode: $body", cause) {

override fun statusCode(): Int = statusCode

override fun headers(): Headers = headers

override fun body(): JsonValue = body

fun toBuilder() = Builder().from(this)

companion object {

/**
* Returns a mutable builder for constructing an instance of [InternalServerException].
*
* The following fields are required:
* ```kotlin
* .statusCode()
* .headers()
* .body()
* ```
*/
fun builder() = Builder()
}

/** A builder for [InternalServerException]. */
class Builder internal constructor() {

private var statusCode: Int? = null
private var headers: Headers? = null
private var body: JsonValue? = null
private var cause: Throwable? = null

internal fun from(internalServerException: InternalServerException) = apply {
statusCode = internalServerException.statusCode
headers = internalServerException.headers
body = internalServerException.body
cause = internalServerException.cause
}

fun statusCode(statusCode: Int) = apply { this.statusCode = statusCode }

fun headers(headers: Headers) = apply { this.headers = headers }

fun body(body: JsonValue) = apply { this.body = body }

fun cause(cause: Throwable?) = apply { this.cause = cause }

/**
* Returns an immutable instance of [InternalServerException].
*
* Further updates to this [Builder] will not mutate the returned instance.
*
* The following fields are required:
* ```kotlin
* .statusCode()
* .headers()
* .body()
* ```
*
* @throws IllegalStateException if any required field is unset.
*/
fun build(): InternalServerException =
InternalServerException(
checkRequired("statusCode", statusCode),
checkRequired("headers", headers),
checkRequired("body", body),
cause,
)
}
}
Loading