https://kotlinlang.org logo
#android
Title
# android
d

dewildte

12/09/2018, 1:52 AM
Is there an adapter for Retrofit that can generate sealed classes from error codes?
r

rkeazor

12/09/2018, 2:20 AM
You can create a custom TypeAdapter and register it
d

dewildte

12/09/2018, 2:49 AM
@rkeazor Do you know where I can find decent documentation on how to do so?
r

rkeazor

12/09/2018, 3:01 AM
Sorry I mean Interceptor
g

gildor

12/09/2018, 4:33 AM
I don't think that global error handler or interceptor will help for this
To achieve that type adapter is also not enough (type adapter works only with successful response types), you need own CallAdapter.Factory, you can get implementation of existing factory that you use (RxJava, coroutines etc) and modify it to propagate response errors as successful result with some sealed class
👍 1
r

rkeazor

12/09/2018, 4:07 PM
i see your point, interceptor will only allow a custom throwable. Your defiantly right @gildor. But keep in mind that means that the return type of that call method would be that Sealed Class. So you would need a success path as well, one that get the actual result… So the sealed class shouldn’t just have child classes for errors
g

gildor

12/09/2018, 4:39 PM
Yes, and using CallAdapter you can do this
d

dewildte

12/09/2018, 10:10 PM
Thanks guys! It's good to know I can achieve this kind of behavior.
34 Views