6

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
SudheerKovalam
  • 628
  • 2
  • 7
  • 13
  • 2
    Your exception handling is very bad. After logging the exception, just use `throw;`. Your code is destroying the stack trace, and losing exception details. – John Saunders Jul 31 '09 at 04:33

2 Answers2

6

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

Tom Carter
  • 2,938
  • 1
  • 27
  • 42
  • Yes i do have custom errors mode "off" But wht i dont get is why do even get that generic message "the server method MyMethod failed with following error: " and only that much nothing in error details. – SudheerKovalam Jun 08 '09 at 13:01
  • What code do you have in your javascript onfailure function ? Do you use get_message to extract the 'message' ? – Tom Carter Jun 08 '09 at 13:22
  • this is how my onfailure method looks like function onCallFailure(ex, userContext, methodName){ alert(ex.get_message());} This is where i see that generic error message i said i get – SudheerKovalam Jun 08 '09 at 14:14
  • my app does have a master page this master page has the script manager with page methods enabled. In one of the pages which use the above mentioned master page, there is a page method call to MyMethod. – SudheerKovalam Jun 08 '09 at 14:36
  • what gets returned from get_statusCode() and get_exceptionType() ? – Tom Carter Jun 08 '09 at 14:47
  • get_statusCode() returns 500 get_exceptionType returns empty string get_message() returns the generic message i am saying – SudheerKovalam Jun 08 '09 at 15:10
  • Were you ever able to figure this out? I have the same issue and customerrors mode set to off doesn't help. – Justin Sep 19 '10 at 15:15
1

After you have set <customErrors mode="off" /> in the <system.web> section of the web.config, try these functions to get all of the exception details from the PageMethod's onfailure:

function onfailure(error) {                
    alert("Error: " + error.get_message() + "; " + 
        "Stack Trace: " + error.get_stackTrace() + "; " + 
        "Status Code: " + error.get_statusCode() + "; " + 
        "Exception Type: " + error.get_exceptionType() + "; " + 
        "Timed Out: " + error.get_timedOut());
}

I've found the error's status code to be far more helpful than the error message itself.

DanM7
  • 2,203
  • 3
  • 28
  • 46