11package io .nexusrpc .handler ;
22
3+ import io .nexusrpc .FailureInfo ;
34import java .util .Arrays ;
45import org .jspecify .annotations .Nullable ;
56
@@ -33,36 +34,162 @@ 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 Use {@link #HandlerException(ErrorType, String, Throwable)} instead. This
45+ * constructor will be removed in a future release.
46+ */
47+ public HandlerException (ErrorType errorType , String causeMessage ) {
48+ this (errorType , new RuntimeException (causeMessage ), RetryBehavior .UNSPECIFIED );
49+ }
50+
51+ /**
52+ * Create a handler exception with the given error type and cause message.
53+ *
54+ * @param errorType The error type.
55+ * @param causeMessage The cause message.
56+ * @param retryBehavior The retry behavior for this exception.
57+ * @deprecated Use {@link #HandlerException(ErrorType, String, Throwable, RetryBehavior)} instead.
58+ * This constructor will be removed in a future release.
59+ */
60+ public HandlerException (ErrorType errorType , String causeMessage , RetryBehavior retryBehavior ) {
61+ this (errorType , new RuntimeException (causeMessage ), retryBehavior );
3962 }
4063
41- public HandlerException (ErrorType errorType , String message , RetryBehavior retryBehavior ) {
42- this (errorType , new RuntimeException (message ), retryBehavior );
64+ /**
65+ * Create a handler exception with the given error type, message, and cause.
66+ *
67+ * @param errorType The error type.
68+ * @param message The error message.
69+ * @param cause The cause of this exception.
70+ */
71+ public HandlerException (ErrorType errorType , String message , @ Nullable Throwable cause ) {
72+ this (errorType , message , cause , RetryBehavior .UNSPECIFIED );
4373 }
4474
75+ /**
76+ * Create a handler exception with the given error type and cause.
77+ *
78+ * @param errorType The error type.
79+ * @param cause The cause of this exception.
80+ */
4581 public HandlerException (ErrorType errorType , @ Nullable Throwable cause ) {
4682 this (errorType , cause , RetryBehavior .UNSPECIFIED );
4783 }
4884
85+ /**
86+ * Create a handler exception with the given error type, cause, and retry behavior.
87+ *
88+ * @param errorType The error type.
89+ * @param cause The cause of this exception.
90+ * @param retryBehavior The retry behavior for this exception.
91+ */
4992 public HandlerException (
5093 ErrorType errorType , @ Nullable Throwable cause , RetryBehavior retryBehavior ) {
51- super (cause == null ? "handler error" : "handler error: " + cause .getMessage (), cause );
94+ this (
95+ errorType ,
96+ cause == null ? "handler error" : "handler error: " + cause .getMessage (),
97+ cause ,
98+ retryBehavior );
99+ }
100+
101+ /**
102+ * Create a handler exception with the given error type, message, cause, and retry behavior.
103+ *
104+ * @param errorType The error type.
105+ * @param message The error message.
106+ * @param cause The cause of this exception.
107+ * @param retryBehavior The retry behavior for this exception.
108+ */
109+ public HandlerException (
110+ ErrorType errorType , String message , @ Nullable Throwable cause , RetryBehavior retryBehavior ) {
111+ this (errorType , message , cause , retryBehavior , null );
112+ }
113+
114+ /**
115+ * Create a handler exception with the given error type, message, cause, retry behavior, and
116+ * original failure.
117+ *
118+ * @param errorType The error type.
119+ * @param message The error message.
120+ * @param cause The cause of this exception.
121+ * @param retryBehavior The retry behavior for this exception.
122+ * @param originalFailure The original failure information if available.
123+ */
124+ public HandlerException (
125+ ErrorType errorType ,
126+ String message ,
127+ @ Nullable Throwable cause ,
128+ RetryBehavior retryBehavior ,
129+ FailureInfo originalFailure ) {
130+ super (message , cause );
52131 this .rawErrorType = errorType .name ();
53- this .errorType = errorType ;
132+ this .errorType =
133+ Arrays .stream (ErrorType .values ()).anyMatch ((t ) -> t .name ().equals (rawErrorType ))
134+ ? ErrorType .valueOf (rawErrorType )
135+ : ErrorType .UNKNOWN ;
54136 this .retryBehavior = retryBehavior ;
137+ this .originalFailure = originalFailure ;
55138 }
56139
140+ /**
141+ * Create a handler exception with a raw error type string, cause, and retry behavior.
142+ *
143+ * @param rawErrorType The raw error type string.
144+ * @param cause The cause of this exception.
145+ * @param retryBehavior The retry behavior for this exception.
146+ */
57147 public HandlerException (
58148 String rawErrorType , @ Nullable Throwable cause , RetryBehavior retryBehavior ) {
59- super (cause == null ? "handler error" : "handler error: " + cause .getMessage (), cause );
149+ this (
150+ rawErrorType ,
151+ cause == null ? "handler error" : "handler error: " + cause .getMessage (),
152+ cause ,
153+ retryBehavior );
154+ }
155+
156+ /**
157+ * Create a handler exception with a raw error type string, message, cause, and retry behavior.
158+ *
159+ * @param rawErrorType The raw error type string.
160+ * @param message The error message.
161+ * @param cause The cause of this exception.
162+ * @param retryBehavior The retry behavior for this exception.
163+ */
164+ public HandlerException (
165+ String rawErrorType , String message , @ Nullable Throwable cause , RetryBehavior retryBehavior ) {
166+ this (rawErrorType , message , cause , retryBehavior , null );
167+ }
168+
169+ /**
170+ * Create a handler exception with a raw error type string, message, cause, retry behavior, and
171+ * original failure.
172+ *
173+ * @param rawErrorType The raw error type string.
174+ * @param message The error message.
175+ * @param cause The cause of this exception.
176+ * @param retryBehavior The retry behavior for this exception.
177+ * @param originalFailure The original failure information if available.
178+ */
179+ public HandlerException (
180+ String rawErrorType ,
181+ String message ,
182+ @ Nullable Throwable cause ,
183+ RetryBehavior retryBehavior ,
184+ @ Nullable FailureInfo originalFailure ) {
185+ super (message , cause );
60186 this .rawErrorType = rawErrorType ;
61187 this .errorType =
62188 Arrays .stream (ErrorType .values ()).anyMatch ((t ) -> t .name ().equals (rawErrorType ))
63189 ? ErrorType .valueOf (rawErrorType )
64190 : ErrorType .UNKNOWN ;
65191 this .retryBehavior = retryBehavior ;
192+ this .originalFailure = originalFailure ;
66193 }
67194
68195 /**
@@ -86,6 +213,12 @@ public RetryBehavior getRetryBehavior() {
86213 return retryBehavior ;
87214 }
88215
216+ /** Original FailureInfo if available */
217+ @ Nullable
218+ public FailureInfo getOriginalFailure () {
219+ return originalFailure ;
220+ }
221+
89222 public boolean isRetryable () {
90223 if (retryBehavior != RetryBehavior .UNSPECIFIED ) {
91224 return retryBehavior == RetryBehavior .RETRYABLE ;
0 commit comments