Skip to content
This repository was archived by the owner on Aug 17, 2019. It is now read-only.

Commit 0f97ec4

Browse files
committed
Simplify Failed
No message is required anymore. Client code should use chained exceptions to elaborate on the error.
1 parent 655bf1e commit 0f97ec4

File tree

3 files changed

+9
-20
lines changed

3 files changed

+9
-20
lines changed

Diff for: src/main/scala/com/ckkloverdos/maybe/Maybe.scala

+6-16
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ object MaybeEither {
102102
def apply[A](x: => A): MaybeEither[A] = Maybe(x) match {
103103
case j@Just(_) =>
104104
j
105-
case f@Failed(_,_) =>
105+
case f@Failed(_) =>
106106
f
107107
case NoVal =>
108108
Failed(new Exception("Got NoVal for a MaybeFailed"))
@@ -132,11 +132,9 @@ object Maybe {
132132
case _ => Just(value)
133133
}
134134
} catch {
135-
case e: Throwable => Failed(e, "Maybe.apply()")
135+
case e: Throwable => Failed(e)
136136
}
137137
}
138-
139-
def failed(exception: Throwable, fmt: String, args: Any*) = Failed(exception, fmt.format(args: _*))
140138
}
141139

142140
final case class Just[@specialized +A](get: A) extends MaybeOption[A] with MaybeEither[A] {
@@ -202,11 +200,9 @@ case object NoVal extends MaybeOption[Nothing] {
202200
/**
203201
* A Maybe wrapper for an exception.
204202
*/
205-
final case class Failed(exception: Throwable, explanation: String = "") extends MaybeEither[Nothing] {
206-
require(exception ne null, "Exception is null")
207-
require(explanation ne null, "Explanation is null")
203+
final case class Failed(cause: Throwable) extends MaybeEither[Nothing] {
204+
require(cause ne null, "cause is null")
208205

209-
def this(exception: Throwable, fmt: String, args: Any*) = this(exception, fmt.format(args))
210206
def isJust = false
211207
def isNoVal = false
212208
def isFailed = true
@@ -233,15 +229,9 @@ final case class Failed(exception: Throwable, explanation: String = "") extends
233229
def flatten1[U](implicit ev: <:<[Nothing, Maybe[U]]) = this
234230

235231
def detailedMessage: String = {
236-
if(explanation.trim.length > 0) {
237-
"%s:[%s] %s".format(explanation, exception.getClass.getName, exception.getMessage)
238-
} else {
239-
"[%s] %s".format(exception.getClass.getName, exception.getMessage)
240-
}
232+
"[%s] %s".format(cause.getClass.getName, cause.getMessage)
241233
}
242234

243235
override def equals(that: Any) =
244-
that.isInstanceOf[Failed] &&
245-
that.asInstanceOf[Failed].exception == this.exception &&
246-
that.asInstanceOf[Failed].explanation == this.explanation
236+
that.isInstanceOf[Failed] && that.asInstanceOf[Failed].cause == this.cause
247237
}

Diff for: src/main/scala/com/ckkloverdos/package.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ package object maybe {
3232
} catch {
3333
case e: Throwable =>
3434
ignoreExceptions (_catch)
35-
Failed(e, "effect{}")
35+
Failed(e)
3636
} finally {
3737
ignoreExceptions(_finally)
3838
}

Diff for: src/test/scala/com/ckkloverdos/maybe/MaybeTest.scala

+2-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ class MaybeTest {
6161
@Test
6262
def testMatchFailed = {
6363
val Except = new Exception("Hello")
64-
val Explan = "Hello there"
65-
Failed(Except, Explan) match {
66-
case Failed(Except, Explan) => ()
64+
Failed(Except) match {
65+
case Failed(Except) => ()
6766
case _ => Assert.assertFalse(true)
6867
}
6968
}

0 commit comments

Comments
 (0)