Skip to content

Commit 15a3225

Browse files
authored
Merge pull request #2
Allow to filter additional data from domain exception that will be added to ProblemDetails
2 parents d82668c + 3b680e8 commit 15a3225

File tree

20 files changed

+280
-20
lines changed

20 files changed

+280
-20
lines changed

.config/dotnet-tools.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"husky": {
6+
"version": "0.7.2",
7+
"commands": [
8+
"husky"
9+
],
10+
"rollForward": false
11+
}
12+
}
13+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,5 @@ ASALocalRun/
330330
#Ocelot.json
331331
ocelot.json
332332
data/
333+
334+
!.husky/csx/

.husky/commit-msg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
dotnet husky run -v --group "commit-msg" --args "$1"
5+
6+
echo
7+
echo Great work! 🥂
8+
echo

.husky/csx/commit-lint.csx

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/// <summary>
2+
/// Commit linter
3+
/// https://www.conventionalcommits.org/en/v1.0.0/
4+
/// https://github.com/angular/angular/blob/22b96b9/CONTRIBUTING.md#type
5+
/// </summary>
6+
7+
#r "System.dll"
8+
#r "System.Linq.dll"
9+
10+
using System;
11+
using System.Linq;
12+
using System.Text.RegularExpressions;
13+
14+
private static var tasksPrefixes = new [] { "SSTV" };
15+
16+
private static var allowedCommitTypes = new []
17+
{
18+
"feat",
19+
"bug",
20+
"ci",
21+
"wip",
22+
"fix",
23+
"perf",
24+
"refactor",
25+
"docs",
26+
"build",
27+
"chore",
28+
"revert",
29+
"style",
30+
"test"
31+
};
32+
33+
private var pattern = $@"^(?=.{{1,90}}$)(?:{string.Join("|", allowedCommitTypes)})(?:\((?<scope>.+)\)|)(|!)(?::)\s.{{4,}}(?<![\.\s])$";
34+
35+
private var msg = File.ReadAllLines(Args[0])[0];
36+
37+
private var match = Regex.Match(msg, pattern);
38+
39+
if (!match.Success)
40+
{
41+
return Fail(msg);
42+
}
43+
44+
private var scope = match.Groups["scope"]?.Value;
45+
46+
if (string.IsNullOrWhiteSpace(scope))
47+
{
48+
return 0;
49+
}
50+
51+
private var isTaskScope = tasksPrefixes.Any(tp => scope?.StartsWith($"{tp}-") == true);
52+
53+
if (isTaskScope)
54+
{
55+
return 0;
56+
}
57+
58+
return Fail(msg);
59+
60+
private static int Fail(string msg)
61+
{
62+
Console.ForegroundColor = ConsoleColor.Red;
63+
Console.WriteLine("Invalid commit message: {0}", msg);
64+
Console.ResetColor();
65+
Console.Write($"commit type must be one of [{string.Join(", ", allowedCommitTypes)}]");
66+
67+
if (tasksPrefixes.Length > 0)
68+
{
69+
Console.WriteLine($" or [{string.Join("|", tasksPrefixes.Select(tp => tp + "-XXX"))}] ");
70+
Console.WriteLine($"e.g: '{allowedCommitTypes[0]}({tasksPrefixes[0]}-1234): subject' or '{allowedCommitTypes[0]}: subject'");
71+
}
72+
else
73+
{
74+
Console.WriteLine();
75+
Console.WriteLine($"e.g: '{allowedCommitTypes[0]}: subject'");
76+
}
77+
78+
Console.ForegroundColor = ConsoleColor.Gray;
79+
Console.WriteLine("more info: https://www.conventionalcommits.org/en/v1.0.0/");
80+
81+
return 1;
82+
}

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
dotnet husky run -v --group "pre-commit"

.husky/pre-push

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
dotnet husky run -v --group "pre-push"

.husky/task-runner.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"$schema": "https://alirezanet.github.io/Husky.Net/schema.json",
3+
"tasks": [
4+
{
5+
"name": "commit-message-linter",
6+
"group": "commit-msg",
7+
"command": "dotnet",
8+
"args": ["husky", "exec", ".husky/csx/commit-lint.csx", "--args", "${args}"]
9+
},
10+
{
11+
"name": "dotnet-format",
12+
"group": "commit-msg",
13+
"command": "dotnet",
14+
"output": "verbose",
15+
"args": ["format", "-v", "normal", "--include", "${staged}"]
16+
},
17+
{
18+
"name": "dotnet-build",
19+
"group": "pre-push",
20+
"command": "dotnet",
21+
"args": ["build", "--no-restore"],
22+
"include": ["**/*.cs"]
23+
},
24+
{
25+
"name": "dotnet-test",
26+
"group": "pre-push",
27+
"command": "dotnet",
28+
"args": ["test", "--no-restore", "--no-build", "--filter", "\"TestCategory=UnitTests\""],
29+
"include": ["**/*.cs"]
30+
}
31+
]
32+
}

Sstv.DomainExceptions.Extensions.ProblemDetails/CHANGELOG.md

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

1515
## [Unreleased]
1616

17-
## [3.1.0] - 2025-03-06
17+
## [3.1.0] - 2025-04-02
1818

1919
### Added
2020

2121
- Added Microsoft.CodeAnalysis.PublicApiAnalyzers to control shipped API
22+
- Allow to filter additional data from domain exception that will be added to ProblemDetails. Specify DomainExceptionSettings.AdditionalDataResponseIncludingFilter.
23+
24+
### Fixed
25+
- DetailedMessage from DomainException not passed to ProblemDetails response
2226

2327

2428
## [3.0.0] - 2024-12-05

Sstv.DomainExceptions.Extensions.ProblemDetails/ErrorCodeProblemDetails.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Text.Json.Serialization;
33

44
namespace Sstv.DomainExceptions.Extensions.ProblemDetails;
@@ -38,6 +38,8 @@ public ErrorCodeProblemDetails(DomainException domainException)
3838
{
3939
ArgumentNullException.ThrowIfNull(domainException);
4040

41+
Detail = domainException.DetailedMessage;
42+
4143
foreach (DictionaryEntry e in domainException.Data)
4244
{
4345
var stringKey = e.Key as string ?? e.Key.ToString();
@@ -46,7 +48,13 @@ public ErrorCodeProblemDetails(DomainException domainException)
4648
{
4749
stringKey = System.Text.Json.JsonNamingPolicy.CamelCase.ConvertName(stringKey);
4850

49-
Extensions.TryAdd(stringKey, e.Value);
51+
var filter = DomainExceptionSettings.Instance.AdditionalDataResponseIncludingFilter;
52+
var add = filter is null || filter.Invoke(new AdditionalDataPropertyFilterArgs(domainException, stringKey, e.Value));
53+
54+
if (add)
55+
{
56+
Extensions.TryAdd(stringKey, e.Value);
57+
}
5058
}
5159
}
5260
}

Sstv.DomainExceptions.Extensions.ProblemDetails/README.md

Lines changed: 6 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 3.0.0
15+
Install-Package Sstv.DomainExceptions.Extensions.ProblemDetails -Version 3.1.0
1616
```
1717

1818
via the .NET CLI:
1919

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

2424
or you can add package reference manually:
2525

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

3030
## How to use?
@@ -65,6 +65,8 @@ services.AddProblemDetails(x =>
6565
})
6666
```
6767

68+
Please, look at [here](./../Sstv.Host/ServiceCollectionExtensions.cs) for additional examples.
69+
6870
### Add exception handler middlware
6971

7072
```csharp
@@ -82,6 +84,7 @@ throw ErrorCodes.NotEnoughMoney.ToException()
8284
{
8385
"type": "https://help.myproject.ru/error-codes/not-enough-money",
8486
"title": "You have not enough money",
87+
"detail": "You want 500, but your account balance is 300.",
8588
"status": 200,
8689
"code": "SSTV10001",
8790
"criticalityLevel": "Low",

0 commit comments

Comments
 (0)