javascript - How to retrieve DbContext SqlException in AJAX error method? -
lets have check constraint not respected when adding value database column or lets says have trigger uses raiseerror custom message.
in webmethod, use sqlexception catch error while updating database. hence should able retrieve automatic sql server error message if check constraint not respected or custom message if trigger raises error message right ?
how do ?
below 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({ // other settings error: function (xhr, status, error){ alert(json.parse(xhr.responsetext).message); alert(error); } }); } here error not return
"my custom message .....(and auto generated message)"....
it returns "internal server error" ..
any idea of should ?
that's because you're catching exception, , returning simple string. webmethod, in theory, never fails... (it fails, instruct webmethod never inform exception). that's why ajax call never reaches error function.
is bad idea encapsulate , hide errors catching exceptions. encapsulate , throw different exception, or remove try/catch block
Comments
Post a Comment