Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit efb8213

Browse files
feich-msdarrenj
authored andcommitted
add exception handling for todo skill (#432)
* add exception handling for todo skill based on Ted's template * optimize the exception handling logic * fix tests failure * remove de resource file of ToDoSharedResponse * add test to check ServiceException
1 parent 3bc6ae8 commit efb8213

File tree

14 files changed

+581
-579
lines changed

14 files changed

+581
-579
lines changed

solutions/Virtual-Assistant/src/csharp/skills/tests/todoskilltest/API/Fakes/MockHttpClientHandlerGen.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ private void SetHttpMockBehavior(ref Mock<HttpClientHandler> mockClient)
156156
Content = new StringContent(string.Empty),
157157
})
158158
.Callback<HttpRequestMessage, CancellationToken>((r, c) => this.MarkOrDeleteOutlookTask(r));
159+
160+
mockClient
161+
.Protected()
162+
.Setup<Task<HttpResponseMessage>>(
163+
"SendAsync",
164+
ItExpr.Is<HttpRequestMessage>(r => r.RequestUri.ToString().StartsWith("https://graph.microsoft.com/beta/me/outlook/taskFolders/Shopping/tasks") && r.Method == HttpMethod.Post),
165+
ItExpr.IsAny<CancellationToken>())
166+
.ReturnsAsync(() => new HttpResponseMessage()
167+
{
168+
StatusCode = System.Net.HttpStatusCode.Unauthorized,
169+
Content = new StringContent(this.GenerateServiceExceptionResponse()),
170+
});
159171
}
160172

161173
private string GetTodoHtml(TaskItem task)
@@ -298,5 +310,10 @@ private bool MarkOrDeleteOutlookTask(HttpRequestMessage request)
298310

299311
return true;
300312
}
313+
314+
private string GenerateServiceExceptionResponse()
315+
{
316+
return "{ \"error\": { \"code\": \"ErrorAccessDenied\", \"message\": \"Access is denied. Check credentials and try again.\", \"innerError\": { \"request-id\": \"fcfed3fd-2c0a-4278-b55a-5b156af772dd\", \"date\": \"2018-12-14T06:34:54\" } } }";
317+
}
301318
}
302319
}

solutions/Virtual-Assistant/src/csharp/skills/tests/todoskilltest/API/OutlookServiceTests.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Net.Http;
34
using System.Threading.Tasks;
5+
using Microsoft.Bot.Solutions.Skills;
46
using Microsoft.VisualStudio.TestTools.UnitTesting;
5-
using ToDoSkill;
67
using ToDoSkill.ServiceClients;
78
using ToDoSkillTest.API.Fakes;
89

@@ -76,5 +77,22 @@ public async Task ShowTaskTest()
7677

7778
Assert.IsTrue(taskList != null && taskList.Count > 0);
7879
}
80+
81+
[TestMethod]
82+
public async Task AddTaskAccessDeniedTests()
83+
{
84+
var service = new OutlookService();
85+
var pageId = new Dictionary<string, string>() { { "ToDo", "ToDo" }, { "Grocery", "Grocery" }, { "Shopping", "Shopping" } };
86+
try
87+
{
88+
await service.InitAsync("test", pageId, mockClient);
89+
await service.AddTaskAsync("Shopping", "Test 9");
90+
Assert.Fail();
91+
}
92+
catch (SkillException ex)
93+
{
94+
Assert.AreEqual(ex.ExceptionType, SkillExceptionType.APIAccessDenied);
95+
}
96+
}
7997
}
8098
}

solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/DeleteToDoItemDialog.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Bot.Solutions.Dialogs;
99
using Microsoft.Bot.Solutions.Extensions;
1010
using Microsoft.Bot.Solutions.Skills;
11+
using Microsoft.Bot.Solutions.Util;
1112
using ToDoSkill.Dialogs.DeleteToDo.Resources;
1213
using ToDoSkill.Dialogs.Shared.Resources;
1314

@@ -58,7 +59,15 @@ public DeleteToDoItemDialog(
5859

5960
public async Task<DialogTurnResult> CollectAskDeletionConfirmation(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
6061
{
61-
return await sc.BeginDialogAsync(Action.CollectDeleteTaskConfirmation);
62+
try
63+
{
64+
return await sc.BeginDialogAsync(Action.CollectDeleteTaskConfirmation);
65+
}
66+
catch (Exception ex)
67+
{
68+
await HandleDialogExceptions(sc, ex);
69+
return new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs);
70+
}
6271
}
6372

6473
public async Task<DialogTurnResult> DeleteToDoTask(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
@@ -141,10 +150,15 @@ public DeleteToDoItemDialog(
141150

142151
return await sc.EndDialogAsync(true);
143152
}
144-
catch
153+
catch (SkillException ex)
154+
{
155+
await HandleDialogExceptions(sc, ex);
156+
return new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs);
157+
}
158+
catch (Exception ex)
145159
{
146-
await HandleDialogExceptions(sc);
147-
throw;
160+
await HandleDialogExceptions(sc, ex);
161+
return new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs);
148162
}
149163
}
150164

@@ -168,10 +182,10 @@ public DeleteToDoItemDialog(
168182
return await sc.PromptAsync(Action.Prompt, new PromptOptions() { Prompt = prompt });
169183
}
170184
}
171-
catch
185+
catch (Exception ex)
172186
{
173-
await HandleDialogExceptions(sc);
174-
throw;
187+
await HandleDialogExceptions(sc, ex);
188+
return new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs);
175189
}
176190
}
177191

@@ -202,10 +216,10 @@ public DeleteToDoItemDialog(
202216
return await sc.BeginDialogAsync(Action.CollectDeleteTaskConfirmation);
203217
}
204218
}
205-
catch
219+
catch (Exception ex)
206220
{
207-
await HandleDialogExceptions(sc);
208-
throw;
221+
await HandleDialogExceptions(sc, ex);
222+
return new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs);
209223
}
210224
}
211225
}

solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/MarkToDoItemDialog.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Bot.Solutions.Dialogs;
88
using Microsoft.Bot.Solutions.Extensions;
99
using Microsoft.Bot.Solutions.Skills;
10+
using Microsoft.Bot.Solutions.Util;
1011
using ToDoSkill.Dialogs.MarkToDo.Resources;
1112
using ToDoSkill.Dialogs.Shared.Resources;
1213

@@ -90,10 +91,15 @@ public MarkToDoItemDialog(
9091
await sc.Context.SendActivityAsync(markToDoReply);
9192
return await sc.EndDialogAsync(true);
9293
}
93-
catch
94+
catch (SkillException ex)
9495
{
95-
await HandleDialogExceptions(sc);
96-
throw;
96+
await HandleDialogExceptions(sc, ex);
97+
return new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs);
98+
}
99+
catch (Exception ex)
100+
{
101+
await HandleDialogExceptions(sc, ex);
102+
return new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs);
97103
}
98104
}
99105
}

solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017
22
// Copyright (c) Microsoft Corporation. All rights reserved.
33
// Licensed under the MIT License.
4-
using System;
54
using System.IO;
65
using System.Runtime.CompilerServices;
76
using Microsoft.Bot.Solutions.Dialogs;
@@ -35,6 +34,8 @@ static ToDoSharedResponses()
3534

3635
public static BotResponse ToDoErrorMessage => GetBotResponse();
3736

37+
public static BotResponse ToDoErrorMessage_BotProblem => GetBotResponse();
38+
3839
public static BotResponse SettingUpOneNoteMessage => GetBotResponse();
3940

4041
public static BotResponse ShowToDoTasks => GetBotResponse();

0 commit comments

Comments
 (0)