Hello ! Is there a good way to manage API errors o...
# getting-started
c
Hello ! Is there a good way to manage API errors on call in Kotlin? I'm writin a small API library and I'm wondering Should I do: -
fun getData(): Data?
-> catch all exceptions and return
null
-
fun getData(): Data
->
throw
exceptions depending on the situation and have the user of the library catch them -
fun getData(): Outcome<S: Success, F: Failure>
-> create a custom class to wrap the result I'm very much a beginner, and I have no clue what would be the best pratice. Btw I'm using
retrofit2
with kotlin's coroutines, so the REST requests might return exceptions.
m
I guess it depends on what you want that happens when an exception occurs. When you don’t want that another piece of your program knows about the exception you can return null. If you want your exception to be handled you can choose between the second and third option. In the third option you are going to force the user of the lib to handle the exception. In the second option the user can decide not handling the exception and let the program crash.
c
I'm guessing I want the user to know about the expections, as they would need to know if it's a network failure, non-existing data, etc.
m
indeed so option 1 won’t fit in my opinion.
c
Nice, thanks!