Skip to content

Commit 363b6f9

Browse files
artursouzacicoyle
andauthored
[cherry-pick] Update Dapr CLI + Add HTTP code to DaprException (#1020)
* Update to 1.13 CLi rc. (#1018) Signed-off-by: Artur Souza <[email protected]> * Add support for HTTP status code in exception. (#1019) Signed-off-by: Artur Souza <[email protected]> --------- Signed-off-by: Artur Souza <[email protected]> Co-authored-by: Cassie Coyle <[email protected]>
1 parent 653ba7e commit 363b6f9

File tree

6 files changed

+85
-37
lines changed

6 files changed

+85
-37
lines changed

.github/workflows/build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ jobs:
4646
GOARCH: amd64
4747
GOPROXY: https://proxy.golang.org
4848
JDK_VER: ${{ matrix.java }}
49-
DAPR_CLI_VER: 1.12.0
49+
DAPR_CLI_VER: 1.13.0-rc.1
5050
DAPR_RUNTIME_VER: 1.13.0-rc.2
51-
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.12.0/install/install.sh
51+
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.13.0-rc.1/install/install.sh
5252
DAPR_CLI_REF:
5353
DAPR_REF:
5454
TOXIPROXY_URL: https://github.com/Shopify/toxiproxy/releases/download/v2.5.0/toxiproxy-server-linux-amd64

.github/workflows/validate.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ jobs:
3737
GOARCH: amd64
3838
GOPROXY: https://proxy.golang.org
3939
JDK_VER: ${{ matrix.java }}
40-
DAPR_CLI_VER: 1.12.0
40+
DAPR_CLI_VER: 1.13.0-rc.1
4141
DAPR_RUNTIME_VER: 1.13.0-rc.5
42-
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.12.0/install/install.sh
42+
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.13.0-rc.1/install/install.sh
4343
DAPR_CLI_REF:
4444
DAPR_REF:
4545
steps:

sdk-tests/src/test/java/io/dapr/it/methodinvoke/http/MethodInvokeIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void testInvokeException() throws Exception {
134134
// TODO(artursouza): change this to INTERNAL once runtime is fixed.
135135
assertEquals("UNKNOWN", exception.getErrorCode());
136136
assertNotNull(exception.getMessage());
137-
assertTrue(exception.getMessage().contains("Internal Server Error"));
137+
assertTrue(exception.getMessage().contains("HTTP status code: 500"));
138138
assertTrue(new String(exception.getPayload()).contains("Internal Server Error"));
139139
}
140140
}

sdk/src/main/java/io/dapr/client/DaprHttp.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ private static DaprError parseDaprError(byte[] json) {
349349
try {
350350
return DAPR_ERROR_DETAILS_OBJECT_MAPPER.readValue(json, DaprError.class);
351351
} catch (IOException e) {
352-
throw new DaprException("UNKNOWN", new String(json, StandardCharsets.UTF_8), json);
352+
// Could not parse DaprError. Return null.
353+
return null;
353354
}
354355
}
355356

@@ -384,17 +385,13 @@ public void onResponse(@NotNull Call call, @NotNull okhttp3.Response response) t
384385
try {
385386
byte[] payload = getBodyBytesOrEmptyArray(response);
386387
DaprError error = parseDaprError(payload);
387-
if ((error != null) && (error.getErrorCode() != null)) {
388-
if (error.getMessage() != null) {
389-
future.completeExceptionally(new DaprException(error, payload));
390-
} else {
391-
future.completeExceptionally(
392-
new DaprException(error.getErrorCode(), "HTTP status code: " + response.code(), payload));
393-
}
388+
if (error != null) {
389+
future.completeExceptionally(new DaprException(error, payload, response.code()));
394390
return;
395391
}
396392

397-
future.completeExceptionally(new DaprException("UNKNOWN", "HTTP status code: " + response.code(), payload));
393+
future.completeExceptionally(
394+
new DaprException("UNKNOWN", "", payload, response.code()));
398395
return;
399396
} catch (DaprException e) {
400397
future.completeExceptionally(e);

sdk/src/main/java/io/dapr/exceptions/DaprException.java

+71-21
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
package io.dapr.exceptions;
1515

16+
import com.google.rpc.Status;
1617
import io.grpc.StatusRuntimeException;
18+
import io.grpc.protobuf.StatusProto;
1719
import reactor.core.Exceptions;
1820
import reactor.core.publisher.Flux;
1921
import 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
}

sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ public void invokeServiceDaprError() {
274274
});
275275

276276
assertEquals("MYCODE", exception.getErrorCode());
277-
assertEquals("MYCODE: My Message", exception.getMessage());
277+
assertEquals("MYCODE: My Message (HTTP status code: 500)", exception.getMessage());
278+
assertEquals(500, exception.getHttpStatusCode());
278279
}
279280

280281
@Test
@@ -308,7 +309,7 @@ public void invokeServiceDaprErrorUnknownJSON() {
308309
});
309310

310311
assertEquals("UNKNOWN", exception.getErrorCode());
311-
assertEquals("UNKNOWN: { \"anything\": 7 }", exception.getMessage());
312+
assertEquals("UNKNOWN: HTTP status code: 500", exception.getMessage());
312313
assertEquals("{ \"anything\": 7 }", new String(exception.getPayload()));
313314
}
314315

0 commit comments

Comments
 (0)