File tree Expand file tree Collapse file tree 6 files changed +33
-29
lines changed
templates/kotlin/src/main/kotlin/io/appwrite Expand file tree Collapse file tree 6 files changed +33
-29
lines changed Original file line number Diff line number Diff 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 ' ,
Original file line number Diff line number Diff line change @@ -2,8 +2,7 @@ package {{ sdk.namespace | caseDot }}
22
33import com.google.gson.Gson
44import {{ 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
76import kotlinx.coroutines.CoroutineScope
87import kotlinx.coroutines.Dispatchers
98import 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 }
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ package {{ sdk.namespace | caseDot }}.exceptions
33import java.lang.Exception
44
55class {{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)
Original file line number Diff line number Diff line change @@ -2,11 +2,33 @@ package {{ sdk.namespace | caseDot }}.extensions
22
33import 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}
Load Diff This file was deleted.
Original file line number Diff line number Diff 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 ' => [
You can’t perform that action at this time.
0 commit comments