/**
* Base class for all exceptions in this library
*/
trait MyAkkaHttpException {}
/**
* Thrown when there is a problem persisting data to a datastore
*/
case class PersistenceException(message: String)
extends Exception(message: String) with MyAkkaHttpException
/**
* Thrown when validation on an object fails
* @param errors
*/
case class ValidationException(message: String, errors: List[String])
extends Exception(message: String) with MyAkkaHttpException
以下代碼:
class ContactFormService(contactFormPersistor: ContactFormPersistor) {
def handleForm(contactForm: ContactForm): ValidationNel[MyAkkaHttpException, String] = {
contactForm.validate() match {
case Success(_) => contactFormPersistor.persist(contactForm)
case Failure(e) =>
new ValidationException(message = "Error validating contact form",
errors = e.toList).failureNel[String]
}
}
}
contactFormPersistor.persist
返回ValidationNel[PersistenceException, String]
contactForm.validate()
回報ValidationNel[String, Boolean]
問題是handleForm
不會接受PersistenceException
和ValidationException
是MyAkkaHttpException
的子類。我需要做什麼才能正確認識到那些返回類型是有效的子類?
使用'+',我相信。 – Elazar
@Elazar我嘗試過,但它顯示爲語法錯誤。請你能寫一個答案嗎? – jbrown
嘗試'ValidationNel [+ MyAkkaHttpException,String]' – Elazar