Skip to content

Commit 4c57617

Browse files
authored
add methods for creating generic errors from string (#218)
1 parent 10c46a9 commit 4c57617

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

shared/src/main/scala/mouse/string.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,18 @@ final class StringOps(private val s: String) extends AnyVal {
6565

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

68+
/**
69+
* Wraps a `String` in `Throwable`.
70+
*/
71+
@inline def asThrowable: Throwable = new Throwable(s)
72+
73+
/**
74+
* Wraps a `String` in `Error`.
75+
*/
76+
@inline def asError: Error = new Error(s)
77+
78+
/**
79+
* Wraps a `String` in `Exception`.
80+
*/
81+
@inline def asException: Exception = new Exception(s)
6882
}

shared/src/test/scala/mouse/StringSyntaxTests.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ class StringSyntaxTests extends MouseSuite {
173173
}
174174
}
175175

176+
test("asThrowable, asError and asException") {
177+
forAll { s: String =>
178+
s.asThrowable.toString should ===(new Throwable(s).toString)
179+
s.asError.toString should ===(new Error(s).toString)
180+
s.asException.toString should ===(new Exception(s).toString)
181+
}
182+
}
183+
176184
}
177185

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

0 commit comments

Comments
 (0)