-
Notifications
You must be signed in to change notification settings - Fork 0
/
BusinessException.cs
executable file
·104 lines (91 loc) · 4.42 KB
/
BusinessException.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Model.DTO;
using Model.Resources;
namespace Model.Exceptions
{
public class BusinessException : Exception
{
#region Constructors
/// <summary>
/// Constructor
/// </summary>
/// <param name="resourceKey">ErrorMessageKeys enum with user friendly error message</param>
/// <param name="errorLogMessage">String containing technical error message</param>
/// <param name="additionalContent">List of strings with additional error information</param>
/// <param name="innerException">Inner exception</param>
///
public BusinessException(ErrorMessageKeys resourceKey, string errorLogMessage, List<string> additionalContent = null, Exception innerException = null)
: base(errorLogMessage, innerException)
{
base.Data.Add("ResourceKey", resourceKey);
base.Data.Add("AdditionalContent", additionalContent);
base.Data.Add("ErrorItems", null);
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="resourceKey">ErrorMessageKeys enum with user friendly error message</param>
/// <param name="errorItems">String with technical error message. (Obtained form ModelState.GetErrorMessage method.)</param>
/// <param name="innerException">Inner exception</param>
///
public BusinessException(ErrorMessageKeys resourceKey, IEnumerable<ErrorItem> errorItems, Exception innerException = null)
: base(GetTechnicalMessage(errorItems), innerException)
{
base.Data.Add("ResourceKey", resourceKey);
base.Data.Add("AdditionalContent", null);
base.Data.Add("ErrorItems", errorItems);
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="resourceKey">ErrorMessageKeys enum with user friendly error message</param>
/// <param name="errorItems">String with technical error message. (Obtained form ModelState.GetErrorMessage method.)</param>
/// <param name="additionalContent">List of strings with additional error information</param>
/// <param name="innerException">Inner exception</param>
///
public BusinessException(ErrorMessageKeys resourceKey, IEnumerable<ErrorItem> errorItems, IEnumerable<string> additionalContent = null, Exception innerException = null)
: base(GetTechnicalMessage(errorItems, additionalContent), innerException)
{
base.Data.Add("ResourceKey", resourceKey);
base.Data.Add("AdditionalContent", additionalContent);
base.Data.Add("ErrorItems", errorItems);
}
#endregion Constructors
#region Public Methods
public ErrorMessageKeys GetErrorMessageKeys()
{
return (ErrorMessageKeys)base.Data["ResourceKey"];
}
#endregion
#region Private Methods
/// <summary>
/// Generates the technical error messages.
/// </summary>
/// <param name="errorItems">List of errors returned from ValidationAttribute objects.</param>
/// <returns>String containing the derived technical error message</returns>
///
private static string GetTechnicalMessage(IEnumerable<ErrorItem> errorItems)
{
// Concatenate all the error messages into a string.
return errorItems.Aggregate(string.Empty, (current, errorItem) => current + (errorItem.Message + " "));
}
/// <summary>
/// Concatenate the technical error messages
/// </summary>
/// <param name="errorItems">List of ErrorItem objects</param>
/// <param name="additonalContent">List of string objects</param>
///
private static string GetTechnicalMessage(IEnumerable<ErrorItem> errorItems, IEnumerable<string> additonalContent)
{
var technicalMessage = string.Empty;
if (errorItems != null)
technicalMessage = errorItems.Aggregate(technicalMessage, (current, item) => current + (item + " : "));
if (additonalContent != null)
technicalMessage = additonalContent.Aggregate(technicalMessage, (current, content) => current + (content + " : "));
return technicalMessage.Substring(0, technicalMessage.LastIndexOf(':') - 1);
}
#endregion Private Methods
}
}