title: Best way to catch exception from rest service call using ajax
body: <p>So I'm calling rest services using ajax:  </p>
  
  <pre><code>$("#loginsubmit").on('click', function () {
      showtooltip('Please wait...', false);
      $('#loginsubmit').attr("disabled", true);
      $.ajax({
          url: '@Utility.ConstructRestURL("Authenticate/Login")',
          type: 'POST',
          contentType: 'application/json',
          data: JSON.stringify({
              client: {
                  SessionID: '@Utility.GetSession(Utility.GetConfigString("ClientSessionID"))'
              },
              loginfo: {
                  u: $("#loginusername").val(),
                  p: $("#loginpassword").val()
              }
          }),
          success: function (response) {
              if (response.LoginResult.SessionID != null || response.LoginResult.UserID != null || response.LoginResult.SourceIP != null) {
                  $.post('@Url.Action("SaveSession", "Auth")', response.LoginResult, function (msg) {
                      window.location.replace(msg.url);
                  });
              } else {
                  showtooltip('Login failed.', false);
                  $('#loginsubmit').removeAttr("disabled");
              }
          },
          error: function (xhr, ajaxOptions, error) {
              showtooltip(xhr.status + ': ' + error, false);
              $('#loginsubmit').removeAttr("disabled");
          }
      });
  });  
  </code></pre>
  
  <p>And most of the services returns a class object:  </p>
  
  <pre><code>    [WebInvoke(UriTemplate = "Login", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
      public CModel.DTO.UserSessionDTO Login(CModel.Entities.ClientAuthentication client, CModel.DTO.LoginCredentials loginfo)
      {
          loginfo.i = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
          CModel.Logic.IAuthenticate svc = CModel.Logic.ConfigAndResourceComponent.Container().Resolve&lt;CModel.Logic.IAuthenticate&gt;();
          CModel.Entities.UserSession session = svc.Login(client, loginfo);
          CModel.DTO.UserSessionDTO copied = Mapper.Map&lt;CModel.Entities.UserSession, CModel.DTO.UserSessionDTO&gt;(session);
          return copied;
      }  
  
  public class UserSessionDTO
  {
      public string SourceIP { get; set; }
      public string SessionID { get; set; }
      public long UserID { get; set; }
  }  
  </code></pre>
  
  <p>When an exception occurred:  </p>
  
  <p><img src="https://i.stack.imgur.com/VtpJP.png" alt="enter image description here"></p>
  
  <p>It displays a "<code>400: Bad request</code>" error on the client side(returned error from ajax).<br>
  What I wanted for ajax to display is the exception message "InvalidLogin".  </p>
  
  <p>My solution is this, but yet I don't know how to do it.  </p>
  
  <p>When an exception is thrown, the rest-project will call some method that will construct the error message, and the constructed message will be the return value.  </p>
  
  <p>Or if you have the better idea, please share.</p>