Skip to content

Commit b6bb081

Browse files
Dave McLainbretthoerner
authored andcommitted
Create a noop sentry client when the user provides bad configuration (#556)
1 parent d5569c7 commit b6bb081

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

sentry/src/main/java/io/sentry/DefaultSentryClientFactory.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,24 @@ public class DefaultSentryClientFactory extends SentryClientFactory {
232232

233233
@Override
234234
public SentryClient createSentryClient(Dsn dsn) {
235-
SentryClient sentryClient = new SentryClient(createConnection(dsn), getContextManager(dsn));
236235
try {
237-
// `ServletRequestListener` was added in the Servlet 2.4 API, and
238-
// is used as part of the `HttpEventBuilderHelper`, see:
239-
// https://tomcat.apache.org/tomcat-5.5-doc/servletapi/
240-
Class.forName("javax.servlet.ServletRequestListener", false, this.getClass().getClassLoader());
241-
sentryClient.addBuilderHelper(new HttpEventBuilderHelper());
242-
} catch (ClassNotFoundException e) {
243-
logger.debug("The current environment doesn't provide access to servlets,"
244-
+ " or provides an unsupported version.");
236+
SentryClient sentryClient = new SentryClient(createConnection(dsn), getContextManager(dsn));
237+
try {
238+
// `ServletRequestListener` was added in the Servlet 2.4 API, and
239+
// is used as part of the `HttpEventBuilderHelper`, see:
240+
// https://tomcat.apache.org/tomcat-5.5-doc/servletapi/
241+
Class.forName("javax.servlet.ServletRequestListener", false, this.getClass().getClassLoader());
242+
sentryClient.addBuilderHelper(new HttpEventBuilderHelper());
243+
} catch (ClassNotFoundException e) {
244+
logger.debug("The current environment doesn't provide access to servlets,"
245+
+ " or provides an unsupported version.");
246+
}
247+
sentryClient.addBuilderHelper(new ContextBuilderHelper(sentryClient));
248+
return configureSentryClient(sentryClient, dsn);
249+
} catch (Exception e) {
250+
logger.error("Failed to initialize sentry, falling back to no-op client", e);
251+
return new SentryClient(new NoopConnection(), new ThreadLocalContextManager());
245252
}
246-
sentryClient.addBuilderHelper(new ContextBuilderHelper(sentryClient));
247-
return configureSentryClient(sentryClient, dsn);
248253
}
249254

250255
/**

sentry/src/test/java/io/sentry/DefaultSentryClientFactoryTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import java.util.*;
66

77
import static org.hamcrest.MatcherAssert.assertThat;
8-
import static org.hamcrest.Matchers.contains;
9-
import static org.hamcrest.Matchers.is;
8+
import static org.hamcrest.Matchers.*;
9+
10+
import io.sentry.connection.NoopConnection;
1011

1112
public class DefaultSentryClientFactoryTest extends BaseTest {
1213
@Test
@@ -43,7 +44,16 @@ public void testFieldsFromDsn() throws Exception {
4344
extrasMap.put("red", "blue");
4445
extrasMap.put("green", "yellow");
4546
assertThat(sentryClient.getExtra(), is(extrasMap));
47+
}
4648

49+
@Test
50+
public void testBadDataInitializesWithoutException() {
51+
String badTags = "foo:";
52+
53+
String dsn = String.format("https://user:pass@example.com/1?tags=%s", badTags);
54+
SentryClient sentryClient = DefaultSentryClientFactory.sentryClient(dsn);
4755

56+
assertThat(sentryClient, isA(SentryClient.class));
57+
assertThat(sentryClient.getContext(), notNullValue());
4858
}
4959
}

0 commit comments

Comments
 (0)