Gasan
07/14/2023, 9:16 AMcatch (cause is myException)
Riccardo Lippolis
07/14/2023, 9:18 AMMyException
, rethrow itAdam S
07/14/2023, 9:20 AMnull
try {
// ...
} catch (ex: Exception) {
val allCauses = generateSequence<Throwable>(ex) { ex.cause }
}
You might want to consider checking suppressedExceptions too?Gasan
07/14/2023, 9:25 AMAdam S
07/14/2023, 9:25 AMsealed interface MyOperationResult {
class Success(...) : MyOperationResult
class Failed(...) : MyOperationResult
}
Gasan
07/14/2023, 9:26 AMAdam S
07/14/2023, 9:27 AMMyOperation.Failed(...)
Adam S
07/14/2023, 9:27 AMAdam S
07/14/2023, 9:31 AMso essentially I have to write a utility function and use it smth like this:
https://pl.kotl.in/4de-VgpHEyes basically, but I’d refactor it a little bit:
fun main() {
try {
// ...
} catch (ex: Exception) {
val allCauses = generateSequence<Throwable>(ex) { ex.cause }
val myException = allCauses.filterIsInstance<MyCustomException>().firstOrNull()
}
}
class MyCustomException : Exception()
Gasan
07/14/2023, 10:06 AMSuccess
Failure1
Failure2
...
Gasan
07/14/2023, 10:07 AMAdam S
07/14/2023, 10:08 AMGasan
07/14/2023, 10:09 AMAdam S
07/14/2023, 10:10 AMsealed interface MyOperationResult {
class Success(...) : MyOperationResult
class Failed(
val attempts: Int, // if attempts < 100, then maybe retry
val invalidProperties: Int, // if invalidProperties > 0, then there's no way to recover, so abort immediately
) : MyOperationResult
}
Adam S
07/14/2023, 10:11 AMGasan
07/14/2023, 10:14 AMGasan
07/14/2023, 10:16 AMFailed
value. So I will end up comparing exception types. Because the origin of those failures are exceptions. Or I need to create a parallel indicators (?)enum and set them when respective exception is thrown and then compare to those indicators. Anyway, thank youAdam S
07/14/2023, 10:40 AMCharles Flynn
07/14/2023, 11:40 AMGasan
07/17/2023, 11:22 AMResult
type. A bit of a problem here is that standard result type requires an exception value, so an exception value must be used to indicate the kind of the failure happened
• on consumer side:
◦ if producer returns a Result
type value, then process error values if any, use is
, as
if you need to further discriminate exception
value by type. Unfortunately there is no generic way at looking at the cause of exception, so that need to be implemented manually if required.
◦ if producer throws an exception, figure out what are recoverable and unrecoverable exceptions, recover from recoverable ones, re-throw all others.Gasan
07/17/2023, 11:28 AMResult
type. That makes me think that returning Result
type type value approach or not standardised or not intended/envisioned by the language creators. But that type in the standard library, so quite confusing.Charles Flynn
07/17/2023, 12:34 PMGasan
11/06/2023, 6:43 PM