PDA

View Full Version : [CLOSED] DirectMethod error wrapping



Leonid_Veriga
May 16, 2013, 3:39 PM
Hello.

I need an opportunity to wrap each exception thrown form direct methods on server-side. I would like generate a unique key for each exception and return this info to the client. Could you suggest how it could be done, without wrapping each direct method on our application? Is there an opportunity to provide custom error handler on server side?

Best regards.

Vladimir
May 16, 2013, 7:18 PM
If an exception was thrown in a direct method then the following properties will contain false and message values


Ext.Net.ResourceManager.AjaxSuccess
Ext.Net.ResourceManager.AjaxErrorMessage


You can modify message property as required at PreRender event (for example, add special prefix to AjaxErrorMessage and get that prefix in the failure handler)

For example


<%@ Page Language="C#" %>


<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>


<script runat="server">
[DirectMethod]
public void ExceptionMethod()
{
throw new Exception("Exception message");
}


protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);


if (!ResourceManager.AjaxSuccess)
{
ResourceManager.AjaxErrorMessage = "" + Guid.NewGuid() + "" + ResourceManager.AjaxErrorMessage;
}
}
</script>


<!DOCTYPE html>


<html>
<head runat="server">
<title>Ext.NET v2 Example</title>


<script type="text/javascript">
function checkMessage(msg) {
var match = msg && msg.match(/\[GUID\](.+?)\[\/GUID\]/);
if (match) {
alert(match[1]);
}
}
</script>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server">
<Listeners>
<AjaxRequestException Handler="checkMessage(result.errorMessage);" />
</Listeners>
</ext:ResourceManager>


<ext:Button runat="server" Text="Click" Handler="App.direct.ExceptionMethod();" />
</form>
</body>
</html>

vadym.f
May 16, 2013, 8:31 PM
We're leveraging PostSharp AOP framework postsharp.net to handle cross-cutting concerns like logging or exception wrapping/shielding of various contexts in a centralized way using custom Aspects. You can look at their examples at http://doc.postsharp.net/postsharp-3.0/##PostSharp-3.0.chm/html/2821bc3a-9e7a-499c-a9a3-a9f4dcf156e3.htm and http://doc.postsharp.net/postsharp-3.0/##PostSharp-3.0.chm/html/952d27f4-3322-4aac-b1be-b3929e245de3.htm. This framework is robust, easy to learn and use and fairly light compared to MS EntLib. Let me know if you'd like more details.

Leonid_Veriga
Mar 31, 2014, 1:23 PM
Hello.

How can I log the exceptions in the static direct methods?


<script runat="server">
[DirectMethod]
public static void ExceptionMethod()
{
throw new Exception("Exception message");
}
</script>

Daniil
Apr 01, 2014, 1:27 AM
I am afraid there is no simple way to catch it globally. There is the same problem with page methods:
http://stackoverflow.com/questions/785585/server-side-asp-net-ajax-exception-handling

Daniil
Apr 01, 2014, 3:48 AM
Maybe

[DirectMethod(RethrowException=true)]
public static void ExceptionStaticMethod()
and listen to Application_Error in Global.asax.

Leonid_Veriga
Apr 03, 2014, 5:46 AM
Thank you for your suggestion.

Daniil
Apr 03, 2014, 2:22 PM
So, does it work for you well?