Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make null param error more informative (#809) #2142

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions modules/core/src/main/scala/doobie/util/put.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sealed abstract class Put[A](
) {}

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

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

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

def unsafeUpdateNullable(rs: ResultSet, n: Int, oa: Option[A]): Unit =
Expand Down
23 changes: 23 additions & 0 deletions modules/core/src/test/scala/doobie/util/WriteSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,27 @@ class WriteSuite extends munit.FunSuite with WriteSuitePlatform {
.unsafeRunSync()
}

test("Write should yield correct error when Some(null) inserted") {
interceptMessage[RuntimeException]("Expected non-nullable param at 2. Use Option to describe nullable values.") {
testNullPut(("a", Some(null)))
}
}

test("Write should yield correct error when null inserted into non-nullable field") {
interceptMessage[RuntimeException]("Expected non-nullable param at 1. Use Option to describe nullable values.") {
testNullPut((null, Some("b")))
}
}

private def testNullPut(input: (String, Option[String])): Int = {
import doobie.implicits.*

(for {
_ <- sql"create temp table t0 (a text, b text null)".update.run
n <- Update[(String, Option[String])]("insert into t0 (a, b) values (?, ?)").run(input)
} yield n)
.transact(xa)
.unsafeRunSync()
}

}