-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathEnvironmentConfigurationResolver.cs
More file actions
74 lines (66 loc) · 2.77 KB
/
EnvironmentConfigurationResolver.cs
File metadata and controls
74 lines (66 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Reqnroll.CommonModels;
using Reqnroll.EnvironmentAccess;
using Reqnroll.Formatters.RuntimeSupport;
using System;
using System.Text.Json;
namespace Reqnroll.Formatters.Configuration;
public class EnvironmentConfigurationResolver : FormattersConfigurationResolverBase, IFormattersEnvironmentOverrideConfigurationResolver
{
private readonly IEnvironmentWrapper _environmentWrapper;
private readonly IFormatterLog _log;
private readonly string _environmentVariableName;
public EnvironmentConfigurationResolver(
IEnvironmentWrapper environmentWrapper,
IFormatterLog log = null)
{
_environmentWrapper = environmentWrapper;
_log = log;
_environmentVariableName = FormattersConfigurationConstants.REQNROLL_FORMATTERS_ENVIRONMENT_VARIABLE;
}
internal EnvironmentConfigurationResolver(
IEnvironmentWrapper environmentWrapper,
string environmentVariableName,
IFormatterLog log = null)
{
_environmentWrapper = environmentWrapper ?? throw new ArgumentNullException(nameof(environmentWrapper));
_log = log;
_environmentVariableName = environmentVariableName ?? throw new ArgumentNullException(nameof(environmentVariableName));
}
protected override JsonDocument GetJsonDocument()
{
try
{
var formatters = _environmentWrapper.GetEnvironmentVariable(_environmentVariableName);
if (formatters is Success<string> formattersSuccess)
{
if (string.IsNullOrWhiteSpace(formattersSuccess.Result))
{
_log?.WriteMessage($"Environment variable {_environmentVariableName} is empty");
return null;
}
try
{
return JsonDocument.Parse(formattersSuccess.Result, new JsonDocumentOptions
{
CommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true // More lenient parsing
});
}
catch (JsonException ex)
{
_log?.WriteMessage($"Failed to parse JSON from environment variable {_environmentVariableName}: {ex.Message}");
}
}
else if (formatters is Failure<string> failure)
{
_log?.WriteMessage($"Could not retrieve environment variable {_environmentVariableName}: {failure.Description}");
}
}
catch (Exception ex) when (ex is not JsonException)
{
// Catch any unexpected exceptions but don't let them propagate
_log?.WriteMessage($"Unexpected error retrieving environment configuration: {ex.Message}");
}
return null;
}
}