jean
12/03/2025, 9:41 AMmap operator? I have this code :
Either.catch {
val tickets = repository.inspect(qrContent = capturedCode.rawValue) // can throw
if (tickets == null) {
travelDocumentHelper.showTicketInspectionFailure(null)
ReadTravelDocumentError.NoTicketFound // <- How can I "raise" this error as a `Either.Left`?
} else {
successfulTicketInspectionHandler.handleSuccessfulTicketInspection(tickets) // <- regular `Either.Right`
}
}.mapLeft {
travelDocumentHelper.showTicketInspectionFailure(it)
Log.error(TAG, "Cannot read QR data: ${capturedCode.rawValue}, exception: $it")
ReadTravelDocumentError.FailedToInspect
}
But the problem here is that my “right” type is Any instead of Unit . It works with a throw inside the catch but it would have been nicer with a raise(...)Alejandro Serrano.Mena
12/03/2025, 10:09 AMEither.catch only "handles" the exceptions. If you want to also raise within a block, the best is using either block, and use catch inside
either {
try {
val tickets = repository.inspect(qrContent = capturedCode.rawValue)
ensureNotNull(tickets) { ReadTravelDocumentError.NoTicketFound }
successfulTicketInspectionHandler.handleSuccessfulTicketInspection(tickets)
} catch {
raise(ReadTravelDocumentError.FailedToInspect)
}
}.onLeft {
travelDocumentHelper.showTicketInspectionFailure(it)
Log.error(TAG, "Cannot read QR data: ${capturedCode.rawValue}, exception: $it")
}jean
12/03/2025, 10:10 AM