51
51
* <li>nonRetryable is set to false
52
52
* <li>details are set to null
53
53
* <li>stack trace is copied from the original exception
54
- * <li>category is set to ApplicationErrorCategory.APPLICATION_ERROR_CATEGORY_UNSPECIFIED
54
+ * <li>category is set to {@link ApplicationErrorCategory#UNSPECIFIED}
55
55
* </ul>
56
56
*/
57
57
public final class ApplicationFailure extends TemporalFailure {
@@ -61,6 +61,16 @@ public final class ApplicationFailure extends TemporalFailure {
61
61
private Duration nextRetryDelay ;
62
62
private final ApplicationErrorCategory category ;
63
63
64
+ /** Creates a new builder for {@link ApplicationFailure}. */
65
+ public static ApplicationFailure .Builder newBuilder () {
66
+ return new ApplicationFailure .Builder ();
67
+ }
68
+
69
+ /** Creates a new builder for {@link ApplicationFailure} initialized with the provided failure. */
70
+ public static ApplicationFailure .Builder newBuilder (ApplicationFailure options ) {
71
+ return new ApplicationFailure .Builder (options );
72
+ }
73
+
64
74
/**
65
75
* New ApplicationFailure with {@link #isNonRetryable()} flag set to false.
66
76
*
@@ -178,45 +188,7 @@ public static ApplicationFailure newNonRetryableFailureWithCause(
178
188
ApplicationErrorCategory .UNSPECIFIED );
179
189
}
180
190
181
- /**
182
- * New ApplicationFailure with a specified category and {@link #isNonRetryable()} flag set to
183
- * false.
184
- *
185
- * <p>Note that this exception still may not be retried by the service if its type is included in
186
- * the doNotRetry property of the correspondent retry policy.
187
- *
188
- * @param message optional error message
189
- * @param type error type
190
- * @param category the category of the application failure.
191
- * @param cause failure cause. Each element of the cause chain will be converted to
192
- * ApplicationFailure for network transmission across network if it doesn't extend {@link
193
- * TemporalFailure}
194
- * @param details optional details about the failure. They are serialized using the same approach
195
- * as arguments and results.
196
- */
197
- public static ApplicationFailure newFailureWithCategory (
198
- String message ,
199
- String type ,
200
- ApplicationErrorCategory category ,
201
- @ Nullable Throwable cause ,
202
- Object ... details ) {
203
- return new ApplicationFailure (
204
- message , type , false , new EncodedValues (details ), cause , null , category );
205
- }
206
-
207
- static ApplicationFailure newFromValues (
208
- String message ,
209
- String type ,
210
- boolean nonRetryable ,
211
- Values details ,
212
- Throwable cause ,
213
- Duration nextRetryDelay ,
214
- ApplicationErrorCategory category ) {
215
- return new ApplicationFailure (
216
- message , type , nonRetryable , details , cause , nextRetryDelay , category );
217
- }
218
-
219
- ApplicationFailure (
191
+ private ApplicationFailure (
220
192
String message ,
221
193
String type ,
222
194
boolean nonRetryable ,
@@ -262,7 +234,7 @@ public void setNextRetryDelay(Duration nextRetryDelay) {
262
234
this .nextRetryDelay = nextRetryDelay ;
263
235
}
264
236
265
- public ApplicationErrorCategory getApplicationErrorCategory () {
237
+ public ApplicationErrorCategory getCategory () {
266
238
return category ;
267
239
}
268
240
@@ -274,4 +246,122 @@ private static String getMessage(String message, String type, boolean nonRetryab
274
246
+ ", nonRetryable="
275
247
+ nonRetryable ;
276
248
}
249
+
250
+ public static final class Builder {
251
+ private String message ;
252
+ private String type ;
253
+ private Values details ;
254
+ private boolean nonRetryable ;
255
+ private Throwable cause ;
256
+ private Duration nextRetryDelay ;
257
+ private ApplicationErrorCategory category ;
258
+
259
+ private Builder () {}
260
+
261
+ private Builder (ApplicationFailure options ) {
262
+ if (options == null ) {
263
+ return ;
264
+ }
265
+ this .message = options .getOriginalMessage ();
266
+ this .type = options .type ;
267
+ this .details = options .details ;
268
+ this .nonRetryable = options .nonRetryable ;
269
+ this .nextRetryDelay = options .nextRetryDelay ;
270
+ this .category = options .category ;
271
+ }
272
+
273
+ /**
274
+ * Sets the error type of this failure. This is used by {@link
275
+ * io.temporal.common.RetryOptions.Builder#setDoNotRetry(String...)} to determine if the
276
+ * exception is non retryable.
277
+ */
278
+ public Builder setType (String type ) {
279
+ this .type = type ;
280
+ return this ;
281
+ }
282
+
283
+ /**
284
+ * Set the optional error message.
285
+ *
286
+ * <p>Default is "".
287
+ */
288
+ public Builder setMessage (String message ) {
289
+ this .message = message ;
290
+ return this ;
291
+ }
292
+
293
+ /**
294
+ * Set the optional details of the failure.
295
+ *
296
+ * <p>Details are serialized using the same approach as arguments and results.
297
+ */
298
+ public Builder setDetails (Object ... details ) {
299
+ this .details = new EncodedValues (details );
300
+ return this ;
301
+ }
302
+
303
+ /**
304
+ * Set the optional details of the failure.
305
+ *
306
+ * <p>Details are serialized using the same approach as arguments and results.
307
+ */
308
+ public Builder setDetails (Values details ) {
309
+ this .details = details ;
310
+ return this ;
311
+ }
312
+
313
+ /**
314
+ * Set the non retryable flag on the failure.
315
+ *
316
+ * <p>It means that this exception is not going to be retried even if it is not included into
317
+ * retry policy doNotRetry list.
318
+ *
319
+ * <p>Default is false.
320
+ */
321
+ public Builder setNonRetryable (boolean nonRetryable ) {
322
+ this .nonRetryable = nonRetryable ;
323
+ return this ;
324
+ }
325
+
326
+ /**
327
+ * Set the optional cause of the failure. Each element of the cause chain will be converted to
328
+ * {@link ApplicationFailure} for network transmission across network if it doesn't extend
329
+ * {@link TemporalFailure}.
330
+ */
331
+ public Builder setCause (Throwable cause ) {
332
+ this .cause = cause ;
333
+ return this ;
334
+ }
335
+
336
+ /**
337
+ * Set the optional delay before the next retry attempt. Overrides the normal retry delay.
338
+ *
339
+ * <p>Default is null.
340
+ */
341
+ public Builder setNextRetryDelay (Duration nextRetryDelay ) {
342
+ this .nextRetryDelay = nextRetryDelay ;
343
+ return this ;
344
+ }
345
+
346
+ /**
347
+ * Set the optional category of the failure.
348
+ *
349
+ * <p>Default is {@link ApplicationErrorCategory#UNSPECIFIED}.
350
+ */
351
+ public Builder setCategory (ApplicationErrorCategory category ) {
352
+ this .category = category ;
353
+ return this ;
354
+ }
355
+
356
+ public ApplicationFailure build () {
357
+ return new ApplicationFailure (
358
+ message ,
359
+ type ,
360
+ nonRetryable ,
361
+ details == null ? new EncodedValues (null ) : details ,
362
+ cause ,
363
+ nextRetryDelay ,
364
+ category );
365
+ }
366
+ }
277
367
}
0 commit comments