0

Lets say I have a check constraint that is not respected when adding a value to a database column or lets says I have a trigger that uses RAISEERROR with a custom message.

In my WebMethod, I use SqlException to catch any error while updating the database. Hence I should be able to retrieve the automatic SQL Server error message if the check constraint is not respected or my custom message if the trigger raises the error message right ?

How do I do that ?

Below is what I have tried :

[WebMethod]
public string AddData(entity e)
{
   using(var context = new DB_ENTITY_DbContext())
   {
       try
       {
           context.entity.Add(e);
           context.SaveChanges();
           return "success";
       }
       catch(SqlException ex)
       {
           return "My custom message " + ex.Message;
       }
   }
}

Javascript code

function AddData()
{
    $.ajax({

    // Some other settings
    error: function (xhr, status, error){
            alert(JSON.parse(xhr.responseText).Message);
            alert(error);
        }    
    });
}

Here the error does not return

"My Custom message .....(and some auto generated message)"....

It returns "Internal server error" ..

Any idea of what I should do ?

Kurt Miller
  • 567
  • 1
  • 8
  • 23

2 Answers2

3

That's because you're catching the exception, and returning a simple string. Your WebMethod, in theory, never fails... (it fails, but you instruct the WebMethod to never inform about the exception). That's why your ajax call never reaches the error function.

Is a bad idea to encapsulate and hide errors by catching the exceptions. Encapsulate and throw a different Exception, or just simply remove the try/catch block

jparaya
  • 1,331
  • 11
  • 15
1

Controller:

public class ClientErrorHandler : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            var response = filterContext.RequestContext.HttpContext.Response;
            response.Write(filterContext.Exception.Message);
            response.ContentType = MediaTypeNames.Text.Plain;
            filterContext.ExceptionHandled = true;
        }
    }

    [ClientErrorHandler]
    public class SomeController : Controller
    {
        [HttpPost]
        public ActionResult SomeAction()
        {
            throw new Exception("Error message");
        }
    }

View script:

$.ajax({
    type: "post", url: "/SomeController/SomeAction",
    success: function (data, text) {
        //...
    },
    error: function (request, status, error) {
        alert(request.responseText);
    }
});
Zaheer Ul Hassan
  • 771
  • 9
  • 24
  • Sorry but I'm a bit new to this technology... I'll add all this in the Code Behind of my webform. ; `ClientErrorHandler ` will be a class in the code behind of the webform ; `SomeController`is actually the partial class of my webform and `SomeAction` is my WebMethod (I'll overload [WebMethod] and [HttpPost]) right ? – Kurt Miller Apr 06 '17 at 14:25
  • 'SomeController' mean your Controller name. – Zaheer Ul Hassan Apr 06 '17 at 14:26
  • I don't think so...I failed everywhere...it said I have to declare classes...I'm trying to save ti to jsFiddle to show you – Kurt Miller Apr 06 '17 at 14:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/140088/discussion-between-zaheer-ul-hassan-and-snorlax). – Zaheer Ul Hassan Apr 06 '17 at 14:47