summaryrefslogtreecommitdiff
path: root/cask/src/cask/router/Result.scala
blob: e38ee28e53780a20cdbaadd0b36ec0f978ef8127 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cask.router




/**
 * Represents what comes out of an attempt to invoke an [[EntryPoint]].
 * Could succeed with a value, but could fail in many different ways.
 */
sealed trait Result[+T]{
  def map[V](f: T => V): Result[V]
  def transform[V](f: PartialFunction[T, V]): Result[V]
}
object Result{

  /**
   * Invoking the [[EntryPoint]] was totally successful, and returned a
   * result
   */
  case class Success[T](value: T) extends Result[T]{
    def map[V](f: T => V) = Success(f(value))
    def transform[V](f: PartialFunction[T, V]) = f.lift(value) match {
      case None => Success(value).asInstanceOf[Result[V]]
      case Some(res) => Success(res)
    }
  }

  /**
   * Invoking the [[EntryPoint]] was not successful
   */
  sealed trait Error extends Result[Nothing]{
    def map[V](f: Nothing => V) = this
    def transform[V](f: PartialFunction[Nothing, V]) = this
  }


  object Error{


    /**
     * Invoking the [[EntryPoint]] failed with an exception while executing
     * code within it.
     */
    case class Exception(t: Throwable) extends Error

    /**
     * Invoking the [[EntryPoint]] failed because the arguments provided
     * did not line up with the arguments expected
     */
    case class MismatchedArguments(missing: Seq[ArgSig[_, _, _, _]],
                                   unknown: Seq[String]) extends Error
    /**
     * Invoking the [[EntryPoint]] failed because there were problems
     * deserializing/parsing individual arguments
     */
    case class InvalidArguments(values: Seq[ParamError]) extends Error
  }

  sealed trait ParamError
  object ParamError{
    /**
     * Something went wrong trying to de-serialize the input parameter;
     * the thrown exception is stored in [[ex]]
     */
    case class Invalid(arg: ArgSig[_, _, _, _], value: String, ex: Throwable) extends ParamError
    /**
     * Something went wrong trying to evaluate the default value
     * for this input parameter
     */
    case class DefaultFailed(arg: ArgSig[_, _, _, _], ex: Throwable) extends ParamError
  }
}