13
13
14
14
package io .dapr .exceptions ;
15
15
16
+ import com .google .rpc .Status ;
16
17
import io .grpc .StatusRuntimeException ;
18
+ import io .grpc .protobuf .StatusProto ;
17
19
import reactor .core .Exceptions ;
18
20
import reactor .core .publisher .Flux ;
19
21
import reactor .core .publisher .Mono ;
@@ -30,26 +32,32 @@ public class DaprException extends RuntimeException {
30
32
/**
31
33
* Dapr's error code for this exception.
32
34
*/
33
- private String errorCode ;
35
+ private final String errorCode ;
34
36
35
37
/**
36
38
* The status details for the error.
37
39
*/
38
- private DaprErrorDetails errorDetails ;
40
+ private final DaprErrorDetails errorDetails ;
39
41
40
42
/**
41
43
* Optional payload, if the exception came from a response body.
42
44
*/
43
- private byte [] payload ;
45
+ private final byte [] payload ;
46
+
47
+ /**
48
+ * Optional HTTP status code, if error happened for an HTTP call (0 if not set).
49
+ */
50
+ private final int httpStatusCode ;
44
51
45
52
/**
46
53
* New exception from a server-side generated error code and message.
47
54
*
48
55
* @param daprError Server-side error.
49
- * @param payload Payload containing the error.
56
+ * @param payload Optional payload containing the error.
57
+ * @param httpStatusCode Optional http Status Code (0 if not set).
50
58
*/
51
- public DaprException (DaprError daprError , byte [] payload ) {
52
- this (daprError .getErrorCode (), daprError .getMessage (), daprError .getDetails (), payload );
59
+ public DaprException (DaprError daprError , byte [] payload , int httpStatusCode ) {
60
+ this (daprError .getErrorCode (), daprError .getMessage (), daprError .getDetails (), payload , httpStatusCode );
53
61
}
54
62
55
63
/**
@@ -77,10 +85,11 @@ public DaprException(Throwable exception) {
77
85
*
78
86
* @param errorCode Client-side error code.
79
87
* @param message Client-side error message.
80
- * @param payload Error's raw payload.
88
+ * @param payload Optional payload containing the error.
89
+ * @param httpStatusCode Optional http Status Code (0 if not set).
81
90
*/
82
- public DaprException (String errorCode , String message , byte [] payload ) {
83
- this (errorCode , message , DaprErrorDetails .EMPTY_INSTANCE , payload );
91
+ public DaprException (String errorCode , String message , byte [] payload , int httpStatusCode ) {
92
+ this (errorCode , message , DaprErrorDetails .EMPTY_INSTANCE , payload , httpStatusCode );
84
93
}
85
94
86
95
/**
@@ -89,10 +98,12 @@ public DaprException(String errorCode, String message, byte[] payload) {
89
98
* @param errorCode Client-side error code.
90
99
* @param message Client-side error message.
91
100
* @param errorDetails Details of the error from runtime.
92
- * @param payload Payload containing the error.
101
+ * @param payload Optional payload containing the error.
102
+ * @param httpStatusCode Optional http Status Code (0 if not set).
93
103
*/
94
- public DaprException (String errorCode , String message , List <Map <String , Object >> errorDetails , byte [] payload ) {
95
- this (errorCode , message , new DaprErrorDetails (errorDetails ), payload );
104
+ public DaprException (
105
+ String errorCode , String message , List <Map <String , Object >> errorDetails , byte [] payload , int httpStatusCode ) {
106
+ this (errorCode , message , new DaprErrorDetails (errorDetails ), payload , httpStatusCode );
96
107
}
97
108
98
109
/**
@@ -101,10 +112,29 @@ public DaprException(String errorCode, String message, List<Map<String, Object>>
101
112
* @param errorCode Client-side error code.
102
113
* @param message Client-side error message.
103
114
* @param errorDetails Details of the error from runtime.
104
- * @param payload Payload containing the error.
115
+ * @param payload Optional payload containing the error.
105
116
*/
106
117
public DaprException (String errorCode , String message , DaprErrorDetails errorDetails , byte [] payload ) {
107
- super (String .format ("%s: %s" , errorCode , message ));
118
+ this (errorCode , message , errorDetails , payload , 0 );
119
+ }
120
+
121
+ /**
122
+ * New Exception from a client-side generated error code and message.
123
+ *
124
+ * @param errorCode Client-side error code.
125
+ * @param message Client-side error message.
126
+ * @param errorDetails Details of the error from runtime.
127
+ * @param payload Optional payload containing the error.
128
+ * @param httpStatusCode Optional http Status Code (0 if not set).
129
+ */
130
+ public DaprException (
131
+ String errorCode ,
132
+ String message ,
133
+ DaprErrorDetails errorDetails ,
134
+ byte [] payload ,
135
+ int httpStatusCode ) {
136
+ super (buildErrorMessage (errorCode , httpStatusCode , message ));
137
+ this .httpStatusCode = httpStatusCode ;
108
138
this .errorCode = errorCode ;
109
139
this .errorDetails = errorDetails ;
110
140
this .payload = payload ;
@@ -120,8 +150,11 @@ public DaprException(String errorCode, String message, DaprErrorDetails errorDet
120
150
* unknown.)
121
151
*/
122
152
public DaprException (String errorCode , String message , Throwable cause ) {
123
- super (String .format ("%s: %s" , errorCode , emptyIfNull (message )), cause );
153
+ super (buildErrorMessage (errorCode , 0 , message ), cause );
154
+ this .httpStatusCode = 0 ;
124
155
this .errorCode = errorCode ;
156
+ this .errorDetails = DaprErrorDetails .EMPTY_INSTANCE ;
157
+ this .payload = null ;
125
158
}
126
159
127
160
/**
@@ -137,7 +170,8 @@ public DaprException(String errorCode, String message, Throwable cause) {
137
170
*/
138
171
public DaprException (
139
172
String errorCode , String message , Throwable cause , DaprErrorDetails errorDetails , byte [] payload ) {
140
- super (String .format ("%s: %s" , errorCode , emptyIfNull (message )), cause );
173
+ super (buildErrorMessage (errorCode , 0 , message ), cause );
174
+ this .httpStatusCode = 0 ;
141
175
this .errorCode = errorCode ;
142
176
this .errorDetails = errorDetails == null ? DaprErrorDetails .EMPTY_INSTANCE : errorDetails ;
143
177
this .payload = payload ;
@@ -170,6 +204,15 @@ public byte[] getPayload() {
170
204
return this .payload == null ? null : this .payload .clone ();
171
205
}
172
206
207
+ /**
208
+ * Returns the exception's http status code, 0 if not applicable.
209
+ *
210
+ * @return Http status code (0 if not applicable).
211
+ */
212
+ public int getHttpStatusCode () {
213
+ return this .httpStatusCode ;
214
+ }
215
+
173
216
/**
174
217
* Wraps an exception into DaprException (if not already DaprException).
175
218
*
@@ -266,7 +309,7 @@ public static RuntimeException propagate(Throwable exception) {
266
309
while (e != null ) {
267
310
if (e instanceof StatusRuntimeException ) {
268
311
StatusRuntimeException statusRuntimeException = (StatusRuntimeException ) e ;
269
- com . google . rpc . Status status = io . grpc . protobuf . StatusProto .fromThrowable (statusRuntimeException );
312
+ Status status = StatusProto .fromThrowable (statusRuntimeException );
270
313
271
314
DaprErrorDetails errorDetails = new DaprErrorDetails (status );
272
315
@@ -289,11 +332,18 @@ public static RuntimeException propagate(Throwable exception) {
289
332
return new DaprException (exception );
290
333
}
291
334
292
- private static String emptyIfNull (String str ) {
293
- if (str == null ) {
294
- return "" ;
335
+ private static String buildErrorMessage (String errorCode , int httpStatusCode , String message ) {
336
+ String result = ((errorCode == null ) || errorCode .isEmpty ()) ? "UNKNOWN: " : errorCode + ": " ;
337
+ if ((message == null ) || message .isEmpty ()) {
338
+ if (httpStatusCode > 0 ) {
339
+ return result + "HTTP status code: " + httpStatusCode ;
340
+ }
341
+ return result ;
295
342
}
296
343
297
- return str ;
344
+ if (httpStatusCode > 0 ) {
345
+ return result + message + " (HTTP status code: " + httpStatusCode + ")" ;
346
+ }
347
+ return result + message ;
298
348
}
299
349
}
0 commit comments