This is a migrated thread and some comments may be shown as answers.

Web Services Error CallBack

1 Answer 93 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Feizal Amlani
Top achievements
Rank 1
Feizal Amlani asked on 03 Nov 2008, 04:35 PM
Hello,

I have a function to call a webservice method like so.
WS_Service.LookUp(myParam, Success, Failure);

In the WebService call I am throwing a Custom Exception and setting the Message property to a friendly message.  For example, "You have an Invalid ID"

My Failure Javascript function does get excecuted.  However, when I try to read the Message Property from the exception that was thrown, I have lost the friendly message that I have set in the origional call.  There must be some kind of conversion going on and wondering how to send a Message Property back to the Failure function.

function Failure(objErr){

    SetFriendlyMessage(objErr.message);
}

Thanks.

1 Answer, 1 is accepted

Sort by
0
Kevin Babcock
Top achievements
Rank 1
answered on 04 Nov 2008, 03:28 PM
Hello Feizal,

In order to retrieve the exception message, you must invoke the object's get_message() function. Alternatively (and not considered a best practice), you can access the message value directly by calling error._message. Here is a simple example:

using System; 
 
namespace KevinBabcock.Examples 
    public class CustomException : Exception 
    { 
        public CustomException(string message) 
            : base(message) 
        { } 
    } 
 

using System; 
using System.ComponentModel; 
using System.Drawing.Design; 
using System.Web.Script.Services; 
using System.Web.Services; 
 
namespace KevinBabcock.Examples 
    [WebService(Namespace = "http://telerik.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [ToolboxItem(false)] 
    [ScriptService] 
    public class TestService : WebService 
    { 
        [WebMethod] 
        public string GetData() 
        { 
            throw new CustomException("my custom exception"); 
        } 
    } 
 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="KevinBabcock.Examples._Default" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"
    <title>Example - Web Service Error Callback</title> 
    <script type="text/javascript"
        function invokeService() { 
            var service = new KevinBabcock.Examples.TestService(); 
            service.GetData(onCompleted, onError); 
            return false; 
        } 
 
        function onCompleted(result) { 
            alert('Error. We should never reach this code.'); 
        } 
 
        function onError(error) { 
            var message = error.get_message(); 
            alert(message); 
        } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"
        <asp:ScriptManager ID="ScriptManager1" runat="server"
            <Services> 
                <asp:ServiceReference Path="~/TestService.asmx" /> 
            </Services> 
        </asp:ScriptManager>     
 
        <asp:Button ID="Button1" runat="server" 
            OnClientClick="return invokeService();" 
            Text="Invoke Service" /> 
 
    </form> 
</body> 
</html> 
 

I hope this helps. Let me know if you have any further questions.

Sincerely,
Kevin Babcock
Tags
Ajax
Asked by
Feizal Amlani
Top achievements
Rank 1
Answers by
Kevin Babcock
Top achievements
Rank 1
Share this question
or