Skip to content

Commit d28f388

Browse files
runningcodeclaude
andauthored
ref: Remove Throwable catches that guard nothing (#6565)
Nothing in these try blocks can throw, other than an SDK bug or a fatal error that should not be swallowed: FilterString.matches with its precompiled pattern, the StatFs getters, the log and metrics attribute setters, the SHA-1 hash, a substring that is already bounds checked, Baggage's outer header loop, and a submit whose RejectedExecutionException is already caught. Refs JAVA-552 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fb3e477 commit d28f388

9 files changed

Lines changed: 89 additions & 169 deletions

File tree

‎sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java‎

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -232,59 +232,39 @@ private void mergeOS(final @NotNull SentryBaseEvent event) {
232232
}
233233

234234
private void setDevice(final @NotNull SentryLogEvent event) {
235-
try {
236-
event.setAttribute(
237-
"device.brand",
238-
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.BRAND));
239-
event.setAttribute(
240-
"device.model",
241-
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.MODEL));
242-
event.setAttribute(
243-
"device.family",
244-
new SentryLogEventAttributeValue(SentryAttributeType.STRING, deviceFamily.getValue()));
245-
} catch (Throwable e) {
246-
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve device info", e);
247-
}
235+
event.setAttribute(
236+
"device.brand", new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.BRAND));
237+
event.setAttribute(
238+
"device.model", new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.MODEL));
239+
event.setAttribute(
240+
"device.family",
241+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, deviceFamily.getValue()));
248242
}
249243

250244
private void setOs(final @NotNull SentryLogEvent event) {
251-
try {
252-
event.setAttribute(
253-
"os.name", new SentryLogEventAttributeValue(SentryAttributeType.STRING, "Android"));
254-
event.setAttribute(
255-
"os.version",
256-
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.VERSION.RELEASE));
257-
} catch (Throwable e) {
258-
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve os system", e);
259-
}
245+
event.setAttribute(
246+
"os.name", new SentryLogEventAttributeValue(SentryAttributeType.STRING, "Android"));
247+
event.setAttribute(
248+
"os.version",
249+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.VERSION.RELEASE));
260250
}
261251

262252
private void setDevice(final @NotNull SentryMetricsEvent event) {
263-
try {
264-
event.setAttribute(
265-
"device.brand",
266-
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.BRAND));
267-
event.setAttribute(
268-
"device.model",
269-
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.MODEL));
270-
event.setAttribute(
271-
"device.family",
272-
new SentryLogEventAttributeValue(SentryAttributeType.STRING, deviceFamily.getValue()));
273-
} catch (Throwable e) {
274-
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve device info", e);
275-
}
253+
event.setAttribute(
254+
"device.brand", new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.BRAND));
255+
event.setAttribute(
256+
"device.model", new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.MODEL));
257+
event.setAttribute(
258+
"device.family",
259+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, deviceFamily.getValue()));
276260
}
277261

278262
private void setOs(final @NotNull SentryMetricsEvent event) {
279-
try {
280-
event.setAttribute(
281-
"os.name", new SentryLogEventAttributeValue(SentryAttributeType.STRING, "Android"));
282-
event.setAttribute(
283-
"os.version",
284-
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.VERSION.RELEASE));
285-
} catch (Throwable e) {
286-
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve os system", e);
287-
}
263+
event.setAttribute(
264+
"os.name", new SentryLogEventAttributeValue(SentryAttributeType.STRING, "Android"));
265+
event.setAttribute(
266+
"os.version",
267+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.VERSION.RELEASE));
288268
}
289269

290270
// Data to be applied to events that was created in the running process

‎sentry-android-core/src/main/java/io/sentry/android/core/DeviceInfoUtil.java‎

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -369,35 +369,21 @@ private Device.DeviceOrientation getOrientation() {
369369
*
370370
* @return the total amount of internal storage, in bytes
371371
*/
372-
@Nullable
373-
private Long getTotalInternalStorage(final @NotNull StatFs stat) {
374-
try {
375-
long blockSize = stat.getBlockSizeLong();
376-
long totalBlocks = stat.getBlockCountLong();
377-
return totalBlocks * blockSize;
378-
} catch (Throwable e) {
379-
options.getLogger().log(SentryLevel.ERROR, "Error getting total internal storage amount.", e);
380-
return null;
381-
}
372+
private long getTotalInternalStorage(final @NotNull StatFs stat) {
373+
long blockSize = stat.getBlockSizeLong();
374+
long totalBlocks = stat.getBlockCountLong();
375+
return totalBlocks * blockSize;
382376
}
383377

384378
/**
385379
* Get the unused amount of internal storage, in bytes.
386380
*
387381
* @return the unused amount of internal storage, in bytes
388382
*/
389-
@Nullable
390-
private Long getUnusedInternalStorage(final @NotNull StatFs stat) {
391-
try {
392-
long blockSize = stat.getBlockSizeLong();
393-
long availableBlocks = stat.getAvailableBlocksLong();
394-
return availableBlocks * blockSize;
395-
} catch (Throwable e) {
396-
options
397-
.getLogger()
398-
.log(SentryLevel.ERROR, "Error getting unused internal storage amount.", e);
399-
return null;
400-
}
383+
private long getUnusedInternalStorage(final @NotNull StatFs stat) {
384+
long blockSize = stat.getBlockSizeLong();
385+
long availableBlocks = stat.getAvailableBlocksLong();
386+
return availableBlocks * blockSize;
401387
}
402388

403389
@Nullable
@@ -449,16 +435,10 @@ private File getExternalStorageDep(final @Nullable File internalStorage) {
449435
* @return the total amount of external storage, in bytes, or null if no external storage is
450436
* mounted
451437
*/
452-
@Nullable
453-
private Long getTotalExternalStorage(final @NotNull StatFs stat) {
454-
try {
455-
final long blockSize = stat.getBlockSizeLong();
456-
final long totalBlocks = stat.getBlockCountLong();
457-
return totalBlocks * blockSize;
458-
} catch (Throwable e) {
459-
options.getLogger().log(SentryLevel.ERROR, "Error getting total external storage amount.", e);
460-
return null;
461-
}
438+
private long getTotalExternalStorage(final @NotNull StatFs stat) {
439+
final long blockSize = stat.getBlockSizeLong();
440+
final long totalBlocks = stat.getBlockCountLong();
441+
return totalBlocks * blockSize;
462442
}
463443

464444
/**
@@ -467,18 +447,10 @@ private Long getTotalExternalStorage(final @NotNull StatFs stat) {
467447
* @return the unused amount of external storage, in bytes, or null if no external storage is
468448
* mounted
469449
*/
470-
@Nullable
471-
private Long getUnusedExternalStorage(final @NotNull StatFs stat) {
472-
try {
473-
final long blockSize = stat.getBlockSizeLong();
474-
final long availableBlocks = stat.getAvailableBlocksLong();
475-
return availableBlocks * blockSize;
476-
} catch (Throwable e) {
477-
options
478-
.getLogger()
479-
.log(SentryLevel.ERROR, "Error getting unused external storage amount.", e);
480-
return null;
481-
}
450+
private long getUnusedExternalStorage(final @NotNull StatFs stat) {
451+
final long blockSize = stat.getBlockSizeLong();
452+
final long availableBlocks = stat.getAvailableBlocksLong();
453+
return availableBlocks * blockSize;
482454
}
483455

484456
@Nullable

‎sentry-jdbc/src/main/java/io/sentry/jdbc/DatabaseUtils.java‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,11 @@ public static DatabaseDetails parse(final @Nullable String databaseConnectionUrl
8080
return parseAsUri(dbSystem, urlString);
8181
}
8282
if ("sqlserver".equalsIgnoreCase(dbSystem)) {
83-
try {
84-
String dbProperty = ";databasename=";
85-
final int index = urlString.indexOf(dbProperty);
86-
if (index >= 0) {
87-
final @NotNull String dbNameAndMaybeMore =
88-
urlString.substring(index + dbProperty.length());
89-
return new DatabaseDetails(
90-
dbSystem, StringUtils.substringBefore(dbNameAndMaybeMore, ";"));
91-
}
92-
} catch (Throwable t) {
93-
// ignore
83+
String dbProperty = ";databasename=";
84+
final int index = urlString.indexOf(dbProperty);
85+
if (index >= 0) {
86+
final @NotNull String dbNameAndMaybeMore = urlString.substring(index + dbProperty.length());
87+
return new DatabaseDetails(dbSystem, StringUtils.substringBefore(dbNameAndMaybeMore, ";"));
9488
}
9589
}
9690
if ("oracle".equalsIgnoreCase(dbSystem)) {

‎sentry/src/main/java/io/sentry/Baggage.java‎

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -112,53 +112,46 @@ public static Baggage fromHeader(
112112
@Nullable Double sampleRand = null;
113113

114114
if (headerValue != null) {
115-
try {
116-
// see https://errorprone.info/bugpattern/StringSplitter for why limit is passed
117-
final String[] keyValueStrings = headerValue.split(",", -1);
118-
for (final String keyValueString : keyValueStrings) {
119-
if (keyValueString.trim().startsWith(SENTRY_BAGGAGE_PREFIX)) {
120-
try {
121-
// As per spec (https://www.w3.org/TR/baggage/#value)
122-
// a value may contain "=" signs, thus we split by the first occurrence
123-
final int separatorIndex = keyValueString.indexOf("=");
124-
final String key = keyValueString.substring(0, separatorIndex).trim();
125-
final String keyDecoded = decode(key);
126-
final String value = keyValueString.substring(separatorIndex + 1).trim();
127-
final String valueDecoded = decode(value);
128-
129-
if (DSCKeys.SAMPLE_RATE.equals(keyDecoded)) {
130-
sampleRate = toDouble(valueDecoded);
131-
} else if (DSCKeys.SAMPLE_RAND.equals(keyDecoded)) {
132-
sampleRand = toDouble(valueDecoded);
133-
} else {
134-
keyValues.put(keyDecoded, valueDecoded);
135-
}
136-
// Without ignoring SAMPLE_RAND here, we'd be freezing baggage that we're transporting
137-
// via OTel span attributes.
138-
// This is done when a transaction is created via Sentry API.
139-
// In that case Baggage is created before the OTel span is created and we put it on
140-
// the span attributes.
141-
// It does however only contain the sample random value as its only value.
142-
// The OTel code then uses it to create a propagation context from it and ends up
143-
// freezing it,
144-
// preventing outgoing requests (to other systems or Sentry) from adding info to
145-
// baggage and only then freeze it.
146-
if (!DSCKeys.SAMPLE_RAND.equalsIgnoreCase(key)) {
147-
shouldFreeze = true;
148-
}
149-
} catch (Throwable e) {
150-
logger.log(
151-
SentryLevel.ERROR,
152-
e,
153-
"Unable to decode baggage key value pair %s",
154-
keyValueString);
115+
// see https://errorprone.info/bugpattern/StringSplitter for why limit is passed
116+
final String[] keyValueStrings = headerValue.split(",", -1);
117+
for (final String keyValueString : keyValueStrings) {
118+
if (keyValueString.trim().startsWith(SENTRY_BAGGAGE_PREFIX)) {
119+
try {
120+
// As per spec (https://www.w3.org/TR/baggage/#value)
121+
// a value may contain "=" signs, thus we split by the first occurrence
122+
final int separatorIndex = keyValueString.indexOf("=");
123+
final String key = keyValueString.substring(0, separatorIndex).trim();
124+
final String keyDecoded = decode(key);
125+
final String value = keyValueString.substring(separatorIndex + 1).trim();
126+
final String valueDecoded = decode(value);
127+
128+
if (DSCKeys.SAMPLE_RATE.equals(keyDecoded)) {
129+
sampleRate = toDouble(valueDecoded);
130+
} else if (DSCKeys.SAMPLE_RAND.equals(keyDecoded)) {
131+
sampleRand = toDouble(valueDecoded);
132+
} else {
133+
keyValues.put(keyDecoded, valueDecoded);
134+
}
135+
// Without ignoring SAMPLE_RAND here, we'd be freezing baggage that we're transporting
136+
// via OTel span attributes.
137+
// This is done when a transaction is created via Sentry API.
138+
// In that case Baggage is created before the OTel span is created and we put it on
139+
// the span attributes.
140+
// It does however only contain the sample random value as its only value.
141+
// The OTel code then uses it to create a propagation context from it and ends up
142+
// freezing it,
143+
// preventing outgoing requests (to other systems or Sentry) from adding info to
144+
// baggage and only then freeze it.
145+
if (!DSCKeys.SAMPLE_RAND.equalsIgnoreCase(key)) {
146+
shouldFreeze = true;
155147
}
156-
} else if (includeThirdPartyValues) {
157-
thirdPartyKeyValueStrings.add(keyValueString.trim());
148+
} catch (Throwable e) {
149+
logger.log(
150+
SentryLevel.ERROR, e, "Unable to decode baggage key value pair %s", keyValueString);
158151
}
152+
} else if (includeThirdPartyValues) {
153+
thirdPartyKeyValueStrings.add(keyValueString.trim());
159154
}
160-
} catch (Throwable e) {
161-
logger.log(SentryLevel.ERROR, e, "Unable to decode baggage header %s", headerValue);
162155
}
163156
}
164157
final String thirdPartyHeader =

‎sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,6 @@ private void sendCachedEnvelopes(
173173
SentryLevel.ERROR,
174174
"Failed to call the executor. Cached events will not be sent. Did you call Sentry.close()?",
175175
e);
176-
} catch (Throwable e) {
177-
options
178-
.getLogger()
179-
.log(SentryLevel.ERROR, "Failed to call the executor. Cached events will not be sent", e);
180176
}
181177
}
182178
}

‎sentry/src/main/java/io/sentry/util/CheckInUtils.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,8 @@ public static boolean isIgnored(
130130
}
131131

132132
for (final FilterString ignoredSlug : ignoredSlugs) {
133-
try {
134-
if (ignoredSlug.matches(slug)) {
135-
return true;
136-
}
137-
} catch (Throwable t) {
138-
// ignore invalid regex
133+
if (ignoredSlug.matches(slug)) {
134+
return true;
139135
}
140136
}
141137

‎sentry/src/main/java/io/sentry/util/SpanUtils.java‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,9 @@ public static boolean isIgnored(
7878
}
7979

8080
for (final FilterString ignoredOrigin : ignoredOrigins) {
81-
try {
82-
if (ignoredOrigin.matches(origin)) {
83-
ignoredSpanDecisionsCache.put(origin, true);
84-
return true;
85-
}
86-
} catch (Throwable t) {
87-
// ignore invalid regex
81+
if (ignoredOrigin.matches(origin)) {
82+
ignoredSpanDecisionsCache.put(origin, true);
83+
return true;
8884
}
8985
}
9086

‎sentry/src/main/java/io/sentry/util/StringUtils.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ private StringUtils() {}
150150
// For specifying wrong message digest algorithms
151151
catch (NoSuchAlgorithmException e) {
152152
logger.log(SentryLevel.INFO, "SHA-1 isn't available to calculate the hash.", e);
153-
} catch (Throwable e) {
154-
logger.log(SentryLevel.INFO, "string: %s could not calculate its hash", e, str);
155153
}
156154
return null;
157155
}

‎sentry/src/main/java/io/sentry/util/TracingUtils.java‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,8 @@ public static boolean isIgnored(
187187
}
188188

189189
for (final FilterString ignoredTransaction : ignoredTransactions) {
190-
191-
try {
192-
if (ignoredTransaction.matches(transactionName)) {
193-
return true;
194-
}
195-
} catch (Throwable t) {
196-
// ignore invalid regex
190+
if (ignoredTransaction.matches(transactionName)) {
191+
return true;
197192
}
198193
}
199194

0 commit comments

Comments
 (0)