Skip to content

Commit

Permalink
fix handling backend errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Bobiński committed Nov 13, 2024
1 parent 2f14c68 commit 3f4c030
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/net/bobinski/backend/AnalysisEndpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object AnalysisEndpoint {
suspend fun forStock(symbol: String): Analysis {
val backendData = getLock(symbol).withLock {
val info = Backend.getInfo(symbol)
if (info.name == null) throw IllegalArgumentException("Unknown symbol: $symbol")
if (info?.name == null) throw IllegalArgumentException("Unknown symbol: $symbol")

var lacking = false
var history = Backend.getHistory(symbol, Backend.Period._5y)
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/net/bobinski/source/Backend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.ktor.client.plugins.cache.HttpCache
import io.ktor.client.plugins.cache.storage.FileStorage
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.get
import io.ktor.http.isSuccess
import io.ktor.serialization.kotlinx.json.json
import net.bobinski.Config
import net.bobinski.data.BasicInfo
Expand All @@ -29,13 +30,13 @@ object Backend {
suspend fun getHistory(symbol: String, period: Period): Collection<HistoricalPrice> {
return client
.get("${Config.backendUrl}/history/$symbol/${period.value}")
.body()
.run { if (status.isSuccess()) body() else emptySet() }
}

suspend fun getInfo(symbol: String): BasicInfo {
suspend fun getInfo(symbol: String): BasicInfo? {
return client
.get("${Config.backendUrl}/info/$symbol")
.body()
.run { if (status.isSuccess()) body() else null }
}

enum class Period(val value: String) {
Expand Down

0 comments on commit 3f4c030

Please sign in to comment.