Skip to content

Commit bf7c7e8

Browse files
committed
feat: any issue that occurs while communicating with the Backend is transparent to the end user
1 parent 3dd0ddb commit bf7c7e8

3 files changed

Lines changed: 82 additions & 4 deletions

File tree

Backend.Tests/TestAPI/BackendBuilder.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class BackendBuilder
1212
{
1313
private readonly BackendContext context;
1414
private Guid databaseId = Guid.NewGuid();
15+
private bool alwaysThrowingAnyException;
1516

1617
private BackendBuilder()
1718
{
@@ -38,10 +39,20 @@ public BackendBuilder With(Guid id)
3839
return this;
3940
}
4041

42+
public BackendBuilder AlwaysThrowingAnyException()
43+
{
44+
alwaysThrowingAnyException = true;
45+
46+
return this;
47+
}
48+
4149
public HttpClient Launch()
4250
{
4351
context.SaveChanges();
4452

53+
if (alwaysThrowingAnyException)
54+
return null;
55+
4556
return new CustomWebAppFactory(BuildConnectionString())
4657
.CreateClient();
4758
}

Desktop.Tests/Integration/BackendTaskRepositoryTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,47 @@ public async Task DeletesJustTheRequestedTask_KeepingTheOtherOnes()
133133
anotherThatMustRemain,
134134
]);
135135
}
136+
137+
[Test]
138+
public async Task AnyIssueOccursWhileLoadingPersistedTasks_ReturnsNothing()
139+
{
140+
var backend = BackendBuilder.Backend()
141+
.With(AnyBackendTask())
142+
.AlwaysThrowingAnyException()
143+
.Launch();
144+
var sut = new BackendTaskRepository(backend);
145+
146+
var result = await sut.All();
147+
148+
result.Should().BeEmpty();
149+
}
150+
151+
[Test]
152+
public async Task AnyIssueOccursWhileSavingTask_DoesNotThrow()
153+
{
154+
var backend = BackendBuilder.Backend()
155+
.With(AnyBackendTask())
156+
.AlwaysThrowingAnyException()
157+
.Launch();
158+
var sut = new BackendTaskRepository(backend);
159+
160+
var sutInvocation = async () => await sut.Save(DesktopTask());
161+
162+
await sutInvocation.Should().NotThrowAsync();
163+
}
164+
165+
[Test]
166+
public async Task AnyIssueOccursWhileDeletingTask_DoesNotThrow()
167+
{
168+
var backend = BackendBuilder.Backend()
169+
.With(BackendTask(named: "ToBeDeleted"))
170+
.AlwaysThrowingAnyException()
171+
.Launch();
172+
var sut = new BackendTaskRepository(backend);
173+
174+
var sutInvocation = async () =>
175+
await sut.Delete(DesktopTask(named: "ToBeDeleted"));
176+
177+
await sutInvocation.Should().NotThrowAsync();
178+
}
136179
}

Desktop/Tasks/BackendTaskRepository.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ public BackendTaskRepository(HttpClient httpClient)
1616

1717
public async Task<IReadOnlyList<Task>> All()
1818
{
19-
var getResult = await httpClient.GetAsync("/project/tasks/");
19+
HttpResponseMessage getResult;
20+
21+
try
22+
{
23+
getResult = await httpClient.GetAsync("/project/tasks/");
24+
}
25+
catch
26+
{
27+
return [];
28+
}
2029

2130
var backendTasks =
2231
await getResult.Content.ReadAsAsync<IEnumerable<BackendTask>>();
@@ -31,7 +40,13 @@ private static Task ToDesktopTask(BackendTask task)
3140

3241
public async SystemTask Save(Task task)
3342
{
34-
await httpClient.PostAsJsonAsync("/project/tasks/", ToBackendTask(task));
43+
try
44+
{
45+
await httpClient.PostAsJsonAsync("/project/tasks/", ToBackendTask(task));
46+
}
47+
catch
48+
{
49+
}
3550
}
3651

3752
private static BackendTask ToBackendTask(Task task)
@@ -41,8 +56,17 @@ private static BackendTask ToBackendTask(Task task)
4156

4257
public async SystemTask Delete(Task toBeDeleted)
4358
{
44-
var httpResponseMessage = await httpClient.DeleteAsync(
45-
$"/project/tasks/{toBeDeleted.Name}");
59+
HttpResponseMessage httpResponseMessage;
60+
61+
try
62+
{
63+
httpResponseMessage = await httpClient.DeleteAsync(
64+
$"/project/tasks/{toBeDeleted.Name}");
65+
}
66+
catch
67+
{
68+
return;
69+
}
4670

4771
httpResponseMessage.EnsureSuccessStatusCode();
4872
}

0 commit comments

Comments
 (0)