forked from MarimerLLC/csla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataPortalException.cs
210 lines (196 loc) · 7.29 KB
/
DataPortalException.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//-----------------------------------------------------------------------
// <copyright file="DataPortalException.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>This exception is returned for any errors occuring</summary>
//-----------------------------------------------------------------------
using System;
#if !NETFX_CORE
using System.Security.Permissions;
#endif
namespace Csla
{
/// <summary>
/// This exception is returned for any errors occurring
/// during the server-side DataPortal invocation.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors")]
[Serializable]
public class DataPortalException : Exception
{
/// <summary>
/// Creates an instance of the object.
/// </summary>
/// <param name="message">Text describing the exception.</param>
/// <param name="businessObject">The business object
/// as it was at the time of the exception.</param>
public DataPortalException(string message, object businessObject)
: base(message)
{
_innerStackTrace = String.Empty;
_businessObject = businessObject;
}
/// <summary>
/// Creates an instance of the object.
/// </summary>
/// <param name="message">Text describing the exception.</param>
/// <param name="ex">Inner exception.</param>
/// <param name="businessObject">The business object
/// as it was at the time of the exception.</param>
public DataPortalException(string message, Exception ex, object businessObject)
: base(message, ex)
{
_innerStackTrace = ex.StackTrace;
_businessObject = businessObject;
}
/// <summary>
/// Creates an instance of the object.
/// </summary>
/// <param name="message">
/// Exception message.
/// </param>
/// <param name="ex">
/// Inner exception.
/// </param>
public DataPortalException(string message, Exception ex)
: base(message, ex)
{
_innerStackTrace = ex.StackTrace;
}
#if !NETFX_PHONE || PCL46 || PCL259
#if !NETCORE && !PCL46 && !ANDROID && !NETSTANDARD2_0 && !PCL259
/// <summary>
/// Creates an instance of the object.
/// </summary>
/// <param name="info">Info about the exception.</param>
public DataPortalException(WcfPortal.WcfErrorInfo info)
: base(info.Message)
{
this.ErrorInfo = new Csla.Server.Hosts.HttpChannel.HttpErrorInfo(info);
}
#endif
/// <summary>
/// Creates an instance of the object.
/// </summary>
/// <param name="info">Info about the exception.</param>
public DataPortalException(Csla.Server.Hosts.HttpChannel.HttpErrorInfo info)
: base(info.Message)
{
this.ErrorInfo = info;
}
/// <summary>
/// Gets a string representation
/// of this object.
/// </summary>
public override string ToString()
{
var sb = new System.Text.StringBuilder();
sb.AppendLine(base.ToString());
if (ErrorInfo != null)
{
sb.AppendLine("------------------------------");
var error = this.ErrorInfo;
while (error != null)
{
sb.AppendFormat("{0}: {1}", error.ExceptionTypeName, error.Message);
sb.Append(Environment.NewLine);
sb.Append(error.StackTrace);
sb.Append(Environment.NewLine);
error = error.InnerError;
}
}
return sb.ToString();
}
/// <summary>
/// Gets information about the original
/// server-side exception. That exception
/// is not an exception on the client side,
/// but this property returns information
/// about the exception.
/// </summary>
public Csla.Server.Hosts.HttpChannel.HttpErrorInfo ErrorInfo { get; private set; }
#endif
private object _businessObject;
private string _innerStackTrace;
/// <summary>
/// Returns a reference to the business object
/// from the server-side DataPortal.
/// </summary>
/// <remarks>
/// Remember that this object may be in an invalid
/// or undefined state. This is the business object
/// (and any child objects) as it existed when the
/// exception occured on the server. Thus the object
/// state may have been altered by the server and
/// may no longer reflect data in the database.
/// </remarks>
public object BusinessObject
{
get { return _businessObject; }
}
/// <summary>
/// Gets the original server-side exception.
/// </summary>
/// <returns>An exception object.</returns>
/// <remarks>
/// When an exception occurs in business code behind
/// the data portal, it is wrapped in a
/// <see cref="Csla.Server.DataPortalException"/>, which
/// is then wrapped in a
/// <see cref="Csla.DataPortalException"/>. This property
/// unwraps and returns the original exception
/// thrown by the business code on the server.
/// </remarks>
public Exception BusinessException
{
get
{
var result = this.InnerException;
if (result != null)
result = result.InnerException;
if (result is DataPortalException dpe && dpe.InnerException != null)
result = dpe.InnerException;
if (result is Csla.Reflection.CallMethodException cme && cme.InnerException != null)
result = cme.InnerException;
return result;
}
}
/// <summary>
/// Get the combined stack trace from the server
/// and client.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object,System.Object)")]
public override string StackTrace
{
get { return String.Format("{0}{1}{2}", _innerStackTrace, Environment.NewLine, base.StackTrace); }
}
#if !(ANDROID || IOS) && !NETFX_CORE && !NETSTANDARD2_0
/// <summary>
/// Creates an instance of the object for serialization.
/// </summary>
/// <param name="info">Serialiation info object.</param>
/// <param name="context">Serialization context object.</param>
protected DataPortalException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
: base(info, context)
{
_businessObject = info.GetValue("_businessObject", typeof(object));
_innerStackTrace = info.GetString("_innerStackTrace");
}
/// <summary>
/// Serializes the object.
/// </summary>
/// <param name="info">Serialiation info object.</param>
/// <param name="context">Serialization context object.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.SerializationFormatter)]
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("_businessObject", _businessObject);
info.AddValue("_innerStackTrace", _innerStackTrace);
}
#endif
}
}