Hi! Is there an easy way to raise an error from a ...
# arrow
j
Hi! Is there an easy way to raise an error from a
map
operator? I have this code :
Copy 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(...)
a
Either.catch
only "handles" the exceptions. If you want to also raise within a block, the best is using
either
block, and use catch inside
Copy code
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")
}
👍🏻 1
➕ 2
j
Thanks!