简体   繁体   English

如何验证Future [List [T]]中的单个元素以返回Future [List [T]]或引发异常

[英]How to validate single element in Future[List[T]] to return Future[List[T]] or throw exception

No need to pay attention to the purpose of the function here, it's only for demonstration: 此处无需注意功能的用途,仅用于演示:

def readAllByPersonOrFail(person: Person, otherPersonId: Long): Future[List[Person]] = {
  val personSiblingsFuture: Future[List[Person]] = personSiblingsDomain.readAllByPersonId(person.id)
  personSiblingsFuture.map { persons =>
    persons.find(_.id == otherPersonId) match {
      case Some(person) =>
        person.isActive match {
          case true => person
          case false => throw new IllegalArgumentException("something inactive")
        }
      case None => throw new IllegalArgumentException("something wrong ehre")
    }
  }
  personSiblingsFuture
}

I would like to return personSiblingsFuture above iff it validates (makes sure correct person is in the list and is active), otherwise throw the exception. 如果验证,我想返回上面的personSiblingsFuture(确保正确的人在列表中并且处于活动状态),否则抛出异常。 I don't think the above code is doing the right thing as it is not existing upon failure. 我不认为上面的代码做对了,因为失败后就不存在了。

Take a look at scala.concurrent.Future.map . 看一下scala.concurrent.Future.map This creates a new future, whose value is resolved by applying a function to the successful result of this future. 这将创建一个新的未来,其价值可以通过将函数应用于该未来的成功结果来解决。

Note that here you're throwing away the resulting future you just created with .map() too. 请注意,这里您还将丢弃刚刚使用.map()创建的结果。

There are a few areas to solve your problem, though you should question more deeply the use of exceptions with Futures . 尽管您应该更深地质疑Futures中异常的使用,但仍有一些地方可以解决您的问题。 Scala provides concepts like Future , Option , and Try specifically to avoid throwing exceptions and have a clearer control flow. Scala提供了诸如FutureOptionTry类的概念,专门用于避免引发异常并具有更清晰的控制流程。

Option 1, return the mapped future 选项1,返回映射的将来

In your funciton, 在您的功能中

def func(...): Future[List[Person]] {
  val personSiblingsFuture = ...;
  personSiblingsFuture.map { persons =>
    ...
  }
}
// note we're not returning personSiblingsFuture,
// but the mapped result

When someone actually tries to get the value of the future, eg by using .value , they might see an exception intead: 当某人实际上尝试获取未来的价值时,例如通过使用.value ,他们可能会看到一个异常提示:

def main() {
  val future = func(...);  // this is fine
  val my_list = future.value;  // awaits Future, might throw here
}

Option 2, actually await the list and throw in the function 选项2,实际上等待列表并放入函数

Returning a future that might throw is strange, it might be a bit easier if the you actually explicitly a had a function that might throw, eg 返回一个可能抛出的未来很奇怪,如果您实际上明确地拥有一个可能抛出的函数,则可能会容易一些。

/** jsdoc describing function **/
def funcMightThrow(...): List[Person] {
  val personSiblingsFuture = ...;
  val personSiblings = personSiblingsFuture.value;

  personSiblings.find(_.id == otherPersonId) match {
    case Some(person) =>
      person.isActive match {
        case true => personSiblings
        case false => throw new IllegalArgumentException("something inactive")
      }
    case None => throw new IllegalArgumentException("something wrong ehre")
  }
}

Option 3, consider making return types more explicit 选项3,考虑使返回类型更明确

def func(...): Future[Try[List[Person]]] {
  val personSiblingsFuture = ...;
  personSiblingsFuture.map { persons =>
    ...
    // successful case returns 'persons' (List[Person])
    // fail cases return Failure(...) instead
  }
}  // return the mapped future

You can also return Try[List[Person]] rather than a Future[] of that, by using .value , which makes func a blocking function. 您也可以使用.value返回Try[List[Person]]而不是其中的Future[] ,这会使func成为阻塞函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM