Andreas Sinz
04/30/2018, 6:26 PMcatch exception
-> put it in a Result
-> rethrow another/the same exception
, so definitely Option 2travis
04/30/2018, 6:29 PMApi
will be using captures exceptions itself to return specific result codes? They are already doing catch exception
→ return as result code
?
Does that change how I should approach it?Api
to never have to deal with exceptions (as the only time exceptions are thrown are when they use the extension functions)?Andreas Sinz
04/30/2018, 6:36 PMApi
already returns an error code, you could just create two methods: getData
returns the data or throws an exception and getDataAsResult
that calls the same Api
but returns a Result
//Returns an Int as error-Code or
// String as Data
fun Api.readData(): Any
@throws MyException
fun MyApi.readData(): String {
val data = Api.readData()
if(data is Int)
throw MyException(data)
return data as String
}
fun MyApi.readDataAsResult(): Result<String> {
val data = Api.readData()
return if(data is Int)
Result.Error(data)
else
Result.Success(data as String)
}
travis
04/30/2018, 6:45 PMAny
return from the core function be more error prone?Andreas Sinz
04/30/2018, 6:45 PMdata
first, just create another function:
fun MyApi.parseData(): Data {
val data = Api.readData()
//Do Work with Data
return newData
}
and use this function inside MyApi.readData
and MyApi.readDataAsResult
instead of the original functionApi
you are using that returns an error-code or the actual datatravis
04/30/2018, 6:46 PMdata
& responseCode
actually comes back as separate parameters in a callback, so I have to "luxury" of deciding how to package it.Andreas Sinz
04/30/2018, 6:47 PMMyApi.readData
calls MyApi.readDataAsResult
or vice-versatravis
04/30/2018, 6:47 PMAndreas Sinz
04/30/2018, 6:50 PMApi
-> core
-> Extension methods
?travis
04/30/2018, 6:51 PMAndreas Sinz
04/30/2018, 6:52 PMApi
you'd throw exception based on error-code
-> catch excetpion
-> return result
?travis
04/30/2018, 6:52 PMdata
& result code in Result
and only throw via extension function:
Result based on error-code
-> return result
-> (optional use of extension) throw exception
Andreas Sinz
04/30/2018, 6:55 PMcore
and extension method
?readData
and readDataAsResult
inside core?travis
04/30/2018, 6:56 PMApi
class (if that's what you mean?).Result
? (as it's "default", i.e. pre-extension function impl)Andreas Sinz
04/30/2018, 7:03 PMApi
returns more often an error-code than it throws, I'd use Option 1, otherwise Option 2 just to have as little overhead as possiblecore
and extensions
and the Api
doesn't have consistent return types (sometimes throws, sometimes returns error codes)travis
04/30/2018, 7:04 PM