Skip to content

Commit c6ff80a

Browse files
committed
feat: add error criticality level
1 parent 55b956d commit c6ff80a

39 files changed

Lines changed: 524 additions & 217 deletions

.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,7 @@ dotnet_diagnostic.CA1711.severity = none
489489
dotnet_diagnostic.IDE0290.severity = none
490490

491491
# prefer new C# 12 collection initialzier
492-
dotnet_diagnostic.IDE0028.severity = none
492+
dotnet_diagnostic.IDE0028.severity = none
493+
494+
# unused public parameter
495+
dotnet_diagnostic.IDE0060.severity = none

Directory.Build.props

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,4 @@
1818
<EnablePackageValidation>true</EnablePackageValidation>
1919
</PropertyGroup>
2020

21-
<PropertyGroup Condition="$(IsPackable) == 'true'">
22-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
23-
<IncludeSymbols>true</IncludeSymbols>
24-
<SymbolPackageFormat>symbols.nupkg</SymbolPackageFormat>
25-
<DocumentationFile>bin\$(Configuration)\$(MSBuildProjectName).xml</DocumentationFile>
26-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
27-
</PropertyGroup>
28-
2921
</Project>

Sstv.DomainExceptions.Extensions.DependencyInjection/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
## [Unreleased]
1616

17+
## [2.3.0] - 2024-12-06
18+
19+
### Changed
20+
21+
- error_codes_total metric now have level label from error code
22+
23+
BREAKING CHANGES:
24+
- ErrorCodesMeter now accept ErrorDescription and instance of error, instead of DomainException. This helps to use not only exceptions, but also Result pattern.
25+
1726
## [2.0.0] - 2024-02-11
1827

1928
- Source gen release

Sstv.DomainExceptions.Extensions.DependencyInjection/ErrorCodesDescriptionFromConfigurationSource.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using Microsoft.Extensions.Configuration;
2-
using System.Diagnostics.CodeAnalysis;
32

43
namespace Sstv.DomainExceptions.Extensions.DependencyInjection;
54

65
/// <summary>
76
/// Error codes description source from <see cref="IConfiguration"/>.
87
/// </summary>
9-
[SuppressMessage("Design", "CA1812: Avoid uninstantiated internal classes")]
108
public sealed class ErrorCodesDescriptionFromConfigurationSource : IErrorCodesDescriptionSource
119
{
1210
/// <summary>
@@ -48,7 +46,7 @@ public ErrorCodesDescriptionFromConfigurationSource(IConfiguration configuration
4846

4947
var description = section[nameof(ErrorDescription.Description)];
5048
var helpLink = section[nameof(ErrorDescription.HelpLink)];
51-
var isObsolete = section.GetValue(nameof(ErrorDescription.IsObsolete), defaultValue: false);
49+
var level = section.GetValue(nameof(ErrorDescription.Level), Level.Undefined);
5250

5351
if (string.IsNullOrWhiteSpace(description))
5452
{
@@ -61,7 +59,7 @@ public ErrorCodesDescriptionFromConfigurationSource(IConfiguration configuration
6159
.Where(x => !string.IsNullOrWhiteSpace(x.Value))
6260
.ToDictionary(x => x.Key, x => (object)x.Value!);
6361

64-
return new ErrorDescription(errorCode, description, helpLink, isObsolete, additionalData);
62+
return new ErrorDescription(errorCode, description, level, helpLink, additionalData);
6563
}
6664

6765
/// <summary>

Sstv.DomainExceptions.Extensions.DependencyInjection/ErrorCodesMeter.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,25 @@ public static class ErrorCodesMeter
2626
/// <summary>
2727
/// Counts error codes.
2828
/// </summary>
29-
/// <param name="domainException">Exception.</param>
30-
public static void Measure(DomainException? domainException)
29+
/// <param name="errorDescription">An error description.</param>
30+
/// <param name="instance">An error instance.</param>
31+
public static void Measure(ErrorDescription? errorDescription, object? instance)
3132
{
3233
if (!_counter.Enabled)
3334
{
3435
return;
3536
}
3637

37-
if (domainException is null)
38+
if (errorDescription is null)
3839
{
3940
return;
4041
}
4142

4243
var tagList = new TagList
4344
{
44-
{ "code", domainException.ErrorCode },
45-
{ "message", domainException.Message }
45+
{ "code", errorDescription.ErrorCode },
46+
{ "message", errorDescription.Description },
47+
{ "level", Enum.GetName(errorDescription.Level) }
4648
};
4749

4850
_counter.Add(1, tagList);
@@ -51,25 +53,15 @@ public static void Measure(DomainException? domainException)
5153
/// <summary>
5254
/// Counts error codes.
5355
/// </summary>
54-
/// <param name="errorDescription">An error description.</param>
55-
public static void Measure(ErrorDescription? errorDescription)
56+
/// <param name="domainException">Exception.</param>
57+
[Obsolete("Use Measure(ErrorDescription?, object?) instead.")]
58+
public static void Measure(DomainException? domainException)
5659
{
57-
if (!_counter.Enabled)
58-
{
59-
return;
60-
}
61-
62-
if (errorDescription is null)
60+
if (domainException is null)
6361
{
6462
return;
6563
}
6664

67-
var tagList = new TagList
68-
{
69-
{ "code", errorDescription.ErrorCode },
70-
{ "message", errorDescription.Description }
71-
};
72-
73-
_counter.Add(1, tagList);
65+
Measure(domainException.GetDescription(), domainException);
7466
}
7567
}

Sstv.DomainExceptions.Extensions.DependencyInjection/InitHostedService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public InitHostedService(IServiceProvider sp, Action<IServiceProvider, DomainExc
1919

2020
if (DomainExceptionSettings.Instance.CollectErrorCodesMetricAutomatically)
2121
{
22-
DomainExceptionSettings.Instance.OnExceptionCreated += ErrorCodesMeter.Measure;
22+
DomainExceptionSettings.Instance.OnErrorCreated += ErrorCodesMeter.Measure;
2323
}
2424

2525
DomainExceptionSettings.ErrorDescriptionSourceGetter =

Sstv.DomainExceptions.Extensions.DependencyInjection/README.md

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ This library brings to Sstv.DomainExceptions additional capabilities to register
1212
You can install using Nuget Package Manager:
1313

1414
```bash
15-
Install-Package Sstv.DomainExceptions.Extensions.DependencyInjection -Version 2.2.0
15+
Install-Package Sstv.DomainExceptions.Extensions.DependencyInjection -Version 2.3.0
1616
```
1717

1818
via the .NET CLI:
1919

2020
```bash
21-
dotnet add package Sstv.DomainExceptions.Extensions.DependencyInjection --version 2.2.0
21+
dotnet add package Sstv.DomainExceptions.Extensions.DependencyInjection --version 2.3.0
2222
```
2323

2424
or you can add package reference manually:
2525

2626
```xml
27-
<PackageReference Include="Sstv.DomainExceptions.Extensions.DependencyInjection" Version="2.2.0" />
27+
<PackageReference Include="Sstv.DomainExceptions.Extensions.DependencyInjection" Version="2.3.0" />
2828
```
2929

3030
## How to use?
@@ -46,13 +46,14 @@ services.AddDomainExceptions(builder =>
4646
settings.GenerateExceptionIdAutomatically = true; // true by default
4747
settings.CollectErrorCodesMetricAutomatically = true; // true by default
4848
settings.ThrowIfHasNoErrorCodeDescription = true; // true by default
49+
settings.AddCriticalityLevel = true; // true by default
4950
5051
// manually provide you own singleton implementation of IErrorCodesDescriptionSource
5152
settings.ErrorCodesDescriptionSource = new MyAwesomeSource();
5253

5354
// override default error description
5455
settings.DefaultErrorDescriptionProvider =
55-
errorCode => new ErrorDescription(errorCode, "N/A");
56+
errorCode => new ErrorDescription(errorCode, "N/A", Level.Fatal);
5657
};
5758
});
5859
```
@@ -69,9 +70,9 @@ services.AddDomainExceptions(bulder =>
6970
// or you can pass in memory dictionary
7071
bulder.WithErrorCodesDescriptionFromMemory(new Dictionary<string, ErrorDescription>
7172
{
72-
["SSTV.10004"] = new("SSTV.10004", "Some error description", "https://help.myproject.ru/error-codes/not-enough-money"),
73-
["SSTV.10005"] = new("SSTV.10005", "Another error", "https://help.myproject.ru/error-codes/SSTV.10005"),
74-
["SSTV.10006"] = new("SSTV.10006", "One more error", "https://help.myproject.ru/error-codes/SSTV.10006"),
73+
["SSTV.10004"] = new("SSTV.10004", "Some error description", Level.Fatal, "https://help.myproject.ru/error-codes/not-enough-money"),
74+
["SSTV.10005"] = new("SSTV.10005", "Another error", Level.Low, "https://help.myproject.ru/error-codes/SSTV.10005"),
75+
["SSTV.10006"] = new("SSTV.10006", "One more error", Level.Medium, "https://help.myproject.ru/error-codes/SSTV.10006"),
7576
});
7677

7778
// or load from appsettings.json, and optionally set configuration section name.
@@ -91,11 +92,12 @@ Below example of appsettings.json, if you choose `WithErrorCodesDescriptionFromC
9192
"ErrorCodes": {
9293
"SSTV.10004": {
9394
"Description": "You have not enough money",
95+
"Level": "Low",
9496
"HelpLink": "https://help.myproject.ru/error-codes/not-enough-money"
9597
},
9698
"SSTV.10005": {
97-
"Description": "This is an obsolete error code from appsettings.json",
98-
"IsObsolete": true
99+
"Description": "This is high criticality level error code from appsettings.json",
100+
"Level": "High"
99101
}
100102
}
101103
}
@@ -113,7 +115,7 @@ Sstv.DomainException expose public class `ErrorCodesMeter` with method `Measure`
113115
which called every time, when DomainException instantiated, and counts errors occured with OpenTelemetry counter metric:
114116

115117
```
116-
error_codes_total { code="SSTV.10004", message="You have not enough money" }
118+
error_codes_total { code="SSTV.10004", message="You have not enough money", level="Low" }
117119
```
118120

119121
This library have an extension method `AddDomainExceptionInstrumentation`, that can be called on `MeterProviderBuilder` to start collecting this metric.
@@ -182,29 +184,31 @@ output example:
182184
```json
183185
// http://localhost:5115/error-codes
184186
{
185-
  "errorCodes": [
186-
    {
187-
      "code": "SSTV.10001",
188-
      "helpLink": "https://help.myproject.ru/error-codes/not-enough-money",
189-
      "message": "You have not enough money"
190-
    },
191-
    {
192-
      "code": "DIF.10003",
193-
      "helpLink": "https://help.myproject.ru/error-codes/DIF.10003",
194-
      "message": "Obsolete error code in enum",
195-
      "isObsolete": true
196-
    },
197-
    {
198-
      "code": "SSTV.10004",
199-
      "helpLink": "https://help.myproject.ru/error-codes/not-enough-money",
200-
      "message": "You have not enough money"
201-
    },
202-
    {
203-
      "code": "SSTV.10005",
204-
      "message": "This is an obsolete error code from appsettings.json",
205-
      "isObsolete": true
206-
    }
207-
  ]
187+
"errorCodes": [
188+
{
189+
"code": "SSTV.10001",
190+
"helpLink": "https://help.myproject.ru/error-codes/not-enough-money",
191+
"message": "You have not enough money",
192+
"level": "Low"
193+
},
194+
{
195+
"code": "DIF.10003",
196+
"helpLink": "https://help.myproject.ru/error-codes/DIF.10003",
197+
"message": "Help link with template in enum member attribute",
198+
"level": "Critical"
199+
},
200+
{
201+
"code": "SSTV.10004",
202+
"helpLink": "https://help.myproject.ru/error-codes/not-enough-money",
203+
"message": "You have not enough money",
204+
"level": "High"
205+
},
206+
{
207+
"code": "SSTV.10005",
208+
"message": "This is medium criticality level error code from appsettings.json",
209+
"level": "Medium"
210+
}
211+
]
208212
}
209213
```
210214

Sstv.DomainExceptions.Extensions.DependencyInjection/Sstv.DomainExceptions.Extensions.DependencyInjection.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<TargetFramework>net6.0</TargetFramework>
55
<IsPackable>true</IsPackable>
66
<PackageReadmeFile>README.md</PackageReadmeFile>
7+
<IncludeSymbols>true</IncludeSymbols>
8+
<SymbolPackageFormat>symbols.nupkg</SymbolPackageFormat>
9+
<DocumentationFile>bin\$(Configuration)\$(MSBuildProjectName).xml</DocumentationFile>
10+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
711
</PropertyGroup>
812

913
<ItemGroup>
@@ -19,11 +23,11 @@
1923
</ItemGroup>
2024

2125
<ItemGroup>
22-
<PackageReference Include="MinVer" Version="4.3.0">
26+
<PackageReference Include="MinVer" Version="6.0.0">
2327
<PrivateAssets>all</PrivateAssets>
2428
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2529
</PackageReference>
26-
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1">
30+
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.2.25">
2731
<PrivateAssets>all</PrivateAssets>
2832
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
2933
</PackageReference>

Sstv.DomainExceptions.Extensions.ProblemDetails/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
## [Unreleased]
1616

17+
## [2.3.0] - 2024-12-06
18+
19+
- No changes here
20+
1721
## [2.2.0] - 2024-02-18
1822

1923
### Added

Sstv.DomainExceptions.Extensions.ProblemDetails/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ This library brings to Sstv.DomainExceptions additional capabilities to create r
1212
You can install using Nuget Package Manager:
1313

1414
```bash
15-
Install-Package Sstv.DomainExceptions.Extensions.ProblemDetails -Version 2.2.0
15+
Install-Package Sstv.DomainExceptions.Extensions.ProblemDetails -Version 2.3.0
1616
```
1717

1818
via the .NET CLI:
1919

2020
```bash
21-
dotnet add package Sstv.DomainExceptions.Extensions.ProblemDetails --version 2.2.0
21+
dotnet add package Sstv.DomainExceptions.Extensions.ProblemDetails --version 2.3.0
2222
```
2323

2424
or you can add package reference manually:
2525

2626
```xml
27-
<PackageReference Include="Sstv.DomainExceptions.Extensions.ProblemDetails" Version="2.2.0" />
27+
<PackageReference Include="Sstv.DomainExceptions.Extensions.ProblemDetails" Version="2.3.0" />
2828
```
2929

3030
## How to use?
@@ -84,6 +84,7 @@ throw ErrorCodes.NotEnoughMoney.ToException()
8484
"title": "You have not enough money",
8585
"status": 200,
8686
"code": "SSTV10001",
87+
"criticalityLevel": "Low",
8788
"errorId": "ad3f064c-1254-41dd-82e4-891507937cf6"
8889
}
8990
```

0 commit comments

Comments
 (0)