Skip to content

Commit

Permalink
catch throwable to suppress errors in Java (#1812)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-garcia committed Nov 18, 2021
1 parent 2d41eca commit 380ea81
Show file tree
Hide file tree
Showing 57 changed files with 104 additions and 94 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Ref: catch Throwable instead of Exception to suppress internal SDK errors (#1812)

## 5.4.1

* Feat: Refactor OkHttp and Apollo to Kotlin functional interfaces (#1797)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ synchronized void setMetrics(final @NotNull Activity activity, final @NotNull Se
SparseIntArray[] framesRates = null;
try {
framesRates = frameMetricsAggregator.remove(activity);
} catch (Exception ignored) {
} catch (Throwable ignored) {
// throws IllegalArgumentException when attempting to remove OnFrameMetricsAvailableListener
// that was never added.
// there's no contains method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
options
.getLogger()
.log(SentryLevel.DEBUG, "AppComponentsBreadcrumbsIntegration installed.");
} catch (Exception e) {
} catch (Throwable e) {
this.options.setEnableAppComponentBreadcrumbs(false);
options.getLogger().log(SentryLevel.INFO, e, "ComponentCallbacks2 is not available.");
}
Expand All @@ -63,8 +63,13 @@ public void close() throws IOException {
try {
// if its a ContextImpl, unregisterComponentCallbacks can't be used
context.unregisterComponentCallbacks(this);
} catch (Exception ignored) {
} catch (Throwable ignored) {
// fine, might throw on older versions
if (options != null) {
options
.getLogger()
.log(SentryLevel.DEBUG, ignored, "It was not possible to unregisterComponentCallbacks");
}
}

if (options != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private ContextUtils() {}
static PackageInfo getPackageInfo(final @NotNull Context context, final @NotNull ILogger logger) {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting package info.", e);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private void setArchitectures(final @NotNull Device device) {
if (emulator != null) {
device.setSimulator((Boolean) emulator);
}
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting emulator.", e);
}

Expand Down Expand Up @@ -419,7 +419,7 @@ private TimeZone getTimeZone() {
}
logger.log(SentryLevel.INFO, "Error getting MemoryInfo.");
return null;
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting MemoryInfo.", e);
return null;
}
Expand All @@ -438,7 +438,7 @@ private TimeZone getTimeZone() {
private @Nullable String getFamily() {
try {
return Build.MODEL.split(" ", -1)[0];
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting device family.", e);
return null;
}
Expand All @@ -461,7 +461,7 @@ private TimeZone getTimeZone() {
float percentMultiplier = 100.0f;

return ((float) level / (float) scale) * percentMultiplier;
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting device battery level.", e);
return null;
}
Expand All @@ -477,7 +477,7 @@ private TimeZone getTimeZone() {
int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
return plugged == BatteryManager.BATTERY_PLUGGED_AC
|| plugged == BatteryManager.BATTERY_PLUGGED_USB;
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting device charging state.", e);
return null;
}
Expand All @@ -489,7 +489,7 @@ private TimeZone getTimeZone() {
if (temperature != -1) {
return ((float) temperature) / 10; // celsius
}
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting battery temperature.", e);
}
return null;
Expand All @@ -512,7 +512,7 @@ private TimeZone getTimeZone() {
"No device orientation available (ORIENTATION_SQUARE|ORIENTATION_UNDEFINED)");
return null;
}
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting device orientation.", e);
}
return deviceOrientation;
Expand Down Expand Up @@ -542,7 +542,7 @@ private TimeZone getTimeZone() {
|| Build.PRODUCT.contains("vbox86p")
|| Build.PRODUCT.contains("emulator")
|| Build.PRODUCT.contains("simulator");
} catch (Exception e) {
} catch (Throwable e) {
logger.log(
SentryLevel.ERROR, "Error checking whether application is running in an emulator.", e);
return null;
Expand All @@ -559,7 +559,7 @@ private TimeZone getTimeZone() {
long blockSize = getBlockSizeLong(stat);
long totalBlocks = getBlockCountLong(stat);
return totalBlocks * blockSize;
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting total internal storage amount.", e);
return null;
}
Expand Down Expand Up @@ -614,7 +614,7 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
long blockSize = getBlockSizeLong(stat);
long availableBlocks = getAvailableBlocksLong(stat);
return availableBlocks * blockSize;
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting unused internal storage amount.", e);
return null;
}
Expand Down Expand Up @@ -686,7 +686,7 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
long blockSize = getBlockSizeLong(stat);
long totalBlocks = getBlockCountLong(stat);
return totalBlocks * blockSize;
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting total external storage amount.", e);
return null;
}
Expand All @@ -710,7 +710,7 @@ private boolean isExternalStorageMounted() {
long blockSize = getBlockSizeLong(stat);
long availableBlocks = getAvailableBlocksLong(stat);
return availableBlocks * blockSize;
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting unused external storage amount.", e);
return null;
}
Expand All @@ -724,7 +724,7 @@ private boolean isExternalStorageMounted() {
private @Nullable DisplayMetrics getDisplayMetrics() {
try {
return context.getResources().getDisplayMetrics();
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting DisplayMetrics.", e);
return null;
}
Expand All @@ -746,7 +746,7 @@ private boolean isExternalStorageMounted() {
if (rooted != null) {
os.setRooted((Boolean) rooted);
}
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting OperatingSystem.", e);
}

Expand Down Expand Up @@ -802,7 +802,7 @@ private void setAppPackageInfo(final @NotNull App app, final @NotNull PackageInf
} else {
return context.getString(stringId);
}
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting application name.", e);
}

Expand All @@ -824,7 +824,7 @@ private void setAppPackageInfo(final @NotNull App app, final @NotNull PackageInf
private @Nullable String getDeviceId() {
try {
return Installation.id(context);
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting installationId.", e);
}
return null;
Expand Down Expand Up @@ -876,7 +876,7 @@ private void setSideLoadedInfo(final @NotNull SentryBaseEvent event) {
event.setTag(entry.getKey(), entry.getValue());
}
}
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error getting side loaded info.", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions
try {
observer.startWatching();
logger.log(SentryLevel.DEBUG, "EnvelopeFileObserverIntegration installed.");
} catch (Exception e) {
} catch (Throwable e) {
// it could throw eg NoSuchFileException or NullPointerException
options
.getLogger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static synchronized String id(final @NotNull Context context) throws Runt
return deviceId;
}
deviceId = readInstallationFile(installation);
} catch (Exception e) {
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ static void applyMetadata(
options
.getLogger()
.log(SentryLevel.INFO, "Retrieving configuration from AndroidManifest.xml");
} catch (Exception e) {
} catch (Throwable e) {
options
.getLogger()
.log(
Expand Down Expand Up @@ -298,7 +298,7 @@ static boolean isAutoInit(final @NotNull Context context, final @NotNull ILogger
autoInit = readBool(metadata, logger, AUTO_INIT, true);
}
logger.log(SentryLevel.INFO, "Retrieving auto-init from AndroidManifest.xml");
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Failed to read auto-init from android manifest metadata.", e);
}
return autoInit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
telephonyManager.listen(listener, android.telephony.PhoneStateListener.LISTEN_CALL_STATE);

options.getLogger().log(SentryLevel.DEBUG, "PhoneStateBreadcrumbsIntegration installed.");
} catch (Exception e) {
} catch (Throwable e) {
this.options
.getLogger()
.log(SentryLevel.INFO, e, "TelephonyManager is not available or ready to use.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
this.options
.getLogger()
.log(SentryLevel.DEBUG, "SystemEventsBreadcrumbsIntegration installed.");
} catch (Exception e) {
} catch (Throwable e) {
this.options.setEnableSystemEventBreadcrumbs(false);
this.options
.getLogger()
Expand Down Expand Up @@ -201,7 +201,7 @@ public void onReceive(Context context, Intent intent) {
if (value != null) {
newExtras.put(item, value.toString());
}
} catch (Exception exception) {
} catch (Throwable exception) {
logger.log(
SentryLevel.ERROR,
exception,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private boolean checkSUExist() {
}
} catch (IOException e) {
logger.log(SentryLevel.DEBUG, "SU isn't found on this Device.");
} catch (Exception e) {
} catch (Throwable e) {
logger.log(SentryLevel.DEBUG, "Error when trying to check if SU exists.", e);
} finally {
if (process != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class DebugImagesLoader implements IDebugImagesLoader {
.getLogger()
.log(SentryLevel.DEBUG, "Debug images loaded: %d", debugImages.size());
}
} catch (Exception e) {
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Failed to load debug images.");
}
}
Expand All @@ -64,7 +64,7 @@ public void clearDebugImages() {
moduleListLoader.clearModuleList();

options.getLogger().log(SentryLevel.INFO, "Debug images cleared.");
} catch (Exception e) {
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Failed to clear debug images.");
}
debugImages = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void addBreadcrumb(final @NotNull Breadcrumb crumb) {
if (!dataRef.isEmpty()) {
data = options.getSerializer().serialize(dataRef);
}
} catch (Exception e) {
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Breadcrumb data is not serializable.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void cancelled() {
currentlyRunning.decrement();
}
});
} catch (Exception e) {
} catch (Throwable e) {
options.getLogger().log(ERROR, "Error when sending envelope", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public SentryInstrumentation() {
finish(span, environment, result);
}
return result;
} catch (Exception e) {
} catch (Throwable e) {
span.setThrowable(e);
span.setStatus(SpanStatus.INTERNAL_ERROR);
finish(span, environment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Response execute(final @NotNull Request request, final @NotNull Request.O
// handles both success and error responses
span.setStatus(SpanStatus.fromHttpStatusCode(response.status()));
return response;
} catch (Exception e) {
} catch (Throwable e) {
// handles cases like connection errors
span.setThrowable(e);
span.setStatus(SpanStatus.INTERNAL_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void main(String[] args) {

try {
throw new RuntimeException("Invalid productId=445");
} catch (Exception e) {
} catch (Throwable e) {
LOGGER.log(Level.SEVERE, "Something went wrong", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void main(String[] args) {

try {
throw new RuntimeException("Invalid productId=445");
} catch (Exception e) {
} catch (Throwable e) {
LOGGER.error("Something went wrong", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void main(String[] args) {

try {
throw new RuntimeException("Invalid productId=445");
} catch (Exception e) {
} catch (Throwable e) {
LOGGER.error("Something went wrong", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void configureScope(HttpServletRequest request) {
new RequestBodyExtractingEventProcessor(request, hub.getOptions()));
}
});
} catch (Exception e) {
} catch (Throwable e) {
hub.getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Failed to set scope for HTTP request", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public SentrySpanClientHttpRequestInterceptor(final @NotNull IHub hub) {
span.setStatus(SpanStatus.fromHttpStatusCode(response.getRawStatusCode()));
responseStatusCode = response.getRawStatusCode();
return response;
} catch (Exception e) {
} catch (Throwable e) {
// handles cases like connection errors
span.setThrowable(e);
span.setStatus(SpanStatus.INTERNAL_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected void doFilterInternal(
final ITransaction transaction = startTransaction(httpRequest, sentryTraceHeader);
try {
filterChain.doFilter(httpRequest, httpResponse);
} catch (Exception e) {
} catch (Throwable e) {
// exceptions that are not handled by Spring
transaction.setStatus(SpanStatus.INTERNAL_ERROR);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl
final Object result = invocation.proceed();
transaction.setStatus(SpanStatus.OK);
return result;
} catch (Exception e) {
} catch (Throwable e) {
transaction.setStatus(SpanStatus.INTERNAL_ERROR);
transaction.setThrowable(e);
throw e;
Expand Down

0 comments on commit 380ea81

Please sign in to comment.