|
|
|
Community Member
|
|
Hi
Is there a way to modify original exception in OnException method?? My requirement is that we have an UI layer which uses a business layer to make calls to webservices/databases for getting data. Business layer uses postsharp to have tracing and exception handling done at a common place. UI layer only understands predefined buisness application exceptions so in business layer we map original exception thrown by databases/webservices to required application exception. Since this code for exception handling and mapping is repeated everywhere we wanted to leverage OnException method of OnMethodBoundaryAspect for doing it. I have something like this
public override void OnException(MethodExecutionEventArgs eventArgs) { IContext currentContext = eventArgs.GetReadOnlyArgumentArray()[0] as IContext; if (currentContext != null) { // log the real exception currentContext.Logger.Write("Real Exception : ", eventArgs.Exception); // my mapping logic to application exception goes here // .. // now create a new application excpetion for UI layer eventArgs.Exception = new Exception("my new updated exception for UI layer"); } }
but in UI layer I am not getting the updated exception, instead I get the real root exception which was thrown by database/webservice.. I looked in the generated code in my assembly and found that PostSharp does a throw of original exception. Is there a way we can alter this flow behaviour and throw MethodExecutionEventArgs.Exception? I guess introducing a new flag in FlowBehaviour say FlowBehaviour.ThrowCustomException and use it in existing switch statement to throw MethodExecutionEventArgs.Exception will solve our issue.
Is there a way to achieve this in 1.5 RTM version of PostSharp?? Is there any alternative available which I can levergae to solve my problem??
~Prem
|
|
|
|
|
Gael Fraiteur
SharpCrafters
|
|
Hi Prem,
Thank you for your suggestion.
Currently, the only way to replace an exception is to throw an exception from the OnException advice, which has of course the side effect to replace the call stack and to add some stuff on the stack trace.
-gael
|
|
|
|