Skip to content

Instantly share code, notes, and snippets.

@erichonorez
Created February 26, 2019 12:34
Show Gist options
  • Save erichonorez/f14081fc8891b892e57b8fbe9c5a27e2 to your computer and use it in GitHub Desktop.
Save erichonorez/f14081fc8891b892e57b8fbe9c5a27e2 to your computer and use it in GitHub Desktop.
Application wide errors with Either
// Start writing your ScalaFiddle code here
import cats.implicits._
trait UseCase[Parameters, Error, Result] {
def execute(p: Parameters): Either[Error, Result]
}
import CreateLoanApplication._
trait CreateLoanApplication extends UseCase[CreateLoanApplicationRequest, CreateLoanApplicationError, CreateLoanApplicationResult] {
override def execute(r: CreateLoanApplicationRequest): Either[CreateLoanApplicationError, CreateLoanApplicationResult] = ???
}
object CreateLoanApplication {
case class CreateLoanApplicationRequest()
sealed trait CreateLoanApplicationError
object DbError extends CreateLoanApplicationError
object ExternalServiceError extends CreateLoanApplicationError
case class CreateLoanApplicationResult()
}
import CompletePersonalInformation._
trait CompletePersonalInformation extends UseCase[CompletePersonalInformationRequest, CompletePersonalInformationError, CompletePersonalInformationResult] {
override def execute(r: CompletePersonalInformationRequest): Either[CompletePersonalInformationError, CompletePersonalInformationResult] = ???
}
object CompletePersonalInformation {
case class CompletePersonalInformationRequest()
sealed trait CompletePersonalInformationError
object DbError extends CompletePersonalInformationError
object ExternalServiceError extends CompletePersonalInformationError
case class CompletePersonalInformationResult()
}
trait AppError
object AppError {
case class CreateLoanApplicationError(err: CreateLoanApplication.CreateLoanApplicationError) extends AppError
case class CompletePersonalInformationError(err: CompletePersonalInformation.CompletePersonalInformationError) extends AppError
}
val main: (CreateLoanApplication, CompletePersonalInformation) => Unit = (createLoanApplication, completePersonalInformation) => {
val result: Either[AppError, Unit] = for {
_ <- createLoanApplication.execute(CreateLoanApplicationRequest())
.leftMap(AppError.CreateLoanApplicationError(_))
_ <- completePersonalInformation.execute(CompletePersonalInformationRequest())
.leftMap(AppError.CompletePersonalInformationError(_))
} yield ()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment