Skip to content

Commit

Permalink
improvement: Better message for request validation exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jhartmann123 authored and davidroth committed Jul 31, 2024
1 parent 934fec9 commit cc73761
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
include:
- project: 'fusonic/devops/images/gitlab-ci-tools'
file: 'dotnet_test.yml'
ref: '5.3'
ref: '5.9'
- project: "fusonic/devops/images/gitlab-ci-tools"
file: "release_tool.yml"
ref: "5.3"
ref: "5.9"

stages:
- build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public Task<TResult> Handle(TCommand request, CancellationToken cancellationToke
kv => kv.Key,
kv => kv.Value!.Errors.Select(e => e.ErrorMessage).ToList());

throw new RequestValidationException(errors);
throw new RequestValidationException(typeof(TCommand), errors);
}
}
32 changes: 30 additions & 2 deletions src/AspNetCore/src/Validation/RequestValidationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,35 @@

namespace Fusonic.Extensions.AspNetCore.Validation;

public class RequestValidationException(IDictionary<string, List<string>> errors) : Exception("One or more validation errors occurred.")
public class RequestValidationException : Exception
{
public IDictionary<string, List<string>> Errors { get; } = errors;
public RequestValidationException(IDictionary<string, List<string>> errors)
: base($"""
One or more validation errors occurred.
{GetErrorMessage(errors)}
""")
{
Errors = errors;
}

public RequestValidationException(Type requestType, IDictionary<string, List<string>> errors)
: base($"""
One or more validation errors occurred.
Request type: {requestType.FullName}.
{GetErrorMessage(errors)}
""")
{
RequestType = requestType;
Errors = errors;
}

public IDictionary<string, List<string>> Errors { get; }
public Type? RequestType { get; }

private static string GetErrorMessage(IDictionary<string, List<string>> errors)
{
var messages = errors.SelectMany(err => err.Value.Select(e => $"{err.Key}: {e}"));
var message = string.Join(Environment.NewLine, messages);
return message;
}
}
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>9.1.0</Version>
<Version>9.1.1</Version>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

Expand Down

0 comments on commit cc73761

Please sign in to comment.