Skip to content

Commit a7d8b4d

Browse files
authored
Merge pull request #626 from cabinetoffice/EHD-1663-sentry-errors
EHD-1663: Reduce errors in Sentry
2 parents 64cb033 + cca20ba commit a7d8b4d

11 files changed

Lines changed: 63 additions & 18 deletions

GenderPayGap.WebUI/BackgroundJobs/ScheduledJobs/NotifyUsersAndRetireInactiveAccountsJob.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ private void SendReminderEmails()
3737
DateTime threeYearsAgoMinusSevenDays = threeYearsAgo.AddDays(7);
3838

3939
List<User> usersToSendReminders = dataRepository.GetAll<User>()
40-
.Where(u => u.LoginDate >= threeYearsAgoMinusThirtyDays && u.LoginDate < threeYearsAgoMinusThirtyDays.AddDays(1) || u.LoginDate >= threeYearsAgoMinusSevenDays && u.LoginDate < threeYearsAgoMinusSevenDays.AddDays(1) )
40+
.Where(u =>
41+
(u.LoginDate >= threeYearsAgoMinusThirtyDays && u.LoginDate < threeYearsAgoMinusThirtyDays.AddDays(1)) ||
42+
(u.LoginDate >= threeYearsAgoMinusSevenDays && u.LoginDate < threeYearsAgoMinusSevenDays.AddDays(1))
43+
)
44+
.Where(u => !u.HasBeenAnonymised)
4145
.ToList();
4246

4347
foreach (User user in usersToSendReminders)

GenderPayGap.WebUI/Controllers/Account/CloseAccountController.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ private void RetireUserAccount(User user)
132132
private void SendAccountClosedEmail(User user)
133133
{
134134
// Send email to user informing them of account closure
135-
emailSendingService.SendCloseAccountCompletedEmail(user.EmailAddress);
135+
if (!user.HasBeenAnonymised)
136+
{
137+
emailSendingService.SendCloseAccountCompletedEmail(user.EmailAddress);
138+
}
136139
}
137140

138141
private void InformGeoOfOrphanedOrganisations(List<Organisation> orphanedOrganisations)

GenderPayGap.WebUI/Controllers/CompareEmployersController.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public IActionResult AddEmployer(long organisationId, string returnUrl)
3333
comparisonBasketService.AddToBasket(organisationId);
3434
comparisonBasketService.SaveComparedEmployersToCookie();
3535

36-
return LocalRedirect(returnUrl);
36+
return Url.IsLocalUrl(returnUrl)
37+
? LocalRedirect(returnUrl)
38+
: RedirectToAction("CompareEmployersNoYear", "CompareEmployers");
3739
}
3840

3941
[HttpGet("/compare-employers/add-js/{organisationId}")]
@@ -59,7 +61,14 @@ public IActionResult RemoveEmployer(long organisationId, string returnUrl)
5961
comparisonBasketService.RemoveFromBasket(organisationId);
6062
comparisonBasketService.SaveComparedEmployersToCookie();
6163

62-
return LocalRedirect(returnUrl);
64+
if (Url.IsLocalUrl(returnUrl))
65+
{
66+
return LocalRedirect(returnUrl);
67+
}
68+
69+
return comparisonBasketService.ComparedEmployers.Any()
70+
? RedirectToAction("CompareEmployersNoYear", "CompareEmployers")
71+
: RedirectToAction("SearchPage", "Search");
6372
}
6473

6574
[HttpGet("/compare-employers/remove-js/{organisationId}")]
@@ -82,7 +91,14 @@ public IActionResult ClearEmployers(string returnUrl)
8291
comparisonBasketService.ClearBasket();
8392
comparisonBasketService.SaveComparedEmployersToCookie();
8493

85-
return LocalRedirect(returnUrl);
94+
if (Url.IsLocalUrl(returnUrl))
95+
{
96+
return LocalRedirect(returnUrl);
97+
}
98+
99+
return comparisonBasketService.ComparedEmployers.Any()
100+
? RedirectToAction("CompareEmployersNoYear", "CompareEmployers")
101+
: RedirectToAction("SearchPage", "Search");
86102
}
87103

88104
[HttpGet("/compare-employers")]

GenderPayGap.WebUI/Controllers/HealthCheckController.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using GenderPayGap.Core.Classes.Logger;
12
using GenderPayGap.Core.Interfaces;
23
using GenderPayGap.Database;
4+
using GenderPayGap.WebUI.ErrorHandling;
35
using GenderPayGap.WebUI.ExternalServices.FileRepositories;
46
using GenderPayGap.WebUI.Search;
57
using Microsoft.AspNetCore.Mvc;
@@ -37,7 +39,8 @@ private void CheckDatabaseConnection()
3739

3840
if (anOrganisation == null)
3941
{
40-
throw new Exception("Could not load an organisation from the database");
42+
CustomLogger.Error("Health Check Failed: Could not load an organisation from the database");
43+
throw new HealthCheckFailedException();
4144
}
4245
}
4346

@@ -64,7 +67,8 @@ private void CheckFileConnection()
6467
}
6568
catch (Exception e)
6669
{
67-
throw new Exception($"Could not read or write a file: {e.Message}", e);
70+
CustomLogger.Error($"Health Check Failed: Could not read or write a file: {e.Message}");
71+
throw new HealthCheckFailedException();
6872
}
6973
}
7074

@@ -73,7 +77,8 @@ private void CheckSearchRepositoryIsLoaded()
7377
if (SearchRepository.CachedOrganisations == null ||
7478
SearchRepository.CachedUsers == null)
7579
{
76-
throw new Exception("SearchRepository was not loaded");
80+
CustomLogger.Error("Health Check Failed: SearchRepository was not loaded");
81+
throw new HealthCheckFailedException();
7782
}
7883
}
7984
}

GenderPayGap.WebUI/Controllers/ManageOrganisations/RemoveUserFromOrganisationController.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,19 @@ private void RemoveUserFromOrganisation(UserOrganisation userOrganisationToRemov
101101
registrationRepository.RemoveRegistration(userOrganisationToRemove, actionByUser);
102102

103103
// Email user that has been unregistered
104-
emailSendingService.SendRemovedUserFromOrganisationEmail(
105-
userToUnregister.EmailAddress,
106-
orgToRemove.OrganisationName,
107-
userToUnregister.Fullname);
104+
if (!userToUnregister.HasBeenAnonymised)
105+
{
106+
emailSendingService.SendRemovedUserFromOrganisationEmail(
107+
userToUnregister.EmailAddress,
108+
orgToRemove.OrganisationName,
109+
userToUnregister.Fullname);
110+
}
108111

109112
// Email the other users of the organisation
110-
IEnumerable<string> emailAddressesForOrganisation = orgToRemove.UserOrganisations.Select(uo => uo.User.EmailAddress);
113+
IEnumerable<string> emailAddressesForOrganisation = orgToRemove.UserOrganisations
114+
.Where(uo => !uo.User.HasBeenAnonymised)
115+
.Select(uo => uo.User.EmailAddress);
116+
111117
foreach (string emailAddress in emailAddressesForOrganisation)
112118
{
113119
emailSendingService.SendRemovedUserFromOrganisationEmail(

GenderPayGap.WebUI/Controllers/ScopeController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ public void SendScopeChangeEmails(Organisation organisation, DateTime reportingY
263263
// Find all email addresses associated with the organisation - only the active ones (who have confirmed their PIN)
264264
IEnumerable<string> emailAddressesForOrganisation = organisation.UserOrganisations
265265
.Where(uo => uo.PINConfirmedDate.HasValue)
266+
.Where(uo => !uo.User.HasBeenAnonymised)
266267
.Select(uo => uo.User.EmailAddress);
267268

268269
// Send email of correct type to each email address associated with organisation

GenderPayGap.WebUI/ErrorHandling/CustomErrorPageExceptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,10 @@ public class RegistrationNotFoundException : CustomErrorPageException
8585
public override string ViewName => "../Errors/RegistrationNotFound";
8686
public override int StatusCode => 404;
8787
}
88+
89+
public class HealthCheckFailedException : CustomErrorPageException
90+
{
91+
public override string ViewName => "../Errors/ThereIsAProblemWithTheService";
92+
public override int StatusCode => 500;
93+
}
8894
}

GenderPayGap.WebUI/GenderPayGap.WebUI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
<ItemGroup>
9292
<ProjectReference Include="..\GenderPayGap.Core\GenderPayGap.Core.csproj" />
9393
<ProjectReference Include="..\GenderPayGap.Database\GenderPayGap.Database.csproj" />
94-
<PackageReference Include="CabinetOffice.GovUkDesignSystem" Version="2.0.1" />
94+
<PackageReference Include="CabinetOffice.GovUkDesignSystem" Version="2.0.2" />
9595
</ItemGroup>
9696

9797
</Project>

GenderPayGap.WebUI/Helpers/StaticAssetsVersioningHelper.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System.Collections.Concurrent;
2+
using System.Reflection;
23
using System.Text.RegularExpressions;
34
using GenderPayGap.Extensions.AspNetCore;
45

@@ -13,7 +14,7 @@ public static class StaticAssetsVersioningHelper
1314
private const string AppIe8CssRegex = "app-ie8-[^-]*.css";
1415
private const string AppJsRegex = "app-.*.js";
1516

16-
private static Dictionary<string, string> cachedFilenames = new Dictionary<string, string>();
17+
private static ConcurrentDictionary<string, string> cachedFilenames = new();
1718

1819
public static string GetAppCssFilename() => GetStaticFile(CompiledDirectory, AppCssRegex);
1920
public static string GetAppIe8CssFilename() => GetStaticFile(CompiledDirectory, AppIe8CssRegex);
@@ -32,11 +33,13 @@ private static string GetStaticFile(string directory, string fileRegex)
3233
// cache the filename so we don't need to search a directory for each request
3334
string cacheKey = directory + "/" + fileRegex;
3435

35-
if (!cachedFilenames.ContainsKey(cacheKey))
36+
if (cachedFilenames.TryGetValue(cacheKey, out string filename))
3637
{
37-
cachedFilenames[cacheKey] = FindMatchingFile(directory, fileRegex);
38+
return filename;
3839
}
3940

41+
cachedFilenames[cacheKey] = FindMatchingFile(directory, fileRegex);
42+
4043
return cachedFilenames[cacheKey];
4144
}
4245
}

GenderPayGap.WebUI/Services/EmailSendingServiceHelpers.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static void SendUserAddedEmailToExistingUsers(Organisation organisation,
2121
public static void SendSuccessfulSubmissionEmailToRegisteredUsers(Return postedReturn, string reportLink, string submittedOrUpdated, EmailSendingService emailSendingService)
2222
{
2323
IEnumerable<string> emailAddressesForOrganisation = postedReturn.Organisation.UserOrganisations
24+
.Where(uo => !uo.User.HasBeenAnonymised)
2425
.Select(uo => uo.User.EmailAddress);
2526

2627
foreach (string emailAddress in emailAddressesForOrganisation)

0 commit comments

Comments
 (0)