forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationError.cs
More file actions
134 lines (114 loc) · 5.1 KB
/
ValidationError.cs
File metadata and controls
134 lines (114 loc) · 5.1 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Text;
using ServiceStack.Model;
using ServiceStack.Text;
namespace ServiceStack.Validation
{
/// <summary>
/// The exception which is thrown when a validation error occurred.
/// This validation is serialized in a extra clean and human-readable way by ServiceStack.
/// </summary>
public class ValidationError : ArgumentException, IResponseStatusConvertible
{
public string ErrorMessage { get; }
public ValidationError(string errorCode)
: this(errorCode, errorCode.SplitCamelCase())
{
}
public ValidationError(ValidationErrorResult validationResult)
: base(validationResult.ErrorMessage)
{
this.ErrorCode = validationResult.ErrorCode;
this.ErrorMessage = validationResult.ErrorMessage;
this.Violations = validationResult.Errors;
}
public ValidationError(ValidationErrorField validationError)
: this(validationError.ErrorCode, validationError.ErrorMessage)
{
this.Violations.Add(validationError);
}
public ValidationError(string errorCode, string errorMessage)
: base(errorMessage)
{
this.ErrorCode = errorCode;
this.ErrorMessage = errorMessage;
this.Violations = new List<ValidationErrorField>();
}
/// <summary>
/// Returns the first error code
/// </summary>
/// <value>The error code.</value>
public string ErrorCode { get; }
public override string Message
{
get
{
//If there is only 1 validation error than we just show the error message
if (this.Violations.Count == 0)
return this.ErrorMessage;
if (this.Violations.Count == 1)
return this.ErrorMessage ?? this.Violations[0].ErrorMessage;
var sb = StringBuilderCache.Allocate()
.Append(this.ErrorMessage).AppendLine();
foreach (var error in this.Violations)
{
if (!string.IsNullOrEmpty(error.ErrorMessage))
{
var fieldLabel = error.FieldName != null ? $" [{error.FieldName}]" : null;
sb.Append($"\n - {error.ErrorMessage}{fieldLabel}");
}
else
{
var fieldLabel = error.FieldName != null ? ": " + error.FieldName : null;
sb.Append($"\n - {error.ErrorCode}{fieldLabel}");
}
}
return StringBuilderCache.ReturnAndFree(sb);
}
}
public IList<ValidationErrorField> Violations { get; private set; }
/// <summary>
/// Used if we need to serialize this exception to XML
/// </summary>
/// <returns></returns>
public string ToXml()
{
var sb = StringBuilderCache.Allocate();
sb.Append("<ValidationException>");
foreach (var error in this.Violations)
{
sb.Append("<ValidationError>")
.Append($"<Code>{error.ErrorCode}</Code>")
.Append($"<Field>{error.FieldName}</Field>")
.Append($"<Message>{error.ErrorMessage}</Message>")
.Append("</ValidationError>");
}
sb.Append("</ValidationException>");
return StringBuilderCache.ReturnAndFree(sb);
}
public static ValidationError CreateException(Enum errorCode) =>
new ValidationError(errorCode.ToString());
public static ValidationError CreateException(Enum errorCode, string errorMessage) =>
new ValidationError(errorCode.ToString(), errorMessage);
public static ValidationError CreateException(Enum errorCode, string errorMessage, string fieldName) =>
CreateException(errorCode.ToString(), errorMessage, fieldName);
public static ValidationError CreateException(string errorCode) => new ValidationError(errorCode);
public static ValidationError CreateException(string errorCode, string errorMessage) => new ValidationError(errorCode, errorMessage);
public static ValidationError CreateException(string errorCode, string errorMessage, string fieldName)
{
var error = new ValidationErrorField(errorCode, fieldName, errorMessage);
return new ValidationError(new ValidationErrorResult(new List<ValidationErrorField> { error }));
}
public static ValidationError CreateException(ValidationErrorField error) => new ValidationError(error);
public static void ThrowIfNotValid(ValidationErrorResult validationResult)
{
if (!validationResult.IsValid)
{
throw new ValidationError(validationResult);
}
}
public ResponseStatus ToResponseStatus() =>
ResponseStatusUtils.CreateResponseStatus(ErrorCode, Message, Violations);
}
}