From 31e6c7fcc8e67c81c013a5799de9640534e6ce90 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Thu, 2 Jul 2026 18:56:35 +0200 Subject: [PATCH] refactor(core): Consolidate scope-persistence error handling (JAVA-608) Extract the duplicated try/catch in PersistingScopeObserver.serializeToDisk into a single runSafely helper used by both the on-executor and submit paths. Pure internal cleanup; error-handling behavior is unchanged. Co-Authored-By: Claude Opus 4.8 --- CHANGELOG.md | 6 +++++ .../sentry/cache/PersistingScopeObserver.java | 25 ++++++++----------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a290e2b3e9..2950cb10768 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Internal + +- Consolidate the duplicated scope-persistence error handling in `PersistingScopeObserver` into a single code path ([#5692](https://github.com/getsentry/sentry-java/pull/5692)) + ## 8.47.0 ### Behavioral Changes diff --git a/sentry/src/main/java/io/sentry/cache/PersistingScopeObserver.java b/sentry/src/main/java/io/sentry/cache/PersistingScopeObserver.java index c9356579c9c..edfcc71b9a6 100644 --- a/sentry/src/main/java/io/sentry/cache/PersistingScopeObserver.java +++ b/sentry/src/main/java/io/sentry/cache/PersistingScopeObserver.java @@ -231,30 +231,25 @@ private void serializeToDisk(final @NotNull Runnable task) { } if (Thread.currentThread().getName().contains("SentryExecutor")) { // we're already on the sentry executor thread, so we can just execute it directly - try { - task.run(); - } catch (Throwable e) { - options.getLogger().log(ERROR, "Serialization task failed", e); - } + runSafely(task); return; } try { - options - .getExecutorService() - .submit( - () -> { - try { - task.run(); - } catch (Throwable e) { - options.getLogger().log(ERROR, "Serialization task failed", e); - } - }); + options.getExecutorService().submit(() -> runSafely(task)); } catch (Throwable e) { options.getLogger().log(ERROR, "Serialization task could not be scheduled", e); } } + private void runSafely(final @NotNull Runnable task) { + try { + task.run(); + } catch (Throwable e) { + options.getLogger().log(ERROR, "Serialization task failed", e); + } + } + private void store(final @NotNull T entity, final @NotNull String fileName) { store(options, entity, fileName); }