Hi everyone, I've been playing around with arrow 1...
# arrow
r
Hi everyone, I've been playing around with arrow 1.2.0 in a side project and I seem to have hit a wall. I have the following repository using Either and i want to try to achieve the same with the new Raise DSL. The repository is as follows
Copy code
interface AgendaRepository {
    suspend fun save(agenda: Agenda): Either<Throwable, Unit>
    suspend fun findBy(criteria: AgendaFindByCriteria): Either<Throwable, Agenda>

    context(Raise<Throwable>)
    suspend fun findByDsl(criteria: AgendaFindByCriteria): Agenda
}
I'm currently using the findBy method in a function with this syntax
Copy code
context(AgendaRepository)
suspend fun bookAgenda(id: UUID, name: Player, hourId: UUID): Either<BookAgendaError, Agenda> =
    findByOrElse(Id(id), onError = { AgendaNotFound })
        .flatMap { agenda -> agenda.bookAvailableHour(hourId, name) }
        .flatMap { agenda -> agenda.saveOrElse { error -> Unknown(error) } }
This is the method definition of the Raise DSL one
Copy code
context(AgendaRepository, Raise<BookAgendaError>)
suspend fun bookAgendaDsl(id: UUID, name: Player, hourId: UUID): Agenda
I've been trying to change the Raise<Throwable> into a Raise<BookAgendaError> with no successful outcome. I can get it to work with the bind() method if I use the repo returning Either but not with the Raise one. I've tried using fold/catch/recover but having been able to. It's most likely something I'm missing or doing it not the way it's pretended to be used. Does anyone know how to do that? Thanks in advance ❤️
y
If I understand your code correctly, I believe what you want is `withError`:
Copy code
context(AgendaRepository, Raise<BookAgendaError>)
suspend fun bookAgendaDsl(id: UUID, name: Player, hourId: UUID): Agenda = withError({ _ : Throwable -> AgendaNotFound}) {
  findByDsl(Id(id)).bookAvailableHour(hourId, name).saveOrElse { error -> raise(Unknown(error)) }
}
a
you can also use
catch
and handle the `Throwable`to turn it into
AgendaNotFound
(this is what
withError
does)
r
Got it to work using withError thanks