【发布时间】:2019-03-07 23:12:26
【问题描述】:
我有一个电子邮件列表,对于每一个,我都会在电子邮件表中查找它以查看该电子邮件是否存在。如果是这样,什么也不做,我会抛出错误。这是我的代码;
def lookupEmailStatus(email: EmailAddress, requestId: RequestId)(
implicit ec: ExecutionContext): HttpServiceResult[List[EmailStatusDTO]] = {
emailDatabase
.getEmailStatusByEmail(email, requestId)
.map(
l =>
if (l.isEmpty) {
logger.error(
LoggingMessage(
requestId,
s"Email status not found by ${email.email} failed"))
EntityNotFound(s"${email.email}", requestId)
} else {
l
}
)
.leftMap[HttpError] {
case e =>
logger.error(
LoggingMessage(
requestId,
s"Retrieve email status by ${email.email} failed"))
DatabaseError(e.message, requestId)
}
}
当我运行代码时出现错误:
Error:(57, 27) type mismatch;
found : cats.data.EitherT[model.HttpError,Product with Serializable]
required: model.HttpServiceResult[List[EmailStatusDTO]]
(which expands to) cats.data.EitherT[model.HttpError,List[EmailStatusDTO]]
.leftMap[HttpError] {
如果我删除了 .map(..) 方法,它会正常工作,但这不是我想要的:
def lookupEmailStatus(email: EmailAddress, requestId: RequestId)(
implicit ec: ExecutionContext): HttpServiceResult[List[EmailStatusDTO]] = {
emailDatabase
.getEmailStatusByEmail(email, requestId)
.leftMap[HttpError] {
case e =>
logger.error(
LoggingMessage(
requestId,
s"Retrieve email status by ${email.email} failed"))
DatabaseError(e.message, requestId)
}
}
这里是类型定义:
type HttpServiceResult[A] = ServiceResult[HttpError, A]
type ServiceResult[Err, A] = EitherT[Future, Err, A]
【问题讨论】:
标签: scala scala-cats