39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
using FluentValidation.Results;
|
|
|
|
namespace cuqmbr.TravelGuide.Application.Common.Exceptions;
|
|
|
|
public class ValidationException : Exception
|
|
{
|
|
public ValidationException()
|
|
: base("One or more validation failures have occurred.")
|
|
{
|
|
Errors = new Dictionary<string, string[]>();
|
|
}
|
|
|
|
public ValidationException(string message) : base(message)
|
|
{
|
|
Errors = new Dictionary<string, string[]>();
|
|
}
|
|
|
|
public ValidationException(IEnumerable<ValidationFailure> failures)
|
|
: this()
|
|
{
|
|
// TODO: Make serialized dictionary look more like this
|
|
// "errors": {
|
|
// "viewModel": [
|
|
// "The viewModel field is required."
|
|
// ],
|
|
// "$.addresses[0].order": [
|
|
// "The JSON value could not be converted to System.Int16. Path: $.addresses[0].order | LineNumber: 5 | BytePositionInLine: 26."
|
|
// ]
|
|
// },
|
|
|
|
Errors = failures
|
|
.GroupBy(f => f.PropertyName, f => f.ErrorMessage)
|
|
.ToDictionary(fg => fg.Key, fg => fg.ToArray());
|
|
}
|
|
|
|
public IDictionary<string, string[]> Errors { get; }
|
|
}
|
|
|