Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class Client {

}
```
The code uses the `DaprClient` created by the `DaprClientBuilder`. It tries to get a state from state store but provides an unknown state store. It causes Dapr sidecar to return error, which is converted to a `DaprException` to the application. To be compatible with Project Reactor, `DaprException` extends from `RuntimeException` - making it an unchecked exception.
The code uses the `DaprClient` created by the `DaprClientBuilder`. It tries to get a state from state store but provides an unknown state store. It causes Dapr sidecar to return error, which is converted to a `DaprException` to the application. To be compatible with Project Reactor, `DaprException` extends from `RuntimeException` - making it an unchecked exception. Applications might also get `IllegalArgumentException` when invoking methods with invalid input parameters that are validated at the client side.

The Dapr client is also within a try-with-resource block to properly close the client at the end.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import java.io.Closeable;
import java.lang.reflect.Proxy;

import static io.dapr.exceptions.DaprException.throwIllegalArgumentException;

/**
* Builder to generate an ActorProxy instance. Builder can be reused for multiple instances.
*/
Expand Down Expand Up @@ -76,10 +74,10 @@ public ActorProxyBuilder(Class<T> actorTypeClass) {
*/
public ActorProxyBuilder(String actorType, Class<T> actorTypeClass) {
if ((actorType == null) || actorType.isEmpty()) {
throwIllegalArgumentException("ActorType is required.");
throw new IllegalArgumentException("ActorType is required.");
}
if (actorTypeClass == null) {
throwIllegalArgumentException("ActorTypeClass is required.");
throw new IllegalArgumentException("ActorTypeClass is required.");
}

this.useGrpc = Properties.USE_GRPC.get();
Expand All @@ -98,7 +96,7 @@ public ActorProxyBuilder(String actorType, Class<T> actorTypeClass) {
*/
public ActorProxyBuilder<T> withObjectSerializer(DaprObjectSerializer objectSerializer) {
if (objectSerializer == null) {
throwIllegalArgumentException("Serializer is required.");
throw new IllegalArgumentException("Serializer is required.");
}

this.objectSerializer = objectSerializer;
Expand All @@ -113,7 +111,7 @@ public ActorProxyBuilder<T> withObjectSerializer(DaprObjectSerializer objectSeri
*/
public T build(ActorId actorId) {
if (actorId == null) {
throwIllegalArgumentException("Cannot instantiate an Actor without Id.");
throw new IllegalArgumentException("Cannot instantiate an Actor without Id.");
}

ActorProxyImpl proxy = new ActorProxyImpl(
Expand Down Expand Up @@ -169,7 +167,7 @@ private static ManagedChannel buildManagedChannel() {

int port = Properties.GRPC_PORT.get();
if (port <= 0) {
throwIllegalArgumentException("Invalid port.");
throw new IllegalArgumentException("Invalid port.");
}

return ManagedChannelBuilder.forAddress(Properties.SIDECAR_IP.get(), port).usePlaintext().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,33 @@

import io.dapr.actors.ActorId;
import io.dapr.actors.ActorType;
import io.dapr.exceptions.DaprException;
import org.junit.Assert;
import org.junit.Test;

public class ActorProxyBuilderTest {

@Test(expected = DaprException.class)
@Test(expected = IllegalArgumentException.class)
public void buildWithNullActorId() {
new ActorProxyBuilder("test", Object.class)
.build(null);

}

@Test(expected = DaprException.class)
@Test(expected = IllegalArgumentException.class)
public void buildWithEmptyActorType() {
new ActorProxyBuilder("", Object.class)
.build(new ActorId("100"));

}

@Test(expected = DaprException.class)
@Test(expected = IllegalArgumentException.class)
public void buildWithNullActorType() {
new ActorProxyBuilder(null, Object.class)
.build(new ActorId("100"));

}

@Test(expected = DaprException.class)
@Test(expected = IllegalArgumentException.class)
public void buildWithNullSerializer() {
new ActorProxyBuilder("MyActor", Object.class)
.withObjectSerializer(null)
Expand Down
10 changes: 4 additions & 6 deletions sdk/src/main/java/io/dapr/client/DaprClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import java.io.Closeable;

import static io.dapr.exceptions.DaprException.throwIllegalArgumentException;

/**
* A builder for the DaprClient,
* Currently only and HTTP Client will be supported.
Expand Down Expand Up @@ -65,11 +63,11 @@ public DaprClientBuilder() {
*/
public DaprClientBuilder withObjectSerializer(DaprObjectSerializer objectSerializer) {
if (objectSerializer == null) {
throwIllegalArgumentException("Object serializer is required");
throw new IllegalArgumentException("Object serializer is required");
}

if (objectSerializer.getContentType() == null || objectSerializer.getContentType().isEmpty()) {
throwIllegalArgumentException("Content Type should not be null or empty");
throw new IllegalArgumentException("Content Type should not be null or empty");
}

this.objectSerializer = objectSerializer;
Expand All @@ -85,7 +83,7 @@ public DaprClientBuilder withObjectSerializer(DaprObjectSerializer objectSeriali
*/
public DaprClientBuilder withStateSerializer(DaprObjectSerializer stateSerializer) {
if (stateSerializer == null) {
throwIllegalArgumentException("State serializer is required");
throw new IllegalArgumentException("State serializer is required");
}

this.stateSerializer = stateSerializer;
Expand Down Expand Up @@ -115,7 +113,7 @@ public DaprClient build() {
private DaprClient buildDaprClientGrpc() {
int port = Properties.GRPC_PORT.get();
if (port <= 0) {
throwIllegalArgumentException("Invalid port.");
throw new IllegalArgumentException("Invalid port.");
}
ManagedChannel channel = ManagedChannelBuilder.forAddress(
Properties.SIDECAR_IP.get(), port).usePlaintext().build();
Expand Down
6 changes: 1 addition & 5 deletions sdk/src/main/java/io/dapr/client/DaprClientHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,6 @@ public Mono<Response<Map<String, String>>> getSecret(GetSecretRequest request) {

@Override
public void close() {
try {
client.close();
} catch (Exception e) {
DaprException.wrap(e);
}
client.close();
}
}
26 changes: 8 additions & 18 deletions sdk/src/main/java/io/dapr/exceptions/DaprException.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import io.grpc.StatusRuntimeException;
import reactor.core.publisher.Mono;

import java.lang.reflect.Executable;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

/**
* A Dapr's specific exception.
Expand Down Expand Up @@ -85,27 +83,14 @@ public String getErrorCode() {
return this.errorCode;
}

/**
* Convenience to throw an wrapped IllegalArgumentException.
* @param message Message for exception.
*/
public static void throwIllegalArgumentException(String message) {
try {
throw new IllegalArgumentException(message);
} catch (Exception e) {
wrap(e);
}
}

/**
* Wraps an exception into DaprException (if not already DaprException).
*
* @param exception Exception to be wrapped.
* @return DaprException.
*/
public static DaprException wrap(Exception exception) {
public static void wrap(Exception exception) {
if (exception == null) {
return null;
return;
}

if (exception instanceof DaprException) {
Expand All @@ -125,6 +110,10 @@ public static DaprException wrap(Exception exception) {
e = e.getCause();
}

if (exception instanceof IllegalArgumentException) {
throw (IllegalArgumentException) exception;
}

throw new DaprException(exception);
}

Expand All @@ -139,7 +128,8 @@ public static <T> Callable<T> wrap(Callable<T> callable) {
try {
return callable.call();
} catch (Exception e) {
return (T) wrap(e);
wrap(e);
return null;
}
};
}
Expand Down
7 changes: 3 additions & 4 deletions sdk/src/test/java/io/dapr/client/DaprClientBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package io.dapr.client;

import io.dapr.exceptions.DaprException;
import io.dapr.serializer.DaprObjectSerializer;
import org.junit.Test;

Expand All @@ -27,17 +26,17 @@ public void build() {
assertNotNull(daprClient);
}

@Test(expected = DaprException.class)
@Test(expected = IllegalArgumentException.class)
public void noObjectSerializer() {
new DaprClientBuilder().withObjectSerializer(null);
}

@Test(expected = DaprException.class)
@Test(expected = IllegalArgumentException.class)
public void blankContentTypeInObjectSerializer() {
new DaprClientBuilder().withObjectSerializer(mock(DaprObjectSerializer.class));
}

@Test(expected = DaprException.class)
@Test(expected = IllegalArgumentException.class)
public void noStateSerializer() {
new DaprClientBuilder().withStateSerializer(null);
}
Expand Down
Loading