|
| 1 | +@file:JvmName("ErrorHandler") |
| 2 | + |
| 3 | +package com.tryfinch.api.core.handlers |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.json.JsonMapper |
| 6 | +import com.google.common.collect.ListMultimap |
| 7 | +import com.tryfinch.api.core.http.HttpResponse |
| 8 | +import com.tryfinch.api.core.http.HttpResponse.Handler |
| 9 | +import com.tryfinch.api.errors.BadRequestException |
| 10 | +import com.tryfinch.api.errors.FinchError |
| 11 | +import com.tryfinch.api.errors.InternalServerException |
| 12 | +import com.tryfinch.api.errors.NotFoundException |
| 13 | +import com.tryfinch.api.errors.PermissionDeniedException |
| 14 | +import com.tryfinch.api.errors.RateLimitException |
| 15 | +import com.tryfinch.api.errors.UnauthorizedException |
| 16 | +import com.tryfinch.api.errors.UnexpectedStatusCodeException |
| 17 | +import com.tryfinch.api.errors.UnprocessableEntityException |
| 18 | +import java.io.ByteArrayInputStream |
| 19 | +import java.io.InputStream |
| 20 | + |
| 21 | +@JvmSynthetic |
| 22 | +internal fun errorHandler(jsonMapper: JsonMapper): Handler<FinchError> { |
| 23 | + val handler = jsonHandler<FinchError>(jsonMapper) |
| 24 | + |
| 25 | + return object : Handler<FinchError> { |
| 26 | + override fun handle(response: HttpResponse): FinchError = |
| 27 | + try { |
| 28 | + handler.handle(response) |
| 29 | + } catch (e: Exception) { |
| 30 | + FinchError.builder().build() |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +@JvmSynthetic |
| 36 | +internal fun <T> Handler<T>.withErrorHandler(errorHandler: Handler<FinchError>): Handler<T> = |
| 37 | + object : Handler<T> { |
| 38 | + override fun handle(response: HttpResponse): T { |
| 39 | + 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 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 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(): ListMultimap<String, String> = this@buffered.headers() |
| 120 | + |
| 121 | + override fun body(): InputStream = ByteArrayInputStream(body) |
| 122 | + |
| 123 | + override fun close() = this@buffered.close() |
| 124 | + } |
| 125 | +} |
0 commit comments