Rafa Gómez
07/13/2023, 4:38 PMinterface 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
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
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 ❤️Youssef Shoaib [MOD]
07/13/2023, 5:54 PMcontext(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)) }
}
Alejandro Serrano Mena
07/13/2023, 6:20 PMcatch
and handle the `Throwable`to turn it into AgendaNotFound
(this is what withError
does)Rafa Gómez
07/13/2023, 6:52 PM