Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit aa1ea57

Browse files
authored
Proper exception throwing withing CompletableFuture methods. (#916)
1 parent 41b3ff7 commit aa1ea57

30 files changed

Lines changed: 560 additions & 417 deletions

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/ActivityHandler.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.microsoft.bot.builder;
55

6+
import com.microsoft.bot.connector.Async;
67
import java.net.HttpURLConnection;
78
import java.util.List;
89
import java.util.concurrent.CompletableFuture;
@@ -53,17 +54,21 @@ public class ActivityHandler implements Bot {
5354
@Override
5455
public CompletableFuture<Void> onTurn(TurnContext turnContext) {
5556
if (turnContext == null) {
56-
throw new IllegalArgumentException("TurnContext cannot be null.");
57+
return Async.completeExceptionally(new IllegalArgumentException(
58+
"TurnContext cannot be null."
59+
));
5760
}
5861

5962
if (turnContext.getActivity() == null) {
60-
throw new IllegalArgumentException("turnContext must have a non-null Activity.");
63+
return Async.completeExceptionally(new IllegalArgumentException(
64+
"turnContext must have a non-null Activity."
65+
));
6166
}
6267

6368
if (turnContext.getActivity().getType() == null) {
64-
throw new IllegalArgumentException(
69+
return Async.completeExceptionally(new IllegalArgumentException(
6570
"turnContext.getActivity must have a non-null Type."
66-
);
71+
));
6772
}
6873

6974
switch (turnContext.getActivity().getType()) {

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotAdapter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.microsoft.bot.builder;
55

6+
import com.microsoft.bot.connector.Async;
67
import com.microsoft.bot.connector.authentication.ClaimsIdentity;
78
import com.microsoft.bot.schema.Activity;
89
import com.microsoft.bot.schema.ConversationReference;
@@ -187,7 +188,9 @@ protected CompletableFuture<Void> runPipeline(
187188
TurnContext context,
188189
BotCallbackHandler callback
189190
) {
190-
BotAssert.contextNotNull(context);
191+
if (context == null) {
192+
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
193+
}
191194

192195
// Call any registered Middleware Components looking for ReceiveActivity()
193196
if (context.getActivity() != null) {

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
99
import com.fasterxml.jackson.databind.node.ObjectNode;
1010
import com.microsoft.bot.builder.integration.AdapterIntegration;
11+
import com.microsoft.bot.connector.Async;
1112
import com.microsoft.bot.connector.Channels;
1213
import com.microsoft.bot.connector.ConnectorClient;
1314
import com.microsoft.bot.connector.Conversations;
@@ -154,7 +155,6 @@ public BotFrameworkAdapter(
154155
RetryStrategy withRetryStrategy,
155156
Middleware withMiddleware
156157
) {
157-
158158
this(
159159
withCredentialProvider,
160160
new AuthenticationConfiguration(),
@@ -181,7 +181,6 @@ public BotFrameworkAdapter(
181181
RetryStrategy withRetryStrategy,
182182
Middleware withMiddleware
183183
) {
184-
185184
if (withCredentialProvider == null) {
186185
throw new IllegalArgumentException("CredentialProvider cannot be null");
187186
}
@@ -226,7 +225,6 @@ public BotFrameworkAdapter(
226225
RetryStrategy withRetryStrategy,
227226
Middleware withMiddleware
228227
) {
229-
230228
if (withCredentials == null) {
231229
throw new IllegalArgumentException("credentials");
232230
}
@@ -287,11 +285,11 @@ public CompletableFuture<Void> continueConversation(
287285
BotCallbackHandler callback
288286
) {
289287
if (reference == null) {
290-
throw new IllegalArgumentException("reference");
288+
return Async.completeExceptionally(new IllegalArgumentException("reference"));
291289
}
292290

293291
if (callback == null) {
294-
throw new IllegalArgumentException("callback");
292+
return Async.completeExceptionally(new IllegalArgumentException("callback"));
295293
}
296294

297295
botAppId = botAppId == null ? "" : botAppId;
@@ -303,7 +301,6 @@ public CompletableFuture<Void> continueConversation(
303301
claims.put(AuthenticationConstants.APPID_CLAIM, botAppId);
304302

305303
ClaimsIdentity claimsIdentity = new ClaimsIdentity("ExternalBearer", claims);
306-
307304
String audience = getBotFrameworkOAuthScope();
308305

309306
return continueConversation(claimsIdentity, reference, audience, callback);
@@ -356,19 +353,21 @@ public CompletableFuture<Void> continueConversation(
356353
BotCallbackHandler callback
357354
) {
358355
if (claimsIdentity == null) {
359-
throw new IllegalArgumentException("claimsIdentity");
356+
return Async.completeExceptionally(new IllegalArgumentException("claimsIdentity"));
360357
}
361358

362359
if (reference == null) {
363-
throw new IllegalArgumentException("reference");
360+
return Async.completeExceptionally(new IllegalArgumentException("reference"));
364361
}
365362

366363
if (callback == null) {
367-
throw new IllegalArgumentException("callback");
364+
return Async.completeExceptionally(new IllegalArgumentException("callback"));
368365
}
369366

370367
if (StringUtils.isEmpty(audience)) {
371-
throw new IllegalArgumentException("audience cannot be null or empty");
368+
return Async.completeExceptionally(new IllegalArgumentException(
369+
"audience cannot be null or empty"
370+
));
372371
}
373372

374373
CompletableFuture<Void> pipelineResult = new CompletableFuture<>();
@@ -434,7 +433,9 @@ public CompletableFuture<InvokeResponse> processActivity(
434433
Activity activity,
435434
BotCallbackHandler callback
436435
) {
437-
BotAssert.activityNotNull(activity);
436+
if (activity == null) {
437+
return Async.completeExceptionally(new IllegalArgumentException("Activity"));
438+
}
438439

439440
return JwtTokenValidation.authenticateRequest(
440441
activity, authHeader, credentialProvider, channelProvider, authConfiguration
@@ -460,7 +461,9 @@ public CompletableFuture<InvokeResponse> processActivity(
460461
Activity activity,
461462
BotCallbackHandler callback
462463
) {
463-
BotAssert.activityNotNull(activity);
464+
if (activity == null) {
465+
return Async.completeExceptionally(new IllegalArgumentException("Activity"));
466+
}
464467

465468
CompletableFuture<InvokeResponse> pipelineResult = new CompletableFuture<>();
466469

@@ -552,17 +555,17 @@ public CompletableFuture<ResourceResponse[]> sendActivities(
552555
List<Activity> activities
553556
) {
554557
if (context == null) {
555-
throw new IllegalArgumentException("context");
558+
return Async.completeExceptionally(new IllegalArgumentException("context"));
556559
}
557560

558561
if (activities == null) {
559-
throw new IllegalArgumentException("activities");
562+
return Async.completeExceptionally(new IllegalArgumentException("activities"));
560563
}
561564

562565
if (activities.size() == 0) {
563-
throw new IllegalArgumentException(
566+
return Async.completeExceptionally(new IllegalArgumentException(
564567
"Expecting one or more activities, but the array was empty."
565-
);
568+
));
566569
}
567570

568571
return CompletableFuture.supplyAsync(() -> {
@@ -690,15 +693,15 @@ public CompletableFuture<Void> deleteConversationMember(
690693
String memberId
691694
) {
692695
if (context.getActivity().getConversation() == null) {
693-
throw new IllegalArgumentException(
696+
return Async.completeExceptionally(new IllegalArgumentException(
694697
"BotFrameworkAdapter.deleteConversationMember(): missing conversation"
695-
);
698+
));
696699
}
697700

698701
if (StringUtils.isEmpty(context.getActivity().getConversation().getId())) {
699-
throw new IllegalArgumentException(
702+
return Async.completeExceptionally(new IllegalArgumentException(
700703
"BotFrameworkAdapter.deleteConversationMember(): missing conversation.id"
701-
);
704+
));
702705
}
703706

704707
ConnectorClient connectorClient = context.getTurnState().get(CONNECTOR_CLIENT_KEY);
@@ -735,15 +738,15 @@ public CompletableFuture<List<ChannelAccount>> getActivityMembers(
735738
}
736739

737740
if (context.getActivity().getConversation() == null) {
738-
throw new IllegalArgumentException(
741+
return Async.completeExceptionally(new IllegalArgumentException(
739742
"BotFrameworkAdapter.GetActivityMembers(): missing conversation"
740-
);
743+
));
741744
}
742745

743746
if (StringUtils.isEmpty(context.getActivity().getConversation().getId())) {
744-
throw new IllegalArgumentException(
747+
return Async.completeExceptionally(new IllegalArgumentException(
745748
"BotFrameworkAdapter.GetActivityMembers(): missing conversation.id"
746-
);
749+
));
747750
}
748751

749752
ConnectorClient connectorClient = context.getTurnState().get(CONNECTOR_CLIENT_KEY);
@@ -760,15 +763,15 @@ public CompletableFuture<List<ChannelAccount>> getActivityMembers(
760763
*/
761764
public CompletableFuture<List<ChannelAccount>> getConversationMembers(TurnContextImpl context) {
762765
if (context.getActivity().getConversation() == null) {
763-
throw new IllegalArgumentException(
766+
return Async.completeExceptionally(new IllegalArgumentException(
764767
"BotFrameworkAdapter.GetActivityMembers(): missing conversation"
765-
);
768+
));
766769
}
767770

768771
if (StringUtils.isEmpty(context.getActivity().getConversation().getId())) {
769-
throw new IllegalArgumentException(
772+
return Async.completeExceptionally(new IllegalArgumentException(
770773
"BotFrameworkAdapter.GetActivityMembers(): missing conversation.id"
771-
);
774+
));
772775
}
773776

774777
ConnectorClient connectorClient = context.getTurnState().get(CONNECTOR_CLIENT_KEY);
@@ -823,11 +826,11 @@ public CompletableFuture<ConversationsResult> getConversations(
823826
String continuationToken
824827
) {
825828
if (StringUtils.isEmpty(serviceUrl)) {
826-
throw new IllegalArgumentException("serviceUrl");
829+
return Async.completeExceptionally(new IllegalArgumentException("serviceUrl"));
827830
}
828831

829832
if (credentials == null) {
830-
throw new IllegalArgumentException("credentials");
833+
return Async.completeExceptionally(new IllegalArgumentException("credentials"));
831834
}
832835

833836
return getOrCreateConnectorClient(serviceUrl, credentials)
@@ -893,28 +896,27 @@ public CompletableFuture<TokenResponse> getUserToken(
893896
String connectionName,
894897
String magicCode
895898
) {
896-
BotAssert.contextNotNull(context);
897-
899+
if (context == null) {
900+
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
901+
}
898902
if (
899903
context.getActivity().getFrom() == null
900904
|| StringUtils.isEmpty(context.getActivity().getFrom().getId())
901905
) {
902-
throw new IllegalArgumentException(
906+
return Async.completeExceptionally(new IllegalArgumentException(
903907
"BotFrameworkAdapter.getUserToken(): missing from or from.id"
904-
);
908+
));
905909
}
906910

907911
if (StringUtils.isEmpty(connectionName)) {
908-
throw new IllegalArgumentException("connectionName");
912+
return Async.completeExceptionally(new IllegalArgumentException("connectionName"));
909913
}
910914

911-
return createOAuthClient(context, null).thenCompose(oAuthClient -> {
912-
return oAuthClient.getUserToken()
913-
.getToken(
914-
context.getActivity().getFrom().getId(), connectionName,
915-
context.getActivity().getChannelId(), magicCode
916-
);
917-
});
915+
return createOAuthClient(context, null).thenCompose(oAuthClient -> oAuthClient.getUserToken()
916+
.getToken(
917+
context.getActivity().getFrom().getId(), connectionName,
918+
context.getActivity().getChannelId(), magicCode
919+
));
918920
}
919921

920922
/**
@@ -931,9 +933,12 @@ public CompletableFuture<String> getOauthSignInLink(
931933
TurnContext context,
932934
String connectionName
933935
) {
934-
BotAssert.contextNotNull(context);
936+
if (context == null) {
937+
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
938+
}
939+
935940
if (StringUtils.isEmpty(connectionName)) {
936-
throw new IllegalArgumentException("connectionName");
941+
return Async.completeExceptionally(new IllegalArgumentException("connectionName"));
937942
}
938943

939944
return createOAuthClient(context, null).thenCompose(oAuthClient -> {
@@ -988,12 +993,14 @@ public CompletableFuture<String> getOauthSignInLink(
988993
String userId,
989994
String finalRedirect
990995
) {
991-
BotAssert.contextNotNull(context);
996+
if (context == null) {
997+
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
998+
}
992999
if (StringUtils.isEmpty(connectionName)) {
993-
throw new IllegalArgumentException("connectionName");
1000+
return Async.completeExceptionally(new IllegalArgumentException("connectionName"));
9941001
}
9951002
if (StringUtils.isEmpty(userId)) {
996-
throw new IllegalArgumentException("userId");
1003+
return Async.completeExceptionally(new IllegalArgumentException("userId"));
9971004
}
9981005

9991006
return createOAuthClient(context, null).thenCompose(oAuthClient -> {
@@ -1044,9 +1051,11 @@ public CompletableFuture<Void> signOutUser(
10441051
String connectionName,
10451052
String userId
10461053
) {
1047-
BotAssert.contextNotNull(context);
1054+
if (context == null) {
1055+
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
1056+
}
10481057
if (StringUtils.isEmpty(connectionName)) {
1049-
throw new IllegalArgumentException("connectionName");
1058+
return Async.completeExceptionally(new IllegalArgumentException("connectionName"));
10501059
}
10511060

10521061
return createOAuthClient(context, null).thenCompose(oAuthClient -> {
@@ -1075,9 +1084,11 @@ public CompletableFuture<List<TokenStatus>> getTokenStatus(
10751084
String userId,
10761085
String includeFilter
10771086
) {
1078-
BotAssert.contextNotNull(context);
1087+
if (context == null) {
1088+
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
1089+
}
10791090
if (StringUtils.isEmpty(userId)) {
1080-
throw new IllegalArgumentException("userId");
1091+
return Async.completeExceptionally(new IllegalArgumentException("userId"));
10811092
}
10821093

10831094
return createOAuthClient(context, null).thenCompose(oAuthClient -> {
@@ -1107,13 +1118,14 @@ public CompletableFuture<Map<String, TokenResponse>> getAadTokens(
11071118
String[] resourceUrls,
11081119
String userId
11091120
) {
1110-
BotAssert.contextNotNull(context);
1121+
if (context == null) {
1122+
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
1123+
}
11111124
if (StringUtils.isEmpty(connectionName)) {
1112-
throw new IllegalArgumentException("connectionName");
1125+
return Async.completeExceptionally(new IllegalArgumentException("connectionName"));
11131126
}
1114-
11151127
if (resourceUrls == null) {
1116-
throw new IllegalArgumentException("resourceUrls");
1128+
return Async.completeExceptionally(new IllegalArgumentException("resourceUrls"));
11171129
}
11181130

11191131
return createOAuthClient(context, null).thenCompose(oAuthClient -> {
@@ -1348,9 +1360,9 @@ private CompletableFuture<ConnectorClient> createConnectorClient(
13481360
String audience
13491361
) {
13501362
if (claimsIdentity == null) {
1351-
throw new UnsupportedOperationException(
1363+
return Async.completeExceptionally(new UnsupportedOperationException(
13521364
"ClaimsIdentity cannot be null. Pass Anonymous ClaimsIdentity if authentication is turned off."
1353-
);
1365+
));
13541366
}
13551367

13561368
// For requests from channel App Id is in Audience claim of JWT token. For
@@ -1477,7 +1489,7 @@ private CompletableFuture<AppCredentials> getAppCredentials(String appId, String
14771489
});
14781490
}
14791491

1480-
private String getBotAppId(TurnContext turnContext) {
1492+
private String getBotAppId(TurnContext turnContext) throws IllegalStateException {
14811493
ClaimsIdentity botIdentity = turnContext.getTurnState().get(BOT_IDENTITY_KEY);
14821494
if (botIdentity == null) {
14831495
throw new IllegalStateException(

0 commit comments

Comments
 (0)