11package io .nexusrpc .handler ;
22
3+ import io .nexusrpc .FailureInfo ;
34import java .util .Arrays ;
45import org .jspecify .annotations .Nullable ;
56
@@ -33,36 +34,160 @@ public enum RetryBehavior {
3334 private final String rawErrorType ;
3435 private final ErrorType errorType ;
3536 private final RetryBehavior retryBehavior ;
37+ private final FailureInfo originalFailure ;
3638
37- public HandlerException (ErrorType errorType , String message ) {
38- this (errorType , new RuntimeException (message ), RetryBehavior .UNSPECIFIED );
39+ /**
40+ * Create a handler exception with the given error type and cause message.
41+ *
42+ * @param errorType The error type.
43+ * @param causeMessage The cause message.
44+ * @deprecated
45+ */
46+ public HandlerException (ErrorType errorType , String causeMessage ) {
47+ this (errorType , new RuntimeException (causeMessage ), RetryBehavior .UNSPECIFIED );
48+ }
49+
50+ /**
51+ * Create a handler exception with the given error type and cause message.
52+ *
53+ * @param errorType The error type.
54+ * @param causeMessage The cause message.
55+ * @param retryBehavior The retry behavior for this exception.
56+ * @deprecated
57+ */
58+ public HandlerException (ErrorType errorType , String causeMessage , RetryBehavior retryBehavior ) {
59+ this (errorType , new RuntimeException (causeMessage ), retryBehavior );
3960 }
4061
41- public HandlerException (ErrorType errorType , String message , RetryBehavior retryBehavior ) {
42- this (errorType , new RuntimeException (message ), retryBehavior );
62+ /**
63+ * Create a handler exception with the given error type, message, and cause.
64+ *
65+ * @param errorType The error type.
66+ * @param message The error message.
67+ * @param cause The cause of this exception.
68+ */
69+ public HandlerException (ErrorType errorType , String message , @ Nullable Throwable cause ) {
70+ this (errorType , message , cause , RetryBehavior .UNSPECIFIED );
4371 }
4472
73+ /**
74+ * Create a handler exception with the given error type and cause.
75+ *
76+ * @param errorType The error type.
77+ * @param cause The cause of this exception.
78+ */
4579 public HandlerException (ErrorType errorType , @ Nullable Throwable cause ) {
4680 this (errorType , cause , RetryBehavior .UNSPECIFIED );
4781 }
4882
83+ /**
84+ * Create a handler exception with the given error type, cause, and retry behavior.
85+ *
86+ * @param errorType The error type.
87+ * @param cause The cause of this exception.
88+ * @param retryBehavior The retry behavior for this exception.
89+ */
4990 public HandlerException (
5091 ErrorType errorType , @ Nullable Throwable cause , RetryBehavior retryBehavior ) {
51- super (cause == null ? "handler error" : "handler error: " + cause .getMessage (), cause );
92+ this (
93+ errorType ,
94+ cause == null ? "handler error" : "handler error: " + cause .getMessage (),
95+ cause ,
96+ retryBehavior );
97+ }
98+
99+ /**
100+ * Create a handler exception with the given error type, message, cause, and retry behavior.
101+ *
102+ * @param errorType The error type.
103+ * @param message The error message.
104+ * @param cause The cause of this exception.
105+ * @param retryBehavior The retry behavior for this exception.
106+ */
107+ public HandlerException (
108+ ErrorType errorType , String message , @ Nullable Throwable cause , RetryBehavior retryBehavior ) {
109+ this (errorType , message , cause , retryBehavior , null );
110+ }
111+
112+ /**
113+ * Create a handler exception with the given error type, message, cause, retry behavior, and
114+ * original failure.
115+ *
116+ * @param errorType The error type.
117+ * @param message The error message.
118+ * @param cause The cause of this exception.
119+ * @param retryBehavior The retry behavior for this exception.
120+ * @param originalFailure The original failure information if available.
121+ */
122+ public HandlerException (
123+ ErrorType errorType ,
124+ String message ,
125+ @ Nullable Throwable cause ,
126+ RetryBehavior retryBehavior ,
127+ FailureInfo originalFailure ) {
128+ super (message , cause );
52129 this .rawErrorType = errorType .name ();
53- this .errorType = errorType ;
130+ this .errorType =
131+ Arrays .stream (ErrorType .values ()).anyMatch ((t ) -> t .name ().equals (rawErrorType ))
132+ ? ErrorType .valueOf (rawErrorType )
133+ : ErrorType .UNKNOWN ;
54134 this .retryBehavior = retryBehavior ;
135+ this .originalFailure = originalFailure ;
55136 }
56137
138+ /**
139+ * Create a handler exception with a raw error type string, cause, and retry behavior.
140+ *
141+ * @param rawErrorType The raw error type string.
142+ * @param cause The cause of this exception.
143+ * @param retryBehavior The retry behavior for this exception.
144+ */
57145 public HandlerException (
58146 String rawErrorType , @ Nullable Throwable cause , RetryBehavior retryBehavior ) {
59- super (cause == null ? "handler error" : "handler error: " + cause .getMessage (), cause );
147+ this (
148+ rawErrorType ,
149+ cause == null ? "handler error" : "handler error: " + cause .getMessage (),
150+ cause ,
151+ retryBehavior );
152+ }
153+
154+ /**
155+ * Create a handler exception with a raw error type string, message, cause, and retry behavior.
156+ *
157+ * @param rawErrorType The raw error type string.
158+ * @param message The error message.
159+ * @param cause The cause of this exception.
160+ * @param retryBehavior The retry behavior for this exception.
161+ */
162+ public HandlerException (
163+ String rawErrorType , String message , @ Nullable Throwable cause , RetryBehavior retryBehavior ) {
164+ this (rawErrorType , message , cause , retryBehavior , null );
165+ }
166+
167+ /**
168+ * Create a handler exception with a raw error type string, message, cause, retry behavior, and
169+ * original failure.
170+ *
171+ * @param rawErrorType The raw error type string.
172+ * @param message The error message.
173+ * @param cause The cause of this exception.
174+ * @param retryBehavior The retry behavior for this exception.
175+ * @param originalFailure The original failure information if available.
176+ */
177+ public HandlerException (
178+ String rawErrorType ,
179+ String message ,
180+ @ Nullable Throwable cause ,
181+ RetryBehavior retryBehavior ,
182+ @ Nullable FailureInfo originalFailure ) {
183+ super (message , cause );
60184 this .rawErrorType = rawErrorType ;
61185 this .errorType =
62186 Arrays .stream (ErrorType .values ()).anyMatch ((t ) -> t .name ().equals (rawErrorType ))
63187 ? ErrorType .valueOf (rawErrorType )
64188 : ErrorType .UNKNOWN ;
65189 this .retryBehavior = retryBehavior ;
190+ this .originalFailure = originalFailure ;
66191 }
67192
68193 /**
@@ -86,6 +211,12 @@ public RetryBehavior getRetryBehavior() {
86211 return retryBehavior ;
87212 }
88213
214+ /** Original FailureInfo if available */
215+ @ Nullable
216+ public FailureInfo getOriginalFailure () {
217+ return originalFailure ;
218+ }
219+
89220 public boolean isRetryable () {
90221 if (retryBehavior != RetryBehavior .UNSPECIFIED ) {
91222 return retryBehavior == RetryBehavior .RETRYABLE ;
0 commit comments