Skip to content

Commit d8ba375

Browse files
runningcodeclaude
andcommitted
ref: Narrow Throwable catches to the exceptions they guard
Replace catch (Throwable) with the types the guarded code can throw, so SDK bugs and fatal errors are no longer swallowed: RejectedExecutionException around executor submit and schedule, IOException and SecurityException around file I/O, and the specific parse exceptions elsewhere. Drop the try where nothing in it can throw. Several tests only passed because these catches hid broken mocks: executor mocks that returned Unit from submit, mock Contexts without assets, a Runtime.exec that returned null. Make those mocks behave like the real objects. Refs JAVA-552 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e6e5918 commit d8ba375

54 files changed

Lines changed: 163 additions & 109 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ private void sendChunks(final @NotNull IScopes scopes, final @NotNull SentryOpti
382382
scopes.captureProfileChunk(payload);
383383
}
384384
});
385-
} catch (Throwable e) {
385+
} catch (RejectedExecutionException e) {
386386
options.getLogger().log(SentryLevel.DEBUG, "Failed to send profile chunks.", e);
387387
}
388388
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.sentry.SentryLevel;
55
import io.sentry.SentryOptions;
66
import io.sentry.logger.LoggerBatchProcessor;
7+
import java.util.concurrent.RejectedExecutionException;
78
import org.jetbrains.annotations.ApiStatus;
89
import org.jetbrains.annotations.NotNull;
910

@@ -38,7 +39,7 @@ public void run() {
3839
flush(LoggerBatchProcessor.FLUSH_AFTER_MS);
3940
}
4041
});
41-
} catch (Throwable t) {
42+
} catch (RejectedExecutionException t) {
4243
options.getLogger().log(SentryLevel.ERROR, t, "Failed to submit log flush in onBackground()");
4344
}
4445
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.sentry.SentryLevel;
55
import io.sentry.SentryOptions;
66
import io.sentry.metrics.MetricsBatchProcessor;
7+
import java.util.concurrent.RejectedExecutionException;
78
import org.jetbrains.annotations.ApiStatus;
89
import org.jetbrains.annotations.NotNull;
910

@@ -38,7 +39,7 @@ public void run() {
3839
flush(MetricsBatchProcessor.FLUSH_AFTER_MS);
3940
}
4041
});
41-
} catch (Throwable t) {
42+
} catch (RejectedExecutionException t) {
4243
options
4344
.getLogger()
4445
.log(SentryLevel.ERROR, t, "Failed to submit metrics flush in onBackground()");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public void onFrameMetricCollected(
214214
isRunning = true;
215215
return new ProfileStartData(
216216
profileStartNanos, profileStartCpuMillis, profileStartTimestamp);
217-
} catch (Throwable e) {
217+
} catch (RuntimeException e) {
218218
endAndCollect(false, null);
219219
logger.log(SentryLevel.ERROR, "Unable to start a profile: ", e);
220220
isRunning = false;
@@ -237,7 +237,7 @@ public void onFrameMetricCollected(
237237
// wrapped, so this should never happen (except for tests, where this is the only method
238238
// that throws)
239239
Debug.stopMethodTracing();
240-
} catch (Throwable e) {
240+
} catch (RuntimeException e) {
241241
logger.log(SentryLevel.ERROR, "Error while stopping profiling: ", e);
242242
} finally {
243243
isRunning = false;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.sentry.util.Objects;
2121
import java.io.Closeable;
2222
import java.io.IOException;
23+
import java.util.concurrent.RejectedExecutionException;
2324
import org.jetbrains.annotations.NotNull;
2425
import org.jetbrains.annotations.Nullable;
2526
import org.jetbrains.annotations.TestOnly;
@@ -74,7 +75,7 @@ private void register(
7475
}
7576
}
7677
});
77-
} catch (Throwable e) {
78+
} catch (RejectedExecutionException e) {
7879
options
7980
.getLogger()
8081
.log(SentryLevel.DEBUG, "Failed to start AnrIntegration on executor thread.", e);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.io.InputStream;
4040
import java.io.InputStreamReader;
4141
import java.util.List;
42+
import java.util.concurrent.RejectedExecutionException;
4243
import org.jetbrains.annotations.ApiStatus;
4344
import org.jetbrains.annotations.NotNull;
4445
import org.jetbrains.annotations.Nullable;
@@ -88,7 +89,7 @@ public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) {
8889
.submit(
8990
new ApplicationExitInfoHistoryDispatcher(
9091
context, scopes, this.options, dateProvider, new AnrV2Policy(this.options)));
91-
} catch (Throwable e) {
92+
} catch (RejectedExecutionException e) {
9293
options.getLogger().log(SentryLevel.DEBUG, "Failed to start ANR processor.", e);
9394
}
9495
options.getLogger().log(SentryLevel.DEBUG, "AnrV2Integration installed.");
@@ -204,7 +205,7 @@ public void markReported(final long timestamp) {
204205
return new ParseResult(ParseResult.Type.NO_DUMP);
205206
}
206207
dump = NativeEventUtils.readBytes(trace);
207-
} catch (Throwable e) {
208+
} catch (IOException e) {
208209
options.getLogger().log(SentryLevel.WARNING, "Failed to read ANR thread dump", e);
209210
return new ParseResult(ParseResult.Type.NO_DUMP);
210211
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.Closeable;
2020
import java.io.IOException;
2121
import java.util.Locale;
22+
import java.util.concurrent.RejectedExecutionException;
2223
import org.jetbrains.annotations.NotNull;
2324
import org.jetbrains.annotations.Nullable;
2425

@@ -173,7 +174,7 @@ private void executeInBackground(final @NotNull Runnable runnable) {
173174
if (options != null) {
174175
try {
175176
options.getExecutorService().submit(runnable);
176-
} catch (Throwable t) {
177+
} catch (RejectedExecutionException t) {
177178
options
178179
.getLogger()
179180
.log(SentryLevel.ERROR, t, "Failed to submit app components breadcrumb task");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ private boolean sampleReplay(
234234
event.getEventId());
235235
return false;
236236
}
237-
} catch (Throwable e) {
237+
} catch (NumberFormatException e) {
238238
options.getLogger().log(SentryLevel.ERROR, "Error parsing replay sample rate.", e);
239239
return false;
240240
}
@@ -468,7 +468,7 @@ private void setAppVersionAndBuild(final @NotNull SentryBaseEvent event) {
468468
app.setAppVersion(versionName);
469469
app.setAppBuild(versionCode);
470470
event.getContexts().setApp(app);
471-
} catch (Throwable e) {
471+
} catch (IndexOutOfBoundsException e) {
472472
options
473473
.getLogger()
474474
.log(SentryLevel.WARNING, "Failed to parse release from scope cache: %s", release);
@@ -699,7 +699,7 @@ private void mergeUser(final @NotNull SentryBaseEvent event) {
699699
private @Nullable String getDeviceId() {
700700
try {
701701
return Installation.id(context);
702-
} catch (Throwable e) {
702+
} catch (RuntimeException e) {
703703
options.getLogger().log(SentryLevel.ERROR, "Error getting installationId.", e);
704704
}
705705
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ private long getUnusedExternalStorage(final @NotNull StatFs stat) {
457457
private String getDeviceId() {
458458
try {
459459
return Installation.id(context);
460-
} catch (Throwable e) {
460+
} catch (RuntimeException e) {
461461
options.getLogger().log(SentryLevel.ERROR, "Error getting installationId.", e);
462462
}
463463
return null;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.sentry.util.Objects;
1515
import java.io.Closeable;
1616
import java.io.File;
17+
import java.util.concurrent.RejectedExecutionException;
1718
import org.jetbrains.annotations.NotNull;
1819
import org.jetbrains.annotations.Nullable;
1920
import org.jetbrains.annotations.TestOnly;
@@ -56,7 +57,7 @@ public final void register(final @NotNull IScopes scopes, final @NotNull SentryO
5657
}
5758
}
5859
});
59-
} catch (Throwable e) {
60+
} catch (RejectedExecutionException e) {
6061
logger.log(
6162
SentryLevel.DEBUG,
6263
"Failed to start EnvelopeFileObserverIntegration on executor thread.",

0 commit comments

Comments
 (0)