• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #257

29 May 2024 08:30PM UTC coverage: 77.442% (-0.02%) from 77.463%
#257

push

github

web-flow
Add support for nextRetryDelay (#2081)

Add support for nextRetryDelay

38 of 43 new or added lines in 6 files covered. (88.37%)

11 existing lines in 5 files now uncovered.

19204 of 24798 relevant lines covered (77.44%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.3
/temporal-sdk/src/main/java/io/temporal/failure/ApplicationFailure.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.failure;
22

23
import com.google.common.base.Strings;
24
import io.temporal.common.converter.DataConverter;
25
import io.temporal.common.converter.EncodedValues;
26
import io.temporal.common.converter.Values;
27
import java.time.Duration;
28
import java.util.Objects;
29
import javax.annotation.Nullable;
30

31
/**
32
 * Application failure is used to communicate application specific failures between workflows and
33
 * activities.
34
 *
35
 * <p>Throw this exception to have full control over type and details if the exception delivered to
36
 * the caller workflow or client.
37
 *
38
 * <p>Any unhandled exception which doesn't extend {@link TemporalFailure} is converted to an
39
 * instance of this class before being returned to a caller.
40
 *
41
 * <p>The {@code type} property is used by {@link io.temporal.common.RetryOptions} to determine if
42
 * an instance of this exception is non retryable. Another way to avoid retrying an exception of
43
 * this type is by setting {@code nonRetryable} flag to @{code true}.
44
 *
45
 * <p>The conversion of an exception that doesn't extend {@link TemporalFailure} to an
46
 * ApplicationFailure is done as following:
47
 *
48
 * <ul>
49
 *   <li>type is set to the exception full type name.
50
 *   <li>message is set to the exception message
51
 *   <li>nonRetryable is set to false
52
 *   <li>details are set to null
53
 *   <li>stack trace is copied from the original exception
54
 * </ul>
55
 */
56
public final class ApplicationFailure extends TemporalFailure {
57
  private final String type;
58
  private final Values details;
59
  private boolean nonRetryable;
60
  private Duration nextRetryDelay;
61

62
  /**
63
   * New ApplicationFailure with {@link #isNonRetryable()} flag set to false.
64
   *
65
   * <p>Note that this exception still may not be retried by the service if its type is included in
66
   * the doNotRetry property of the correspondent retry policy.
67
   *
68
   * @param message optional error message
69
   * @param type optional error type that is used by {@link
70
   *     io.temporal.common.RetryOptions.Builder#setDoNotRetry(String...)}.
71
   * @param details optional details about the failure. They are serialized using the same approach
72
   *     as arguments and results.
73
   */
74
  public static ApplicationFailure newFailure(String message, String type, Object... details) {
75
    return newFailureWithCause(message, type, null, details);
1✔
76
  }
77

78
  /**
79
   * New ApplicationFailure with {@link #isNonRetryable()} flag set to false.
80
   *
81
   * <p>Note that this exception still may not be retried by the service if its type is included in
82
   * the doNotRetry property of the correspondent retry policy.
83
   *
84
   * @param message optional error message
85
   * @param type optional error type that is used by {@link
86
   *     io.temporal.common.RetryOptions.Builder#setDoNotRetry(String...)}.
87
   * @param details optional details about the failure. They are serialized using the same approach
88
   *     as arguments and results.
89
   * @param cause failure cause. Each element of the cause chain will be converted to
90
   *     ApplicationFailure for network transmission across network if it doesn't extend {@link
91
   *     TemporalFailure}
92
   */
93
  public static ApplicationFailure newFailureWithCause(
94
      String message, String type, @Nullable Throwable cause, Object... details) {
95
    return new ApplicationFailure(message, type, false, new EncodedValues(details), cause, null);
1✔
96
  }
97

98
  /**
99
   * New ApplicationFailure with {@link #isNonRetryable()} flag set to false.
100
   *
101
   * <p>Note that this exception still may not be retried by the service if its type is included in
102
   * the doNotRetry property of the correspondent retry policy.
103
   *
104
   * @param message optional error message
105
   * @param type optional error type that is used by {@link
106
   *     io.temporal.common.RetryOptions.Builder#setDoNotRetry(String...)}.
107
   * @param details optional details about the failure. They are serialized using the same approach
108
   *     as arguments and results.
109
   * @param cause failure cause. Each element of the cause chain will be converted to
110
   *     ApplicationFailure for network transmission across network if it doesn't extend {@link
111
   *     TemporalFailure}
112
   * @param nextRetryDelay delay before the next retry attempt.
113
   */
114
  public static ApplicationFailure newFailureWithCauseAndDelay(
115
      String message,
116
      String type,
117
      @Nullable Throwable cause,
118
      Duration nextRetryDelay,
119
      Object... details) {
120
    return new ApplicationFailure(
1✔
121
        message, type, false, new EncodedValues(details), cause, nextRetryDelay);
122
  }
123

124
  /**
125
   * New ApplicationFailure with {@link #isNonRetryable()} flag set to true.
126
   *
127
   * <p>It means that this exception is not going to be retried even if it is not included into
128
   * retry policy doNotRetry list.
129
   *
130
   * @param message optional error message
131
   * @param type error type
132
   * @param details optional details about the failure. They are serialized using the same approach
133
   *     as arguments and results.
134
   */
135
  public static ApplicationFailure newNonRetryableFailure(
136
      String message, String type, Object... details) {
137
    return newNonRetryableFailureWithCause(message, type, null, details);
1✔
138
  }
139

140
  /**
141
   * New ApplicationFailure with {@link #isNonRetryable()} flag set to true.
142
   *
143
   * <p>This exception will not be retried even if it is absent from the retry policy doNotRetry
144
   * list.
145
   *
146
   * @param message optional error message
147
   * @param type error type
148
   * @param details optional details about the failure. They are serialized using the same approach
149
   *     as arguments and results.
150
   * @param cause failure cause. Each element of the cause chain will be converted to
151
   *     ApplicationFailure for network transmission across network if it doesn't extend {@link
152
   *     TemporalFailure}
153
   */
154
  public static ApplicationFailure newNonRetryableFailureWithCause(
155
      String message, String type, @Nullable Throwable cause, Object... details) {
156
    return new ApplicationFailure(message, type, true, new EncodedValues(details), cause, null);
1✔
157
  }
158

159
  static ApplicationFailure newFromValues(
160
      String message,
161
      String type,
162
      boolean nonRetryable,
163
      Values details,
164
      Throwable cause,
165
      Duration nextRetryDelay) {
166
    return new ApplicationFailure(message, type, nonRetryable, details, cause, nextRetryDelay);
1✔
167
  }
168

169
  ApplicationFailure(
170
      String message,
171
      String type,
172
      boolean nonRetryable,
173
      Values details,
174
      Throwable cause,
175
      Duration nextRetryDelay) {
176
    super(getMessage(message, Objects.requireNonNull(type), nonRetryable), message, cause);
1✔
177
    this.type = type;
1✔
178
    this.details = details;
1✔
179
    this.nonRetryable = nonRetryable;
1✔
180
    this.nextRetryDelay = nextRetryDelay;
1✔
181
  }
1✔
182

183
  public String getType() {
184
    return type;
1✔
185
  }
186

187
  public Values getDetails() {
188
    return details;
1✔
189
  }
190

191
  @Nullable
192
  public Duration getNextRetryDelay() {
193
    return nextRetryDelay;
1✔
194
  }
195

196
  public void setNonRetryable(boolean nonRetryable) {
197
    this.nonRetryable = nonRetryable;
1✔
198
  }
1✔
199

200
  public boolean isNonRetryable() {
201
    return nonRetryable;
1✔
202
  }
203

204
  @Override
205
  public void setDataConverter(DataConverter converter) {
206
    ((EncodedValues) details).setDataConverter(converter);
1✔
207
  }
1✔
208

209
  public void setNextRetryDelay(Duration nextRetryDelay) {
NEW
210
    this.nextRetryDelay = nextRetryDelay;
×
NEW
211
  }
×
212

213
  private static String getMessage(String message, String type, boolean nonRetryable) {
214
    return (Strings.isNullOrEmpty(message) ? "" : "message='" + message + "', ")
1✔
215
        + "type='"
216
        + type
217
        + '\''
218
        + ", nonRetryable="
219
        + nonRetryable;
220
  }
221
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc