Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix kotlin exceptions #305

Merged
merged 3 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/SDK/Language/Kotlin.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,6 @@ public function getFiles()
'template' => '/kotlin/src/main/kotlin/io/appwrite/extensions/JsonExtensions.kt.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/Error.kt',
'template' => '/kotlin/src/main/kotlin/io/appwrite/models/Error.kt.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/services/BaseService.kt',
Expand Down
14 changes: 4 additions & 10 deletions templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package {{ sdk.namespace | caseDot }}

import com.google.gson.Gson
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
import {{ sdk.namespace | caseDot }}.extensions.JsonExtensions.fromJson
import {{ sdk.namespace | caseDot }}.models.Error
import {{ sdk.namespace | caseDot }}.extensions.fromJson
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -273,16 +272,11 @@ class Client @JvmOverloads constructor(

val contentType: String = response.headers["content-type"] ?: ""
val error = if (contentType.contains("application/json", ignoreCase = true)) {
bodyString.fromJson(Error::class.java)
bodyString.fromJson()
} else {
Error(bodyString, response.code)
{{ spec.title | caseUcfirst }}Exception(bodyString, response.code)
}

it.cancel(AppwriteException(
error.message,
error.code,
bodyString
))
it.cancel(error)
}
it.resume(response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package {{ sdk.namespace | caseDot }}.exceptions
import java.lang.Exception

class {{spec.title | caseUcfirst}}Exception(
message: String? = null,
override val message: String? = null,
val code: Int? = null,
val response: String? = null
) : Exception(message)
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,33 @@ package {{ sdk.namespace | caseDot }}.extensions

import com.google.gson.Gson

object JsonExtensions {
val gson = Gson()

fun Any.toJson(): String =
Gson().toJson(this)
fun Any.toJson(): String =
gson.toJson(this)

fun <T> String.fromJson(clazz: Class<T>): T =
Gson().fromJson(this, clazz)
fun <T> String.fromJson(clazz: Class<T>): T =
gson.fromJson(this, clazz)

inline fun <reified T> String.fromJson(): T =
gson.fromJson(this, T::class.java)

fun <T> Any.jsonCast(to: Class<T>): T =
toJson().fromJson(to)

inline fun <reified T> Any.jsonCast(): T =
toJson().fromJson(T::class.java)

fun <T> Any.tryJsonCast(to: Class<T>): T? = try {
toJson().fromJson(to)
} catch (ex: Exception) {
ex.printStackTrace()
null
}

inline fun <reified T> Any.tryJsonCast(): T? = try {
toJson().fromJson(T::class.java)
} catch (ex: Exception) {
ex.printStackTrace()
null
}

This file was deleted.

2 changes: 1 addition & 1 deletion tests/SDKTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class SDKTest extends TestCase
'envs' => [
'java-8' => 'docker run --rm -v $(pwd):/app -w /app/tests/sdks/kotlin openjdk:8-jdk-alpine sh -c "./gradlew :test -q && cat result.txt"',
],
'supportException' => false,
'supportException' => true,
],

'swift-server' => [
Expand Down