Skip to content

Commit b445be2

Browse files
Handles exceptions when loading service config
Improves the robustness of the service configuration loading process by catching potential exceptions. Specifically, it catches exceptions when attempting to access the AppData path and unauthorized access exceptions when reading the service configuration file. This prevents the service from crashing and provides more informative error messages to the user.
1 parent bb45fd0 commit b445be2

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/Certify.Shared/Utils/ServiceConfigManager.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@ public static ServiceConfig GetAppServiceConfig()
2020
ConfigStatus = ConfigStatus.DefaultFailed
2121
};
2222

23-
var appDataPath = EnvironmentUtil.EnsuredAppDataPath();
23+
var appDataPath = string.Empty;
24+
try
25+
{
26+
appDataPath = EnvironmentUtil.EnsuredAppDataPath();
27+
}
28+
catch (Exception exp)
29+
{
30+
System.Console.WriteLine($"ServiceConfigManager: Failed to get AppData path. {exp.Message}");
31+
serviceConfig.ServiceFaultMsg = $"Failed to get AppData path. {exp.Message}";
32+
return serviceConfig;
33+
}
34+
2435
var serviceConfigFile = Path.Combine(appDataPath, "serviceconfig.json");
2536
#if DEBUG
2637
serviceConfigFile = Path.Combine(appDataPath, "serviceconfig.debug.json");
@@ -51,6 +62,12 @@ public static ServiceConfig GetAppServiceConfig()
5162

5263
}
5364
}
65+
catch (UnauthorizedAccessException uaExp)
66+
{
67+
serviceConfig.ConfigStatus = ConfigStatus.DefaultFailed;
68+
serviceConfig.ServiceFaultMsg = $"Access denied to service configuration file at {serviceConfigFile}. {uaExp.Message}";
69+
System.Console.WriteLine($"ServiceConfigManager: {serviceConfig.ServiceFaultMsg}");
70+
}
5471
catch (Exception exp)
5572
{
5673
if (serviceConfig != null)

0 commit comments

Comments
 (0)