Getting Exception Details from ASP.NET PageMethods on the Client SIde

SudheerKovalam picture SudheerKovalam · Jun 8, 2009 · Viewed 10.1k times · Source

I am having a pagemethod to which i give a call from my JavaScript say

Pagemethods.MyMethod(MyParameter, onsucess, onfailure);

In the Code behind, I have something like this:

[WebMethod]
public static void MyMethod(Param)
{
   try{
     //DoSomething..
   }
   catch(exception ex)
   {
      //Log the exception and rethrow
      throw new exception(ex.innerexception);
   }
}

Now the issue i face is :

Whenever i do get an exception, i re throw the exception from code behind

But in the onfailure method, i just get a generic message saying that "the server method MyMethod failed with following error: "

I don't seem to get the exception details and only that generic exception,

How can i get the exception details on JavaScript, in order to handle it according on the UI/JavaScript side.

I have verified, that it is not an issue with custom errors settings in web.config.

Can some one enlighten to me as to what is happening here?

PS: i have stepped through each and every line of code and the exception after being logged is rethrown with the proper exception details i.e. message.

Answer

Tom Carter picture Tom Carter · Jun 8, 2009

As far as I understand as long as you have

<customErrors mode="off" />

in your web.config, the message will be returned to client. Are you sure you have this setting ?

To display the message associated with error you need to have oassed the name of the function as the third parameter of the page method call : this function could be as simple as:

function onfailure( result )
{
   alert( result.get_message() );
}

That's what we have and it works OK