2017-07-19 64 views
0

我的(不完全)的代碼如下所示:匹配與andThen

def doSomething(name: String): { implicit request => 
    check(name).map { 
     case Something() => Ok() 
     case SomethingElse() => NoContent 
    } 
    .andThen { 
     case r: Result => logger.info(s"Request finished with status ${r.header.status}") 
    } 
    } 

我得到這樣一個錯誤:

[error] myfile.scala:39: fruitless type test: a value of type scala.util.Try[play.api.mvc.Result] cannot also be a play.api.mvc.Result 
[error]  case r: Result => logger.info(s"Request finished with status ${r.header.status}") 

我怎樣才能匹配Try

回答

3

如果你想匹配一個Try,你需要匹配它的構造,不是它的構造舉行:

.andThen { 
    case Success(r) => 
     logger.info(s"Request finished with status ${r.header.status}") 

    case Failure(e) => // Handle failure 
}