1313
1414package io .dapr .exceptions ;
1515
16+ import com .google .rpc .Status ;
1617import io .grpc .StatusRuntimeException ;
18+ import io .grpc .protobuf .StatusProto ;
1719import reactor .core .Exceptions ;
1820import reactor .core .publisher .Flux ;
1921import reactor .core .publisher .Mono ;
@@ -30,26 +32,32 @@ public class DaprException extends RuntimeException {
3032 /**
3133 * Dapr's error code for this exception.
3234 */
33- private String errorCode ;
35+ private final String errorCode ;
3436
3537 /**
3638 * The status details for the error.
3739 */
38- private DaprErrorDetails errorDetails ;
40+ private final DaprErrorDetails errorDetails ;
3941
4042 /**
4143 * Optional payload, if the exception came from a response body.
4244 */
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 ;
4451
4552 /**
4653 * New exception from a server-side generated error code and message.
4754 *
4855 * @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).
5058 */
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 );
5361 }
5462
5563 /**
@@ -77,10 +85,11 @@ public DaprException(Throwable exception) {
7785 *
7886 * @param errorCode Client-side error code.
7987 * @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).
8190 */
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 );
8493 }
8594
8695 /**
@@ -89,10 +98,12 @@ public DaprException(String errorCode, String message, byte[] payload) {
8998 * @param errorCode Client-side error code.
9099 * @param message Client-side error message.
91100 * @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).
93103 */
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 );
96107 }
97108
98109 /**
@@ -101,10 +112,29 @@ public DaprException(String errorCode, String message, List<Map<String, Object>>
101112 * @param errorCode Client-side error code.
102113 * @param message Client-side error message.
103114 * @param errorDetails Details of the error from runtime.
104- * @param payload Payload containing the error.
115+ * @param payload Optional payload containing the error.
105116 */
106117 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 ;
108138 this .errorCode = errorCode ;
109139 this .errorDetails = errorDetails ;
110140 this .payload = payload ;
@@ -120,8 +150,11 @@ public DaprException(String errorCode, String message, DaprErrorDetails errorDet
120150 * unknown.)
121151 */
122152 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 ;
124155 this .errorCode = errorCode ;
156+ this .errorDetails = DaprErrorDetails .EMPTY_INSTANCE ;
157+ this .payload = null ;
125158 }
126159
127160 /**
@@ -137,7 +170,8 @@ public DaprException(String errorCode, String message, Throwable cause) {
137170 */
138171 public DaprException (
139172 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 ;
141175 this .errorCode = errorCode ;
142176 this .errorDetails = errorDetails == null ? DaprErrorDetails .EMPTY_INSTANCE : errorDetails ;
143177 this .payload = payload ;
@@ -170,6 +204,15 @@ public byte[] getPayload() {
170204 return this .payload == null ? null : this .payload .clone ();
171205 }
172206
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+
173216 /**
174217 * Wraps an exception into DaprException (if not already DaprException).
175218 *
@@ -266,7 +309,7 @@ public static RuntimeException propagate(Throwable exception) {
266309 while (e != null ) {
267310 if (e instanceof StatusRuntimeException ) {
268311 StatusRuntimeException statusRuntimeException = (StatusRuntimeException ) e ;
269- com . google . rpc . Status status = io . grpc . protobuf . StatusProto .fromThrowable (statusRuntimeException );
312+ Status status = StatusProto .fromThrowable (statusRuntimeException );
270313
271314 DaprErrorDetails errorDetails = new DaprErrorDetails (status );
272315
@@ -289,11 +332,18 @@ public static RuntimeException propagate(Throwable exception) {
289332 return new DaprException (exception );
290333 }
291334
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 ;
295342 }
296343
297- return str ;
344+ if (httpStatusCode > 0 ) {
345+ return result + message + " (HTTP status code: " + httpStatusCode + ")" ;
346+ }
347+ return result + message ;
298348 }
299349}
0 commit comments