Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Room Park Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vladislav Bogdashkin
Room Park Android
Commits
19977c81
Commit
19977c81
authored
Nov 12, 2019
by
Vladislav Bogdashkin
🎣
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added custom api error resolver
parent
5f55c936
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
4 deletions
+54
-4
RetrofitModule.kt
...oompark/data/repository/api/retrofit/di/RetrofitModule.kt
+2
-4
CustomExceptionDeserializer.kt
...pository/api/retrofit/util/CustomExceptionDeserializer.kt
+52
-0
No files found.
app/src/main/java/com/biganto/visual/roompark/data/repository/api/retrofit/di/RetrofitModule.kt
View file @
19977c81
...
...
@@ -3,10 +3,7 @@ package com.biganto.visual.roompark.data.repository.api.retrofit.di
import
android.content.Context
import
com.biganto.visual.roompark.data.repository.api.retrofit.IRoomParkMobileApi
import
com.biganto.visual.roompark.data.repository.api.retrofit.raw.ErrorRaw
import
com.biganto.visual.roompark.data.repository.api.retrofit.util.DateSerializer
import
com.biganto.visual.roompark.data.repository.api.retrofit.util.ErrorsListDeserializer
import
com.biganto.visual.roompark.data.repository.api.retrofit.util.NullOnEmptyConverterFactory
import
com.biganto.visual.roompark.data.repository.api.retrofit.util.VersionValidationInterceptor
import
com.biganto.visual.roompark.data.repository.api.retrofit.util.*
import
com.biganto.visual.roompark.data.service.network.INetworkMonitor
import
com.biganto.visual.roompark.data.service.network.LiveNetworkMonitor
import
com.biganto.visual.roompark.data.service.network.NoNetworkException
...
...
@@ -46,6 +43,7 @@ class RetrofitModule{
private
fun
gsonConverterFactory
()
=
GsonConverterFactory
.
create
(
GsonBuilder
()
.
registerTypeHierarchyAdapter
(
Any
::
class
.
java
,
CustomExceptionDeserializer
<
Any
>())
.
registerTypeAdapter
(
genericType
<
MutableList
<
ErrorRaw
>>(),
ErrorsListDeserializer
())
.
registerTypeAdapter
(
Date
::
class
.
java
,
DateSerializer
())
.
setLenient
()
...
...
app/src/main/java/com/biganto/visual/roompark/data/repository/api/retrofit/util/CustomExceptionDeserializer.kt
0 → 100644
View file @
19977c81
package
com.biganto.visual.roompark.data.repository.api.retrofit.util
import
com.biganto.visual.roompark.data.repository.api.retrofit.raw.ErrorRaw
import
com.biganto.visual.roompark.domain.custom_exception.parseException
import
com.google.gson.Gson
import
com.google.gson.JsonDeserializationContext
import
com.google.gson.JsonDeserializer
import
com.google.gson.JsonElement
import
org.json.JSONArray
import
org.json.JSONObject
import
timber.log.Timber
import
java.lang.reflect.Type
/**
* Created by Vladislav Bogdashkin on 12.11.2019.
*/
class
CustomExceptionDeserializer
<
T
>
:
JsonDeserializer
<
T
>
{
override
fun
deserialize
(
json
:
JsonElement
,
typeOfT
:
Type
,
ctx
:
JsonDeserializationContext
):
T
{
val
vals
=
ArrayList
<
ErrorRaw
>()
//region valid case
when
(
json
){
is
JSONObject
->
if
(!
json
.
asJsonObject
.
keySet
().
contains
(
"errors"
))
return
Gson
().
newBuilder
().
create
().
fromJson
(
json
,
typeOfT
)
is
JSONArray
->
return
Gson
().
newBuilder
().
create
().
fromJson
(
json
,
typeOfT
)
}
//endregion valid case
//region parse errorList
val
errorList
=
json
.
asJsonObject
.
get
(
"errors"
)
Timber
.
w
(
"api errorlist: $errorList"
)
when
(
json
)
{
is
JSONArray
->
errorList
.
asJsonArray
.
forEach
{
vals
.
add
(
ctx
.
deserialize
<
T
>(
it
,
ErrorRaw
::
class
.
java
)
as
ErrorRaw
)
}
is
JSONObject
->
json
.
asJsonObject
.
get
(
"errors"
).
asJsonArray
.
forEach
{
vals
.
add
(
ctx
.
deserialize
<
T
>(
it
,
ErrorRaw
::
class
.
java
)
as
ErrorRaw
)
}
else
->
throw
RuntimeException
(
"Unexpected JSON type: "
+
json
.
javaClass
)
}
if
(
vals
.
size
==
0
)
error
(
"unhandled api result : $json"
)
throw
parseException
(
vals
.
first
())
//-> for now throws only first!
//endregion
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment