CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
DataPortalExceptionHandler.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DataPortalExceptionHandler.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>This class provides a hoook for developers to add custom error handling in the DataPortal. </summary>
7//-----------------------------------------------------------------------
8using System;
10using Csla.Properties;
11using Csla.Reflection;
12
13namespace Csla.Server
14{
15
23 {
24 #region Custom Exception Inspector Loader
25
26 private static string _datePortalInspecorName;
27
31 public static string ExceptionInspector
32 {
33 get
34 {
35 if (_datePortalInspecorName == null)
36 {
37 string setting = ConfigurationManager.AppSettings["CslaDataPortalExceptionInspector"];
38 if (!string.IsNullOrEmpty(setting))
39 _datePortalInspecorName = setting;
40 }
41 return _datePortalInspecorName;
42 }
43 set
44 {
45 _datePortalInspecorName = value;
46 }
47 }
48
49
54 private static IDataPortalExceptionInspector GetExceptionInspector()
55 {
56 if (string.IsNullOrEmpty(ExceptionInspector)) return null;
57
58#if NETFX_CORE
59 var t = Type.GetType(ExceptionInspector);
60#else
61 var t = Type.GetType(ExceptionInspector, true, true);
62#endif
63 return (IDataPortalExceptionInspector)Reflection.MethodCaller.CreateInstance(t);
64
65 }
66 #endregion
67
68 #region DataPortal Exception
69
78 public Exception InspectException(Type objectType, object criteria, string methodName, Exception ex)
79 {
80 Exception handledException;
81 if (ex is CallMethodException)
82 {
83 if (CallExceptionInspector(objectType, null, criteria, methodName, ex.InnerException, out handledException))
84 {
85 ex = new CallMethodException(methodName + " " + Resources.MethodCallFailed, handledException);
86 }
87 }
88 else
89 {
90 if (CallExceptionInspector(objectType, null, criteria, methodName, ex, out handledException))
91 {
92 ex = handledException;
93 }
94 }
95
96 return ex;
97 }
98
108 public Exception InspectException(Type objectType, object businessObject, object criteria, string methodName, Exception ex)
109 {
110 // The exception as parameter is always a CallMethodException containing the business exception as Inner exception
111 Exception handledException;
112 if (ex is CallMethodException)
113 {
114 if (CallExceptionInspector(objectType, businessObject, criteria, methodName, ex.InnerException, out handledException))
115 {
116 // developer should only transform and if rethrows a new we will wrap as new CallMethodException
117 ex = new Csla.Reflection.CallMethodException(methodName + " " + Resources.MethodCallFailed, handledException);
118 }
119 }
120 else
121 {
122 if (CallExceptionInspector(objectType, null, criteria, methodName, ex, out handledException))
123 {
124 ex = handledException;
125 }
126 }
127 return ex;
128 }
129
142 private bool CallExceptionInspector(Type objectType, object businessObject, object criteria, string methodName, Exception exception, out Exception handledException)
143 {
144 handledException = null;
145 var inspector = GetExceptionInspector();
146 // if no inspector is defined then just return false
147 if (inspector == null) return false;
148
149 try
150 {
151 // This method should rethrow a new exception to be handled
152 inspector.InspectException(objectType, businessObject, criteria, methodName, exception);
153 }
154 catch (Exception ex)
155 {
156 handledException = ex;
157 return true; // new exception returned
158 }
159 return false; // exception was not handled
160 }
161
162 #endregion
163 }
164}
A strongly-typed resource class, for looking up localized strings, etc.
static string MethodCallFailed
Looks up a localized string similar to method call failed.
This exception is returned from the CallMethod method in the server-side DataPortal and contains the ...
This class provides a hoook for developers to add custom error handling in the DataPortal.
static string ExceptionInspector
Gets or sets the fully qualified name of the ExceptionInspector class.
Exception InspectException(Type objectType, object businessObject, object criteria, string methodName, Exception ex)
Transforms the exception in a Update, Delete method
Exception InspectException(Type objectType, object criteria, string methodName, Exception ex)
Transforms the exception in a Fetch, Create or Execute method.
Implement this interface to check a DataPortalException before returning Exception to the client.