0

我发现 Success 案例经常隐藏在许多错误和一个成功的匹配中。是否有另一种方法可以更清晰地编写此代码,以使成功可能通过在部分函数中包含所有错误而脱颖而出?或者,也许还有另一种写法,但更简洁。我一般只是在寻找可以完成的其他想法/解决方案。

results.responseCode match {
  case Success =>
    // TODO make this less smelly. can results.results be None?
    val searchResults = results.results.get.results
    SomeService.getUsersFromThriftResults(
      userRepo,
      searchResults,
      Seq(WithCounts)) map { userResults =>
      val renderableStatuses = getStatuses(searchResults, userResults.userMap)
      new JsonAction(transformedQuery, renderableStatuses)
    }
  case ErrorInvalidQuery =>
    throw new SomeBadRequestException("invalid query")
  case ErrorOverCapacity |
       ErrorTimeout =>
    throw new SomeServiceUnavailableException("service unavailable")
  //TODO: take care of these errors differently
  //          case ErrorInvalidWorkflow |
  //               ErrorBackendFailure |
  //               ErrorEventNotFound |
  //               PartialSuccess |
  case _ =>
    throw new SomeApplicationException("internal server error")
}
4

4 回答 4

3

您可以将部分函数与orElse.

就像是

type ResponseCode = Int // This would be the type of the results.responseCode.
type RespHandler = PartialFunction[ResponseCode, JsonAction]

val invalidQuery: RespHandler =
  { case ErrorInvalidQuery => ... }

val overCapacity: RespHandler =
  { case ErrorOverCapacity => ... }

results.responseCode match {
  case Success => ...
} orElse invalidQuery orElse overCapacity orElse ...

你可以在这篇博文中看到更多关于这个的信息:用 orElse 链接部分函数

编辑:这不像书面的那样工作,你需要编写处理然后apply它(例如(success orElse invalidQuery ..)(results.getResponseCode))。

更好的解决方案是将其更改为返回 a并在匹配块Try[ResponseCode]中处理异常。Failure

于 2014-08-20T17:31:01.047 回答
2

你可以试试Try[A]

文档中的示例:

import scala.util.{Try, Success, Failure}

def divide: Try[Int] = {
  val dividend = Try(Console.readLine("Enter an Int that you'd like to divide:\n").toInt)
  val divisor = Try(Console.readLine("Enter an Int that you'd like to divide by:\n").toInt)
  val problem = dividend.flatMap(x => divisor.map(y => x/y))
  problem match {
    case Success(v) =>
      println("Result of " + dividend.get + "/"+ divisor.get +" is: " + v)
      Success(v)
    case Failure(e) =>
      println("You must've divided by zero or entered something that's not an Int. Try again!")
      println("Info from the exception: " + e.getMessage)
      divide
  }
}
于 2014-08-20T17:17:27.797 回答
1

您应该考虑使用 Either[Throwable, A] 来表示结果类型。

http://tersesystems.com/2012/12/27/error-handling-in-scala/

于 2014-08-20T17:02:56.550 回答
1

您可以将结果转换为Either[ResponseCode, Content]

def codeToEither(result: Result): Either[ResponseCode, Content] = 
  result.responseCode match {
    case Success => Right(result.results.get.results)
    case _       => Left(result.responseCode)
  }

然后fold在它上面

codeToEither(result).fold(
  errorCode => ... ,
  content   => ...
)

或者Either[Exception, Content],如果您愿意,可以以相同的方式将结果转换为。

于 2014-08-20T17:13:09.067 回答