Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chore(client)!: refactor exception structure and methods #93

Merged
merged 1 commit into from
Mar 20, 2025
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,84 @@
// File generated from our OpenAPI spec by Stainless.

@file:JvmName("ErrorHandler")

package com.openlayer.api.core.handlers

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

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

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

@JvmSynthetic
internal fun <T> Handler<T>.withErrorHandler(errorHandler: Handler<OpenlayerError>): 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 this@withErrorHandler.handle(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 -> this@withErrorHandler.handle(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 = this@buffered.statusCode()

override fun headers(): Headers = this@buffered.headers()

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

override fun close() = this@buffered.close()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,80 @@
// File generated from our OpenAPI spec by Stainless.

package com.openlayer.api.errors

import com.openlayer.api.core.JsonValue
import com.openlayer.api.core.checkRequired
import com.openlayer.api.core.http.Headers
import java.util.Optional
import kotlin.jvm.optionals.getOrNull

class BadRequestException
private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) :
OpenlayerServiceException("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:
* ```java
* .headers()
* .body()
* ```
*/
@JvmStatic 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

@JvmSynthetic
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 }

/** Alias for calling [Builder.cause] with `cause.orElse(null)`. */
fun cause(cause: Optional<Throwable>) = cause(cause.getOrNull())

class BadRequestException(headers: Headers, body: String, error: OpenlayerError) :
OpenlayerServiceException(400, headers, body, error)
/**
* Returns an immutable instance of [BadRequestException].
*
* Further updates to this [Builder] will not mutate the returned instance.
*
* The following fields are required:
* ```java
* .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,91 @@
// File generated from our OpenAPI spec by Stainless.

package com.openlayer.api.errors

import com.openlayer.api.core.JsonValue
import com.openlayer.api.core.checkRequired
import com.openlayer.api.core.http.Headers
import java.util.Optional
import kotlin.jvm.optionals.getOrNull

class InternalServerException
private constructor(
private val statusCode: Int,
private val headers: Headers,
private val body: JsonValue,
cause: Throwable?,
) : OpenlayerServiceException("$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:
* ```java
* .statusCode()
* .headers()
* .body()
* ```
*/
@JvmStatic 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

@JvmSynthetic
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 }

/** Alias for calling [Builder.cause] with `cause.orElse(null)`. */
fun cause(cause: Optional<Throwable>) = cause(cause.getOrNull())

class InternalServerException(
statusCode: Int,
headers: Headers,
body: String,
error: OpenlayerError,
) : OpenlayerServiceException(statusCode, headers, body, error)
/**
* Returns an immutable instance of [InternalServerException].
*
* Further updates to this [Builder] will not mutate the returned instance.
*
* The following fields are required:
* ```java
* .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