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

Commit

Permalink
Allow registering error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Jul 29, 2022
1 parent c10311e commit fbc91d5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/main/kotlin/com/cyb3rko/m3okotlin/Exceptions.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package com.cyb3rko.m3okotlin

class InvalidParameterException(message: String) : Exception(message)
import kotlinx.serialization.Serializable

class InvalidParameterException(message: String) : Exception(message)

@Serializable
data class CustomError(
val code: Int,
val detail: String,
val id: String,
val status: String
)
50 changes: 49 additions & 1 deletion src/main/kotlin/com/cyb3rko/m3okotlin/M3O.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import io.ktor.client.features.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.coroutines.isActive
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

object M3O {
private const val BASE_URL = "https://api.m3o.com/v1"
Expand All @@ -17,6 +20,7 @@ object M3O {
internal lateinit var ktorHttpClient: HttpClient
private lateinit var ktorHttpMultipartClient: HttpClient
private lateinit var ktorHttpRedirectClient: HttpClient
private lateinit var onReceivedError: (title: String, message: String, e: Exception) -> Unit

fun initialize(apiKey: String) {
Log.initialize()
Expand All @@ -41,6 +45,8 @@ object M3O {
header(HttpHeaders.Authorization, authorization.second)
}

installResponseValidator()

// Logging for debugging

// install(Logging) {
Expand All @@ -57,6 +63,44 @@ object M3O {
Log.i("Ktor M3O Client initialized.")
}

fun registerResponseValidation(
onReceivedError: (title: String, message: String, e: Exception) -> Unit
): M3O {
this.onReceivedError = onReceivedError
Log.i("Ktor Response Validation registered")
return this
}

private fun HttpClientConfig<*>.installResponseValidator() {
if (this@M3O::onReceivedError.isInitialized) {
this.HttpResponseValidator {
handleResponseException {
val clientException = it as? ClientRequestException
val serverException: ServerResponseException?

val response: String
if (clientException != null) {
response = clientException.response.readText()
} else {
serverException = it as? ServerResponseException
response = serverException?.response?.readText() ?: it.message.toString()
}
try {
val errorInformation = Json.decodeFromString<CustomError>(response)
val title = errorInformation.status
val message = when (errorInformation.code) {
HttpStatusCode.Unauthorized.value -> "Your API Key may be invalid"
else -> errorInformation.detail
}
onReceivedError(title, message, it as Exception)
} catch (_: Exception) {
onReceivedError("An Error Occurred", response, it as Exception)
}
}
}
}
}

fun terminate() {
if (::ktorHttpClient.isInitialized) {
ktorHttpClient.close()
Expand Down Expand Up @@ -85,6 +129,8 @@ object M3O {
header(HttpHeaders.Authorization, authorization.second)
}

installResponseValidator()

// Logging for debugging

// install(Logging) {
Expand Down Expand Up @@ -120,6 +166,8 @@ object M3O {
header(HttpHeaders.Authorization, authorization.second)
}

installResponseValidator()

// Logging for debugging

// install(Logging) {
Expand All @@ -141,4 +189,4 @@ object M3O {
val url = if (!stream) BASE_URL else BASE_URL_STREAM
return "$url/$service/$endpoint"
}
}
}

0 comments on commit fbc91d5

Please sign in to comment.