Skip to content

Commit

Permalink
add methods for creating generic errors from string (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzai authored Jul 10, 2021
1 parent 10c46a9 commit 4c57617
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions shared/src/main/scala/mouse/string.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,18 @@ final class StringOps(private val s: String) extends AnyVal {

private def parse[A](f: String => A): NumberFormatException Either A = Either.catchOnly[NumberFormatException](f(s))

/**
* Wraps a `String` in `Throwable`.
*/
@inline def asThrowable: Throwable = new Throwable(s)

/**
* Wraps a `String` in `Error`.
*/
@inline def asError: Error = new Error(s)

/**
* Wraps a `String` in `Exception`.
*/
@inline def asException: Exception = new Exception(s)
}
8 changes: 8 additions & 0 deletions shared/src/test/scala/mouse/StringSyntaxTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ class StringSyntaxTests extends MouseSuite {
}
}

test("asThrowable, asError and asException") {
forAll { s: String =>
s.asThrowable.toString should ===(new Throwable(s).toString)
s.asError.toString should ===(new Error(s).toString)
s.asException.toString should ===(new Exception(s).toString)
}
}

}

final class EitherIdOps[A](val obj: A) extends AnyVal {
Expand Down

0 comments on commit 4c57617

Please sign in to comment.