2

I'm trying to have a C# exception caught clientside, currently we have a section where a logged in user gets a delete link, if clicked we do a check to make sure the user is still logged in before deleting. if not logged in anymore, the code throws and exception.

        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    [WebMethod]
    public static void DeleteComment(Guid commentId)
    {
        try
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                UserEntity currentUser = ((UserEntity)Membership.GetUser());
                .. do work
            }
            else
            {
                throw new HttpException(401, "401");
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

this error goes to the javascript code, which then is checked:

if (result.responseJSON.Message == "401") {

this works fine in our local environment, the Message field contains 401 and the result.responseJSON.ExceptionType contains "System.Web.HttpException", but when trying in our staging environment we get a 500 error code instead and the ExceptionType is ""

I've tried remote dugging and I see the exception occurring when not logged in, it's thrown but when it hits the client side code it's always 500.

Paritosh
  • 4,243
  • 7
  • 47
  • 80
  • 1
    You could probably just return the error in the response status code, [like in this post](http://stackoverflow.com/questions/5649852/asp-net-web-service-i-would-like-to-return-error-403-forbidden). – MikeSmithDev Nov 12 '13 at 19:31
  • Here is a discussion of this issue, along with some ideas for workarounds: http://stackoverflow.com/questions/5612970/throwing-an-httpexception-always-sends-back-http-500-error – mikey Nov 12 '13 at 19:36
  • I tried @MikeSmithDev link and that worked, still not sure why what I had would not work outside of our local machines, but regardless the method used in the link worked on both machines. – Paritosh Nov 12 '13 at 19:48
  • You wouldn't get anything outside because no matter what happens, you are returning void from your function. – RealityDysfunction Nov 12 '13 at 21:56

0 Answers0