Skip to content

Commit

Permalink
Fix RequestExecutor error handling & reformat code/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
smaugfm committed Jul 10, 2023
1 parent 2d0c011 commit 301fb60
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 151 deletions.
1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

124 changes: 0 additions & 124 deletions .idea/uiDesigner.xml

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {
}

dependencies {
implementation("io.github.smaugfm:lunchmoney:1.0.0")
implementation("io.github.smaugfm:lunchmoney:1.0.1")
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins {
}

group = "io.github.smaugfm"
version = "1.0.0"
version = "1.0.1"
val isReleaseVersion = !version.toString().endsWith("SNAPSHOT")

repositories {
Expand Down
2 changes: 2 additions & 0 deletions detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<ID>LongParameterList:LunchmoneyApi.kt$LunchmoneyApi$( name: String, typeName: LunchmoneyAssetType, balance: BigDecimal, subtypeName: String? = null, displayName: String? = null, balanceAsOf: Instant? = null, currency: Currency? = null, institutionName: String? = null, closedOn: LocalDate? = null, excludeTransactions: Boolean? = null )</ID>
<ID>LongParameterList:LunchmoneyApi.kt$LunchmoneyApi$( tagId: Long? = null, recurringId: Long? = null, plaidAccountId: Long? = null, categoryId: Long? = null, assetId: Long? = null, groupId: Long? = null, isGroup: Boolean? = null, status: LunchmoneyTransactionStatus? = null, offset: Long? = null, limit: Long? = null, startDate: LocalDate? = null, endDate: LocalDate? = null, debitAsNegative: Boolean? = null, pending: Boolean? = null )</ID>
<ID>LongParameterList:LunchmoneyApi.kt$LunchmoneyApi$( transactions: List&lt;LunchmoneyInsertTransaction>, applyRules: Boolean? = null, skipDuplicates: Boolean? = null, checkForRecurring: Boolean? = null, debitAsNegative: Boolean? = null, skipBalanceUpdate: Boolean? = null )</ID>
<ID>TooGenericExceptionCaught:RequestExecutor.kt$RequestExecutor$error: Throwable</ID>
<ID>TooManyFunctions:LunchmoneyApi.kt$LunchmoneyApi : LunchmoneyApiInternal</ID>
<ID>UtilityClassWithPublicConstructor:TestBase.kt$TestBase</ID>
</ManuallySuppressedIssues>
</SmellBaseline>
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ internal class RequestExecutor(
.send(Mono.just(body))
.responseSingle { resp, byteBufMono ->
processResponse(resp, byteBufMono, responseSerializer)
.doOnNext { model ->
log.debug { "Response (${resp.status()}): $model" }
.doOnNext {
log.debug { "Response (${resp.status()}): $it" }
}
}.doOnSubscribe {
log.debug { "Performing Lunchmoney API request $request" }
Expand Down Expand Up @@ -100,7 +100,7 @@ internal class RequestExecutor(
)
} catch (e: LunchmoneyApiResponseException) {
Mono.error(e)
} catch (error: IOException) {
} catch (error: Throwable) {
Mono.error(
LunchmoneyApiResponseException(
body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,22 @@ class LunchmoneyApiResponseException : LunchmoneyApiException {
apiErrorResponse: ApiErrorResponse,
statusCode: Int
) : super(
apiErrorResponse.message ?: apiErrorResponse.error?.joinToString(", ")
?: "Received erroneous response from Lunchmoney API"
errorMessage(apiErrorResponse)
) {
this.apiErrorResponse = apiErrorResponse
this.body = body
this.statusCode = statusCode
}

companion object {
private fun errorMessage(apiErrorResponse: ApiErrorResponse): String {
val msg = apiErrorResponse.message
if (msg != null) {
return msg
}

return apiErrorResponse.error?.joinToString(", ")
?: "Received erroneous response from Lunchmoney API"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,13 @@ import io.github.smaugfm.lunchmoney.model.LunchmoneyInsertTransaction
import io.github.smaugfm.lunchmoney.model.LunchmoneyTransaction
import io.github.smaugfm.lunchmoney.model.LunchmoneyUpdateTransaction
import io.github.smaugfm.lunchmoney.model.LunchmoneyUser
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable
import reactor.tools.agent.ReactorDebugAgent
import java.math.BigDecimal
import java.time.LocalDate

@EnabledIfEnvironmentVariable(named = "LUNCHMONEY_TEST_TOKEN", matches = "\\w+")
class JsonSchemaUpToDateTest {

companion object {
@JvmStatic
@BeforeAll
fun initReactor() {
ReactorDebugAgent.init()
}
}
class JsonSchemaUpToDateTest : TestBase() {

private val api = LunchmoneyApi(System.getenv("LUNCHMONEY_TEST_TOKEN"))

Expand Down
14 changes: 14 additions & 0 deletions src/test/kotlin/io/github/smaugfm/lunchmoney/TestBase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.smaugfm.lunchmoney

import org.junit.jupiter.api.BeforeAll
import reactor.tools.agent.ReactorDebugAgent

open class TestBase {
companion object {
@BeforeAll
@JvmStatic
fun initReactorTools() {
ReactorDebugAgent.init()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.mockserver.model.HttpResponse
import org.mockserver.model.NottableString
import org.slf4j.event.Level

open class TestMockServerBase {
abstract class TestMockServerBase : TestBase() {
protected val api: LunchmoneyApiInternal = LunchmoneyTest(
TOKEN,
BASE_URL,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.github.smaugfm.lunchmoney.api

import assertk.all
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNull
import assertk.assertions.prop
import io.github.smaugfm.lunchmoney.TestMockServerBase
import io.github.smaugfm.lunchmoney.exception.LunchmoneyApiResponseException
import io.github.smaugfm.lunchmoney.request.user.GetCurrentUserRequest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.mockserver.model.HttpRequest.request
import org.mockserver.model.HttpResponse.response
import org.mockserver.model.MediaType

class RequestExecutorTest : TestMockServerBase() {
@Test
fun unexpectedResponseTest() {
val body = """
<!DOCTYPE html>
<html>
</html>
""".trimIndent()
mockServer
.`when`(
request("/me")
.withMethod("GET")
).respond(
response()
.withStatusCode(200)
.withContentType(MediaType.TEXT_HTML_UTF_8)
.withStatusCode(500)
.withBody(body)
)
assertThat(
assertThrows<Throwable> {
api.execute(GetCurrentUserRequest()).block()
}
)
.prop(Throwable::cause)
.transform { it as LunchmoneyApiResponseException }
.all {
prop(LunchmoneyApiResponseException::statusCode)
.isEqualTo(500)
prop(LunchmoneyApiResponseException::body)
.isEqualTo(body)
prop(LunchmoneyApiResponseException::apiErrorResponse)
.isNull()
}
}
}

0 comments on commit 301fb60

Please sign in to comment.