-
Notifications
You must be signed in to change notification settings - Fork 62
Added [NetworkResponse.Error] an interface to generalize errors #31
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
Conversation
|
Thank you for this PR. I've gone through the code and I like how it simplifies generic error handling. I'm not sure if I'm a fan of adding even more components to the The crux of these changes to is to enable access to a I think extension properties might be the way to go here, as they allow extension of the class without modifying its definition: when (val response = api.foo()) {
is NetworkResponse.Success -> { /* ... */ }
else -> {
val error = response.error // Extension property on `NetworkResponse` class
/* ..handle error */
}
}
val NetworkResponse.error: Throwable?
get() = when (this) {
is NetworkResponse.Success -> null
is NetworkResponse.ServerError -> Exception(this.body?.toString())
is NetworkResponse.NetworkError -> error
is NetworkResponse.UnknownError -> error
}What do you think this design? |
|
I see your point but I don't fully agree with it, let me explain why. First about the A possible use case is an event logger, I want to know and report that there was an error and be able to see it in my tracking because an error means the user wasn't able to complete the action as intended therefore I want to know why. Regarding the extension property. The property can be accessed regardless of the result, so even if the action was successful, where there isn't an error value. To solve that you have to make it "Was the action not successful and is the error not null", now I know it can be run by calling But one of the great things about your library is the removal of the checks by leveraging the kotlin compiler. |
|
I'm still in split minds about this change. I'll take some time to think about it, and get back to you by this weekend. |
|
That's fine, I think all changes be beneficial, now it's just a design question and that's up to you, it's your creation. I can vouch for my suggestion because I currently use it, but again it's totally up to you. I would anyway be happy to be a contributor |
|
Hi, So I wanted to see an implementation with the new kotlin 1.5 I used different names to not cause confusion but that's just for testing. Given a new class The following cases compile and But the following cases won't compile because the In addition, because Here is a small change that makes the And when you use the So you can use as much or as little information as needed I'm not saying you should change it to this but I just wanted to share another approach and I would love to hear your thoughts |
|
Huh, the sealed interfaces approach does look nice. We might be able to switch to it in a v5 release, since it's a big breaking change. |
|
Awesome, do you want me to open a v5 branch with this change and some cleanup of the deprecated classes? |
|
I don't know if Github allows non-maintainers to push code to a public repository. You can create the branch in a fork and I'll be happy to collaborate there! |
|
I've had more time to think about the changes in this PR now, and they make a lot of sense. I've been thinking of alternate ways to get the same result, but none of them are necessarily better than this approach. Thank you for this contribution. We can discuss the sealed interfaces implementation in another issue. |
|
Nice, Good to know. I'm still thinking of new things I want to add as part of a full new version |
|
Excited to see them! I have a few more changes I want to do before releasing a new version, such as upgrading Kotlin and Coroutines versions. Expect a new release next weekend, but until then you can try a snapshot release from Jitpack. |
I saw you merged the commit, but I can't seem to find the relevant changes when using the Master branch. Am I missing something? |
|
Are you sure you're inspecting the correct branch? I can see the changes in master here |
In my build.gradle(:app) I have the following line - implementation "com.github.haroldadmin:NetworkResponseAdapter:4.1.0" But I can't use NetworkResponse.Error class |
|
There's no stable release with these changes yet. If you need to use these changes please use this pre-release version: implementation 'com.github.haroldadmin:NetworkResponseAdapter:d9912f3dfe'These changes will be released in v4.2.0 soon, along with a few dependency updates. Once that release is out, you can switch to the stable version again. |
Great! Thanks. Any time estimation for v4.2.0? |
|
Soon ✌️ |
|
v4.2.1 is out now with this feature. Thanks @gilgoldzweig! |
There are cases where I don't care what was the type of the error, I just want to know that there was an error without specifying the type each time