Skip to content

Commit 472eb3e

Browse files
authored
Merge pull request #2142 from ulfryk/fix/uninformative-null-error-simple
Make null param error more informative (#809)
2 parents 941f66d + aab920f commit 472eb3e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

modules/core/src/main/scala/doobie/util/put.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ sealed abstract class Put[A](
5656
) {}
5757

5858
def unsafeSetNonNullable(ps: PreparedStatement, n: Int, a: A): Unit =
59-
if (a == null) sys.error("oops, null")
59+
if (a == null) sys.error(s"Expected non-nullable param at $n. Use Option to describe nullable values.")
6060
else put.fi.apply(ps, n, (put.k(a)))
6161

6262
def unsafeSetNullable(ps: PreparedStatement, n: Int, oa: Option[A]): Unit =
@@ -66,7 +66,7 @@ sealed abstract class Put[A](
6666
}
6767

6868
def unsafeUpdateNonNullable(rs: ResultSet, n: Int, a: A): Unit =
69-
if (a == null) sys.error("oops, null")
69+
if (a == null) sys.error(s"Expected non-nullable param at $n. Use Option to describe nullable values.")
7070
else update.fi.apply(rs, n, (update.k(a)))
7171

7272
def unsafeUpdateNullable(rs: ResultSet, n: Int, oa: Option[A]): Unit =

modules/core/src/test/scala/doobie/util/WriteSuite.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,27 @@ class WriteSuite extends munit.FunSuite with WriteSuitePlatform {
122122
.unsafeRunSync()
123123
}
124124

125+
test("Write should yield correct error when Some(null) inserted") {
126+
interceptMessage[RuntimeException]("Expected non-nullable param at 2. Use Option to describe nullable values.") {
127+
testNullPut(("a", Some(null)))
128+
}
129+
}
130+
131+
test("Write should yield correct error when null inserted into non-nullable field") {
132+
interceptMessage[RuntimeException]("Expected non-nullable param at 1. Use Option to describe nullable values.") {
133+
testNullPut((null, Some("b")))
134+
}
135+
}
136+
137+
private def testNullPut(input: (String, Option[String])): Int = {
138+
import doobie.implicits.*
139+
140+
(for {
141+
_ <- sql"create temp table t0 (a text, b text null)".update.run
142+
n <- Update[(String, Option[String])]("insert into t0 (a, b) values (?, ?)").run(input)
143+
} yield n)
144+
.transact(xa)
145+
.unsafeRunSync()
146+
}
147+
125148
}

0 commit comments

Comments
 (0)