【问题标题】:MapTo Either silent fail when it's not matched不匹配时 MapTo 静默失败
【发布时间】:2016-04-13 00:24:09
【问题描述】:

我有以下代码静默失败而没有发现任何错误:

(actor ? GetRowCount()).mapTo[Either[Rejection, Long]] map {
      case Left(x) => ctx.reject(x)
      case Right(totalRows) => ctx.complete(totalRows)
}

例如,当GetRowCount() 不返回Long,而是返回String 时,没有发现任何错误,它只是静默失败。

我正在考虑使用以下内容:

(actor ? GetRowCount()).mapTo[Either[Rejection, Any]] map {
      case Left(x) => ctx.reject(x)
      case Right(totalRows: Long) => ctx.complete(totalRows)
      case _ => ctx.reject(Rejection("Type mismatch"))
}

但是有更好的答案吗?

【问题讨论】:

    标签: scala akka future spray


    【解决方案1】:

    我会使用recoverrecoverWith

    (actor ? GetRowCount).mapTo[Either[Rejection, Long]] map {
      case Left(x) => ctx.reject(x)
      case Right(totalRows) => ctx.complete(totalRows)
    } recover {
      case e: Throwable =>
        logger.error(e, "Some thing wrong while performing ask")
        //throw an error or return something here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-10
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      相关资源
      最近更新 更多