Skip to content

Commit 91ec618

Browse files
adinauerclaude
andcommitted
fix: Throw clear error when DSN is empty
Previously an empty or whitespace-only DSN string would fall through to the URI constructor, producing a confusing error message. Now the Dsn constructor checks for empty strings after trimming and throws an IllegalArgumentException with a clear message. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e20e757 commit 91ec618

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

sentry/src/main/java/io/sentry/Dsn.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ URI getSentryUri() {
5151
Dsn(@Nullable String dsn) throws IllegalArgumentException {
5252
try {
5353
final String dsnString = Objects.requireNonNull(dsn, "The DSN is required.").trim();
54+
if (dsnString.isEmpty()) {
55+
throw new IllegalArgumentException("The DSN is empty.");
56+
}
5457
final URI uri = new URI(dsnString).normalize();
5558
final String scheme = uri.getScheme();
5659
if (!("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme))) {

sentry/src/test/java/io/sentry/DsnTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ class DsnTest {
9595
assertEquals("https://host/api/id", dsn.sentryUri.toURL().toString())
9696
}
9797

98+
@Test
99+
fun `when dsn is empty, throws exception`() {
100+
val ex = assertFailsWith<IllegalArgumentException> { Dsn("") }
101+
assertEquals(
102+
"java.lang.IllegalArgumentException: The DSN is empty.",
103+
ex.message,
104+
)
105+
}
106+
107+
@Test
108+
fun `when dsn is only whitespace, throws exception`() {
109+
val ex = assertFailsWith<IllegalArgumentException> { Dsn(" ") }
110+
assertEquals(
111+
"java.lang.IllegalArgumentException: The DSN is empty.",
112+
ex.message,
113+
)
114+
}
115+
98116
@Test
99117
fun `non http protocols are not accepted`() {
100118
assertFailsWith<IllegalArgumentException> { Dsn("ftp://publicKey:secretKey@host/path/id") }

0 commit comments

Comments
 (0)