Web Service Return Custom Error Code

Last post 05-10-2008 11:36 AM by mrmercury. 5 replies.

Sort Posts:

  • Web Service Return Custom Error Code

    05-09-2008, 2:10 PM
    • Loading...
    • rjbdetroit
    • Joined on 04-28-2008, 11:29 AM
    • Posts 94

    I'm trying to write a Web Service that will return several different error codes so the client can handle each error code individually (e.g. err=600 call funcA, err=700 call funcB, etc..).

    My code below always returns "500" for the _statusCode (or get_statusCode). Am I doing this wrong? How do I get a Web Service to return custom error codes to the client (javascript)? Any info appreciated. Thanks.

    Try

    If IsNothing(Membership.GetUser(UserName.ToString.Trim)) Then

    objReturnValue = New UserInfo(UserName.ToString.Trim, False)

    Else

    objReturnValue = Nothing

    Err.Raise(vbObjectError + 601, "UserExists", UserName)

    End If

    Catch ex As Exception When Err.Number = 601 'User Exists

    objReturnValue = Nothing

    Throw New System.Web.HttpException(601, UserName)

    Err.Raise(vbObjectError + 601, "Membership.UserExists", UserName)

    Catch ex As Exception

    objReturnValue = Nothing

    Err.Raise(vbObjectError + 602, "Membership.UserExists", ex.Message)

    End Try

  • Re: Web Service Return Custom Error Code

    05-09-2008, 2:27 PM
    • Loading...
    • mrmercury
    • Joined on 04-04-2006, 2:26 PM
    • Mexico City, Mexico
    • Posts 319

    For this to work you need to set the customErrors tag to off in the web.config file.

    If this post helped you please remember to set it as Answer so it can help others.
  • Re: Web Service Return Custom Error Code

    05-09-2008, 3:04 PM
    • Loading...
    • rjbdetroit
    • Joined on 04-28-2008, 11:29 AM
    • Posts 94

    Thanks for the response. I tried your suggestion but it didn't work. Javascript still returns a 500 error code regardless of the error I raise in my web service:

     -  error {...} Object
    +  get_exceptionType {...} Object
    +  get_stackTrace {...} Object
    +  get_message {...} Object
    +  get_statusCode {...} Object
    +  get_timedOut {...} Object
      _timedOut false Boolean
      _message "aaa" String
      _stackTrace "   at Microsoft.VisualBasic.ErrObject.Raise(Int32 Number, Object Source, Object Description, Object HelpFile, Object HelpContext)
       at WebService.UserExists(String UserName) in c:\inetpub\wwwroot\App\App_Code\WebService.vb:line 271" String
      _exceptionType "System.Exception" String
      _statusCode 500 Long
    +  userContext {...} Object
      methodName "UserExists" String

  • Re: Web Service Return Custom Error Code

    05-10-2008, 9:43 AM
    • Loading...
    • mrmercury
    • Joined on 04-04-2006, 2:26 PM
    • Mexico City, Mexico
    • Posts 319

    Are you sure the call is getting to the server? You shouldn’t be getting a 500 error.

    If this post helped you please remember to set it as Answer so it can help others.
  • Re: Web Service Return Custom Error Code

    05-10-2008, 9:48 AM
    • Loading...
    • rjbdetroit
    • Joined on 04-28-2008, 11:29 AM
    • Posts 94

    mr - Thanks for your response. Yes, in debug mode, I see the web service returning the error code I'm expecting, but when I get back to the Javascript debug window, the return code is 500. Very odd...

  • Re: Web Service Return Custom Error Code

    05-10-2008, 11:36 AM
    Answer
    • Loading...
    • mrmercury
    • Joined on 04-04-2006, 2:26 PM
    • Mexico City, Mexico
    • Posts 319
    One more thing: are you using a HTTPModule, for example for compressing your webmethod's results? If so it could be setting the “Content-Encoding” too early and it’s getting lost, it should be set in the PreSendRequestHeaders event handler.
    If this post helped you please remember to set it as Answer so it can help others.
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter