Skip to content

Commit 2e55044

Browse files
Merge pull request #305 from appwrite/fix-kotlin-exceptions
Fix kotlin exceptions
2 parents 8ad4ddf + b03127d commit 2e55044

File tree

6 files changed

+33
-29
lines changed

6 files changed

+33
-29
lines changed

src/SDK/Language/Kotlin.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,6 @@ public function getFiles()
352352
'template' => '/kotlin/src/main/kotlin/io/appwrite/extensions/JsonExtensions.kt.twig',
353353
'minify' => false,
354354
],
355-
[
356-
'scope' => 'default',
357-
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/Error.kt',
358-
'template' => '/kotlin/src/main/kotlin/io/appwrite/models/Error.kt.twig',
359-
'minify' => false,
360-
],
361355
[
362356
'scope' => 'default',
363357
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/services/BaseService.kt',

templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package {{ sdk.namespace | caseDot }}
22

33
import com.google.gson.Gson
44
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
5-
import {{ sdk.namespace | caseDot }}.extensions.JsonExtensions.fromJson
6-
import {{ sdk.namespace | caseDot }}.models.Error
5+
import {{ sdk.namespace | caseDot }}.extensions.fromJson
76
import kotlinx.coroutines.CoroutineScope
87
import kotlinx.coroutines.Dispatchers
98
import kotlinx.coroutines.Job
@@ -273,16 +272,11 @@ class Client @JvmOverloads constructor(
273272

274273
val contentType: String = response.headers["content-type"] ?: ""
275274
val error = if (contentType.contains("application/json", ignoreCase = true)) {
276-
bodyString.fromJson(Error::class.java)
275+
bodyString.fromJson()
277276
} else {
278-
Error(bodyString, response.code)
277+
{{ spec.title | caseUcfirst }}Exception(bodyString, response.code)
279278
}
280-
281-
it.cancel(AppwriteException(
282-
error.message,
283-
error.code,
284-
bodyString
285-
))
279+
it.cancel(error)
286280
}
287281
it.resume(response)
288282
}

templates/kotlin/src/main/kotlin/io/appwrite/exceptions/Exception.kt.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package {{ sdk.namespace | caseDot }}.exceptions
33
import java.lang.Exception
44

55
class {{spec.title | caseUcfirst}}Exception(
6-
message: String? = null,
6+
override val message: String? = null,
77
val code: Int? = null,
88
val response: String? = null
99
) : Exception(message)

templates/kotlin/src/main/kotlin/io/appwrite/extensions/JsonExtensions.kt.twig

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,33 @@ package {{ sdk.namespace | caseDot }}.extensions
22

33
import com.google.gson.Gson
44

5-
object JsonExtensions {
5+
val gson = Gson()
66

7-
fun Any.toJson(): String =
8-
Gson().toJson(this)
7+
fun Any.toJson(): String =
8+
gson.toJson(this)
99

10-
fun <T> String.fromJson(clazz: Class<T>): T =
11-
Gson().fromJson(this, clazz)
10+
fun <T> String.fromJson(clazz: Class<T>): T =
11+
gson.fromJson(this, clazz)
12+
13+
inline fun <reified T> String.fromJson(): T =
14+
gson.fromJson(this, T::class.java)
15+
16+
fun <T> Any.jsonCast(to: Class<T>): T =
17+
toJson().fromJson(to)
18+
19+
inline fun <reified T> Any.jsonCast(): T =
20+
toJson().fromJson(T::class.java)
21+
22+
fun <T> Any.tryJsonCast(to: Class<T>): T? = try {
23+
toJson().fromJson(to)
24+
} catch (ex: Exception) {
25+
ex.printStackTrace()
26+
null
27+
}
28+
29+
inline fun <reified T> Any.tryJsonCast(): T? = try {
30+
toJson().fromJson(T::class.java)
31+
} catch (ex: Exception) {
32+
ex.printStackTrace()
33+
null
1234
}

templates/kotlin/src/main/kotlin/io/appwrite/models/Error.kt.twig

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/SDKTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class SDKTest extends TestCase
9393
'envs' => [
9494
'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"',
9595
],
96-
'supportException' => false,
96+
'supportException' => true,
9797
],
9898

9999
'swift-server' => [

0 commit comments

Comments
 (0)