2016-04-13 1 views
0

J'ai le code suivant qui échouent en silence sans prendre aucune erreur:MapTo Soit silencieux échouent quand il ne correspond pas

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

Lorsque GetRowCount() ne retourne pas Long, mais String par exemple, aucune erreur n'a été pris et ça échoue silencieusement.

Je pense à utiliser ce qui suit:

(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")) 
} 

Mais est-il une meilleure solution?

Répondre

0

J'utiliser recover ou recoverWith

(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 
}