Skip to content
This repository has been archived by the owner on Nov 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #12 from GSculerlor/better-throwing
Browse files Browse the repository at this point in the history
Better exception handing
  • Loading branch information
GSculerlor committed May 5, 2020
2 parents 0fe939e + c22a6d1 commit 51e50c7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/moe/ganen/jikankt/JikanKt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import moe.ganen.jikankt.utils.InterfaceAdapter
import moe.ganen.jikankt.utils.deserialize

object JikanKt {
private val restClient = RestClient()
internal var restClient = RestClient(false)
private val gson = GsonBuilder().registerTypeAdapter(Entity::class.java, InterfaceAdapter<Entity>()).create()

//region Anime
Expand Down
45 changes: 31 additions & 14 deletions src/main/kotlin/moe/ganen/jikankt/connection/RestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import moe.ganen.jikankt.JikanClient
import moe.ganen.jikankt.exception.JikanException
import moe.ganen.jikankt.exception.TooManyRequestException

class RestClient : JikanClient() {
class RestClient(private val isDebug: Boolean) : JikanClient() {
private val client = httpClient
private val gson = Gson()

Expand Down Expand Up @@ -49,26 +48,44 @@ class RestClient : JikanClient() {
logger.debug("Response from Jikan: ${response.status.value}, body: $json")

if (response.status.value !in 200..299) {
if (response.status.value in 500..599)
throw Exception("An internal server error has occurred, code: ${response.status.value}")
if (response.status.value in 500..599) {
val ex = Exception("An internal server error has occurred, code: ${response.status.value}")
if (isDebug)
throw ex
else
exceptionHandler(ex)
} else {
val ex = JikanException(
"Jikan API returns code ${response.status.value} and body ${json?.toString()}",
response.status.value
)

throw JikanException(
"Jikan API returns code ${response.status.value} and body ${json?.toString()}",
response.status.value
)
if (isDebug)
throw ex
else
exceptionHandler(ex)
}
}

return json ?: JsonObject()
} catch (ex: JikanException) {
throw ex
} catch (ex: TooManyRequestException) {
throw ex
} catch (ex: Exception) {
logger.error(ex) { "An unexpected error has occurred!" }
throw ex
if (!isDebug) {
return exceptionHandler(ex, "An unexpected error has occurred!")
} else
throw ex
}
}

private fun exceptionHandler(ex: java.lang.Exception, message: String? = null) : JsonObject {
if (message.isNullOrEmpty())
logger.error { "Something went wrong! Exception: ${ex.localizedMessage}" }
else
logger.error(ex) { message }

//Will return empty json object instead
return JsonObject()
}

companion object {
private const val BASE_URL = "https://api.jikan.moe/v3/"
}
Expand Down
23 changes: 20 additions & 3 deletions src/test/kotlin/moe/ganen/jikankt/TestCaseSearch.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package moe.ganen.jikankt

import kotlinx.coroutines.runBlocking
import moe.ganen.jikankt.connection.RestClient
import moe.ganen.jikankt.exception.JikanException
import moe.ganen.jikankt.models.base.types.CharacterSearchSubEntity
import moe.ganen.jikankt.models.base.types.MalSubEntity
import moe.ganen.jikankt.models.base.types.PeopleSearchSubEntity
import moe.ganen.jikankt.models.search.CharacterSearchResult
import org.junit.Assert.assertEquals
import org.junit.Test

Expand Down Expand Up @@ -34,9 +36,24 @@ class TestCaseSearch {
assertEquals(expected, result)
}

@Test
fun `test search random character return empty`() {
val jikan = JikanKt.apply {
restClient = RestClient(false)
}

val expected = CharacterSearchResult()
val result = runBlocking { jikan.searchCharacter("Bjir") }

assertEquals(expected, result)
}

@Test(expected = JikanException::class)
fun `test search random character`() {
runBlocking { JikanKt.searchCharacter("Bjir").results?.get(0) }
fun `test search random character return exception`() {
val jikan = JikanKt.apply {
restClient = RestClient(true)
}
runBlocking { jikan.searchCharacter("Bjir").results?.get(0) }
}

@Test
Expand All @@ -52,7 +69,7 @@ class TestCaseSearch {
assertEquals(expected.name, result?.name)
}

@Test()
@Test
fun `test search random people`() {
val result = runBlocking { JikanKt.searchPeople("Bjir").results }
assertEquals(0, result?.size)
Expand Down

0 comments on commit 51e50c7

Please sign in to comment.