Repository navigation
Add ExceptionUtil to reduce repetitive exception handling - #3272
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📜 Recent review details⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
📝 WalkthroughWalkthroughAdds a new thread-safe ExceptionUtil utility (throwing functional interfaces, default/optional wrappers, and runnable helpers) and updates NVML, CoreFoundation, and IOKit FFM bindings to use these helpers instead of scattered try/catch blocks. ChangesException Utility and Refactoring
🎯 3 (Moderate) | ⏱️ ~25 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
oshi-common/src/main/java/oshi/util/ExceptionUtil.java (1)
102-109: ⚡ Quick winPreserve stack traces in debug logging overloads.
The current logging calls at Line 106 and in the other 8 overloads drop the exception stack trace by only passing
t.getMessage()to the logger. Apply the fix below to preserve the full stack trace, which is essential for diagnosing failures in native/FFM code paths:Proposed patch
- } catch (Throwable t) { - log.debug(msg, t.getMessage()); + } catch (Throwable t) { + log.debug(msg, t.getMessage(), t); return defaultValue; }Apply the same change to all logging overloads in the class (Lines 106, 140, 174, 209, 244, 262, 280, 298, and 327).
This follows the SLF4J best practice: when a Throwable is the final argument in a log call, SLF4J automatically appends the full stack trace while still formatting any message placeholders with the preceding arguments.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@oshi-common/src/main/java/oshi/util/ExceptionUtil.java` around lines 102 - 109, The debug log calls in ExceptionUtil (e.g., getOrDefault(ThrowingSupplier) and the other overloads referenced) currently pass t.getMessage(), which drops the stack trace; change each log.debug(...) to pass the Throwable as the last argument (for example log.debug(msg, t)) so SLF4J will append the full stack trace while preserving message formatting; apply this same adjustment to all overloads in the class (the methods around the reported locations including the overloads at the other nine spots) to ensure full exception details are logged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@oshi-common/src/main/java/oshi/util/ExceptionUtil.java`:
- Around line 102-109: The debug log calls in ExceptionUtil (e.g.,
getOrDefault(ThrowingSupplier) and the other overloads referenced) currently
pass t.getMessage(), which drops the stack trace; change each log.debug(...) to
pass the Throwable as the last argument (for example log.debug(msg, t)) so SLF4J
will append the full stack trace while preserving message formatting; apply this
same adjustment to all overloads in the class (the methods around the reported
locations including the overloads at the other nine spots) to ensure full
exception details are logged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 13b51a02-80a9-41d7-861c-a63769d1248a
📒 Files selected for processing (5)
oshi-common/src/main/java/oshi/util/ExceptionUtil.javaoshi-common/src/test/java/oshi/util/ExceptionUtilTest.javaoshi-core-ffm/src/main/java/oshi/ffm/common/NvmlFunctions.javaoshi-core-ffm/src/main/java/oshi/ffm/mac/CoreFoundation.javaoshi-core-ffm/src/main/java/oshi/ffm/mac/IOKit.java
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Analyze (java)
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: Test JDK 11, macos-latest
- GitHub Check: JMH Benchmarks, ubuntu-latest
- GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
- GitHub Check: Test JDK 21, macos-latest
- GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
- GitHub Check: Test JDK 21, windows-latest
- GitHub Check: JMH Benchmarks, windows-latest
- GitHub Check: JMH Benchmarks, macos-latest
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-04-12T19:22:09.716Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3149
File: oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java:159-163
Timestamp: 2026-04-12T19:22:09.716Z
Learning: When reviewing OSHI hardware platform code that uses or implements `CentralProcessor`/`PhysicalProcessor.getEfficiency()`, remember the API’s counterintuitive semantics: a *higher* efficiency-class value indicates *greater performance* (lower efficiency). P-cores/“performance cores” should therefore have higher values (e.g., 1) than E-cores/“efficiency cores” (e.g., 0), as documented in the `CentralProcessor` Javadoc. Do not flag code as incorrect solely because ARM_P_CORES or Intel P-cores are assigned higher efficiency values.
Applied to files:
oshi-common/src/main/java/oshi/util/ExceptionUtil.java
🔇 Additional comments (4)
oshi-core-ffm/src/main/java/oshi/ffm/common/NvmlFunctions.java (1)
23-24: LGTM!Also applies to: 158-159, 168-169, 179-179, 190-191, 203-203, 214-214, 225-226, 237-237, 249-250, 261-261, 273-274, 285-285
oshi-core-ffm/src/main/java/oshi/ffm/mac/CoreFoundation.java (1)
43-44: LGTM!Also applies to: 111-111, 129-129, 138-138, 154-154, 162-162, 195-203, 215-223, 235-243, 255-263, 275-283, 295-303, 327-327, 351-351, 364-364, 388-388, 400-400, 412-418, 443-443, 455-455, 469-470, 492-492, 514-525, 536-559, 577-578, 600-607, 618-619
oshi-core-ffm/src/main/java/oshi/ffm/mac/IOKit.java (1)
39-40: LGTM!Also applies to: 72-72, 79-84, 97-100, 126-135, 142-151, 158-169, 176-187, 194-205, 212-215, 222-232, 239-245, 250-264, 268-287, 290-309, 312-331, 334-350, 354-368, 407-407
oshi-common/src/test/java/oshi/util/ExceptionUtilTest.java (1)
1-281: LGTM!
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3272 +/- ##
============================================
- Coverage 74.56% 74.52% -0.05%
Complexity 73 73
============================================
Files 406 407 +1
Lines 16047 16103 +56
Branches 2554 2554
============================================
+ Hits 11965 12000 +35
- Misses 2602 2614 +12
- Partials 1480 1489 +9
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Introduce oshi.util.ExceptionUtil with functional interfaces (ThrowingSupplier, ThrowingIntSupplier, ThrowingLongSupplier, ThrowingBooleanSupplier, ThrowingDoubleSupplier, ThrowingRunnable) and utility methods that consolidate the try/catch/return-default pattern used extensively in FFM native call wrappers. Refactor three of the most repetitive FFM files as demonstration: - NvmlFunctions.java: 13 catch blocks reduced to one-liners - CoreFoundation.java: 26 catch blocks reduced to one-liners - IOKit.java: 18 catch blocks reduced to one-liners This eliminates ~400 lines of boilerplate while preserving identical behavior. The ExceptionUtil class is fully tested with 30 test cases covering success paths, exception paths, error paths, and edge cases. The same pattern can be applied to the remaining ~220 catch(Throwable) blocks in the FFM module in follow-up commits.
33553c7 to
ace1df9
Compare
Cover the return supplier.getAsFoo() line in getIntOrDefault, getLongOrDefault, getBooleanOrDefault, and getDoubleOrDefault logging variants that were missing test coverage.
Summary
Introduces
oshi.util.ExceptionUtil— a utility class that consolidates the repetitive try/catch/return-default pattern used extensively in FFM native call wrappers.Problem
The codebase has 278
catch (Throwable)blocks, almost all in the FFM module, following the same pattern:This creates significant duplication (~760 lines of boilerplate) and makes the error-handling paths untestable.
Solution
ExceptionUtilprovides functional interfaces and utility methods:getOrDefault(ThrowingSupplier<T>, T)— silent variantgetOrDefault(ThrowingSupplier<T>, T, Logger, String)— logging variantgetIntOrDefault,getLongOrDefault,getBooleanOrDefault,getDoubleOrDefault— primitive variantsgetOptional,getOptionalInt,getOptionalLong— Optional-returning variantsrunSilently,runOrLog— void operation variantsBefore:
After:
Changes
oshi-common/.../util/ExceptionUtil.java— the utility classoshi-common/.../util/ExceptionUtilTest.java— 30 test casesNvmlFunctions.java(13 catch blocks → one-liners)CoreFoundation.java(26 catch blocks → one-liners)IOKit.java(18 catch blocks → one-liners)Net: ~400 lines of boilerplate eliminated in this PR. The same pattern can be applied to the remaining ~220
catch(Throwable)blocks in follow-up PRs.Testing
ExceptionUtilTestcovers all methods with success, exception, and error (Throwable) pathsoshi-commonfull test suite passesoshi-commonandoshi-core-ffmcompile cleanlySummary by CodeRabbit