Skip to content

Commit f377102

Browse files
committed
Don't crash when user records can't be saved
1 parent efc5d4e commit f377102

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

LinkDotNet.Blog.UnitTests/Web/Shared/UserRecordServiceTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using LinkDotNet.Blog.Web.Shared;
88
using LinkDotNet.Domain;
99
using LinkDotNet.Infrastructure.Persistence;
10+
using Microsoft.Extensions.Logging;
1011
using Moq;
1112
using Xunit;
1213

@@ -30,7 +31,8 @@ public UserRecordServiceTests()
3031
repositoryMock.Object,
3132
fakeNavigationManager,
3233
fakeAuthenticationStateProvider,
33-
localStorageService.Object);
34+
localStorageService.Object,
35+
new Mock<ILogger>().Object);
3436
}
3537

3638
[Fact]
@@ -78,5 +80,15 @@ public async Task ShouldNotStoreForAdmin()
7880

7981
repositoryMock.Verify(r => r.StoreAsync(It.IsAny<UserRecord>()), Times.Never);
8082
}
83+
84+
[Fact]
85+
public async Task ShouldNotThrowExceptionToOutsideWorld()
86+
{
87+
localStorageService.Setup(l => l.ContainKeyAsync("user", default)).Throws<Exception>();
88+
89+
Func<Task> act = () => sut.StoreUserRecordAsync();
90+
91+
await act.Should().NotThrowAsync<Exception>();
92+
}
8193
}
8294
}

LinkDotNet.Blog.Web/Shared/UserRecordService.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using LinkDotNet.Infrastructure.Persistence;
66
using Microsoft.AspNetCore.Components;
77
using Microsoft.AspNetCore.Components.Authorization;
8+
using Microsoft.Extensions.Logging;
89

910
namespace LinkDotNet.Blog.Web.Shared
1011
{
@@ -19,20 +20,35 @@ public class UserRecordService : IUserRecordService
1920
private readonly NavigationManager navigationManager;
2021
private readonly AuthenticationStateProvider authenticationStateProvider;
2122
private readonly ILocalStorageService localStorageService;
23+
private readonly ILogger logger;
2224

2325
public UserRecordService(
2426
IRepository<UserRecord> userRecordRepository,
2527
NavigationManager navigationManager,
2628
AuthenticationStateProvider authenticationStateProvider,
27-
ILocalStorageService localStorageService)
29+
ILocalStorageService localStorageService,
30+
ILogger logger)
2831
{
2932
this.userRecordRepository = userRecordRepository;
3033
this.navigationManager = navigationManager;
3134
this.authenticationStateProvider = authenticationStateProvider;
3235
this.localStorageService = localStorageService;
36+
this.logger = logger;
3337
}
3438

3539
public async Task StoreUserRecordAsync()
40+
{
41+
try
42+
{
43+
await GetAndStoreUSerRecordAsync();
44+
}
45+
catch (Exception e)
46+
{
47+
logger.Log(LogLevel.Error, e, "Couldn't write user record");
48+
}
49+
}
50+
51+
private async Task GetAndStoreUSerRecordAsync()
3652
{
3753
var userIdentity = (await authenticationStateProvider.GetAuthenticationStateAsync()).User.Identity;
3854
if (userIdentity == null || userIdentity.IsAuthenticated)

0 commit comments

Comments
 (0)