This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 3124799065 TODO-290: Add cause-carrying Shorts exception factories;
convert 2 IDIOM-02 sites
3124799065 is described below
commit 3124799065a3d5dcdf347a54f0a6b46b27a13da0
Author: James Bognar <[email protected]>
AuthorDate: Thu Jul 23 14:31:07 2026 -0400
TODO-290: Add cause-carrying Shorts exception factories; convert 2 IDIOM-02
sites
Adds the Throwable-first isex(Throwable, String, Object...) and bare-cause
isex(Throwable) overloads to Shorts (completing the iaex/isex/rex/ioex
cause-carrying family for symmetry; the other three already existed), with
unit tests. Converts the 2 cause-carrying constructor sites flagged by the
IDIOM sweep: LegacyHttpResponseAdapter (isex) and DumpsManager (rex).
Full reactor suite green.
Co-authored-by: Cursor <[email protected]>
---
.../org/apache/juneau/commons/utils/Shorts.java | 6 ++++
.../apache/juneau/commons/utils/Shorts_Test.java | 40 ++++++++++++++++++++++
.../rest/server/management/DumpsManager.java | 4 ++-
.../processor/LegacyHttpResponseAdapter.java | 2 +-
4 files changed, 50 insertions(+), 2 deletions(-)
diff --git
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/Shorts.java
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/Shorts.java
index ca52b6fe7f..a81633282b 100644
---
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/Shorts.java
+++
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/Shorts.java
@@ -1521,6 +1521,12 @@ public class Shorts {
/** Creates an {@link IllegalStateException} with a formatted message.
*/
public static IllegalStateException isex(String m, Object...a) { return
new IllegalStateException(StringUtils.format(m, a)); }
+ /** Creates an {@link IllegalStateException} wrapping a cause. */
+ public static IllegalStateException isex(Throwable t) { return new
IllegalStateException(t); }
+
+ /** Creates an {@link IllegalStateException} with a cause and formatted
message. */
+ public static IllegalStateException isex(Throwable t, String m,
Object...a) { return new IllegalStateException(StringUtils.format(m, a), t); }
+
/** Creates an {@link UnsupportedOperationException} with the message
"Not supported." */
public static UnsupportedOperationException uoex() { return new
UnsupportedOperationException("Not supported."); }
diff --git
a/juneau-core/juneau-commons/src/test/java/org/apache/juneau/commons/utils/Shorts_Test.java
b/juneau-core/juneau-commons/src/test/java/org/apache/juneau/commons/utils/Shorts_Test.java
index 2a20cc07c3..b3ad163f56 100644
---
a/juneau-core/juneau-commons/src/test/java/org/apache/juneau/commons/utils/Shorts_Test.java
+++
b/juneau-core/juneau-commons/src/test/java/org/apache/juneau/commons/utils/Shorts_Test.java
@@ -20,6 +20,7 @@ import static org.apache.juneau.commons.utils.Shorts.*;
import static org.apache.juneau.commons.utils.ThrowableUtils.*;
import static org.junit.jupiter.api.Assertions.*;
+import java.io.*;
import java.util.*;
import org.apache.juneau.commons.*;
@@ -291,6 +292,45 @@ class Shorts_Test extends TestBase {
assertEquals("ok", r);
}
+ @Test
+ void e009_rex_causeCarrying() {
+ Throwable cause = new Exception("root");
+ RuntimeException e = rex(cause, "test %s", "msg");
+ assertEquals("test msg", e.getMessage());
+ assertSame(cause, e.getCause());
+ }
+
+ @Test
+ void e010_iaex_causeCarrying() {
+ Throwable cause = new Exception("root");
+ IllegalArgumentException e = iaex(cause, "bad arg %s", "x");
+ assertEquals("bad arg x", e.getMessage());
+ assertSame(cause, e.getCause());
+ }
+
+ @Test
+ void e011_isex_causeCarrying() {
+ Throwable cause = new Exception("root");
+ IllegalStateException e = isex(cause, "bad state %s", 1);
+ assertEquals("bad state 1", e.getMessage());
+ assertSame(cause, e.getCause());
+ }
+
+ @Test
+ void e012_ioex_causeCarrying() {
+ Throwable cause = new Exception("root");
+ IOException e = ioex(cause, "bad io %s", "y");
+ assertEquals("bad io y", e.getMessage());
+ assertSame(cause, e.getCause());
+ }
+
+ @Test
+ void e013_isex_causeOnly() {
+ Throwable cause = new Exception("root");
+ IllegalStateException e = isex(cause);
+ assertSame(cause, e.getCause());
+ }
+
// ---- PredicateUtils aliases ----
@Test
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/server/management/DumpsManager.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/server/management/DumpsManager.java
index 1488e5397c..f642c7c33b 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/server/management/DumpsManager.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/server/management/DumpsManager.java
@@ -16,6 +16,8 @@
*/
package org.apache.juneau.rest.server.management;
+import static org.apache.juneau.commons.utils.Shorts.*;
+
import java.io.*;
import java.lang.management.*;
import java.nio.file.*;
@@ -103,7 +105,7 @@ public class DumpsManager {
} catch (ClassNotFoundException | NoSuchMethodException e) {
return false; // HTT: non-HotSpot JVM with no
heap-dump support — not reproducible on HotSpot CI (caller degrades to 501).
} catch (ReflectiveOperationException e) {
- throw new RuntimeException("Failed to write heap dump
to " + target.getAbsolutePath(), e);
+ throw rex(e, "Failed to write heap dump to %s",
target.getAbsolutePath());
}
}
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/server/processor/LegacyHttpResponseAdapter.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/server/processor/LegacyHttpResponseAdapter.java
index d080105be9..89063647dc 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/server/processor/LegacyHttpResponseAdapter.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/server/processor/LegacyHttpResponseAdapter.java
@@ -138,7 +138,7 @@ final class LegacyHttpResponseAdapter {
var m = target.getClass().getMethod(method, argTypes);
return m.invoke(target, args);
} catch (NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
- throw new IllegalStateException("Failed to invoke " +
method + " on " + cn(target), e);
+ throw isex(e, "Failed to invoke %s on %s", method,
cn(target));
}
}