locked
Should I catch errors in js level or C# level? RRS feed

  • Question

  • User283528319 posted

    hi all,

    Which is the right approach catch the error in js level or C# level?

    like below

     $.get("NK/GonderiyiReddet", function (data) {
    
        }).fail(function () { $.alert("Error") });

    or below

                try
                {
                    Gonderiler ReddedilecekGonderi = _VNTSClientContext.Gonderiler.Single(e => e.GonderiDurumuId == HttpContext.Session.GetInt32("TiklananGonderiId"));
                    ReddedilecekGonderi.GonderiDurumuId = 4;//reddedildi kodu
                    _VNTSClientContext.Gonderiler.Update(ReddedilecekGonderi);
                    _VNTSClientContext.SaveChanges();
                    return "+";
                }
                catch (Exception)
                {
                    return "Error";
                }

    then get the return message and alert with it.

    Friday, August 2, 2019 7:25 AM

Answers

  • User-821857111 posted

    It depends on what you want to do with the error message. 

    If you catch the exception in C#, and return a string as above, the .fail method will not fire. You have to parse data to see what it contains. However, your fail method will still fire if e.g. the user encounters networking issues which prevent the request from completing. So now you are handling errors in two places. 

    If you want to log details of the error in C#, you can do that then re-throw the exception so that it causes the fail method in your client code to fire:

    try{
        ...
    }
    catch (Exception ex)
    {
        Log(ex);
        throw ex;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 8:41 AM
  • User475983607 posted

    fatihbarut

    could you simply show how to get exception message in the console of browser instead of logging it (for debug purposes)?

    I write to the console.log() during development and build permanent code error handlers for production code. 

    Are you asking how to write to the console?

     $.get("NK/GonderiyiReddet", function (data) {
    
        }).fail(function (jqXHR, textStatus, errorThrown) { console.log(errorThrown) });

    https://api.jquery.com/jQuery.ajax/

    jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
    An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 11:47 AM

All replies

  • User-821857111 posted

    It depends on what you want to do with the error message. 

    If you catch the exception in C#, and return a string as above, the .fail method will not fire. You have to parse data to see what it contains. However, your fail method will still fire if e.g. the user encounters networking issues which prevent the request from completing. So now you are handling errors in two places. 

    If you want to log details of the error in C#, you can do that then re-throw the exception so that it causes the fail method in your client code to fire:

    try{
        ...
    }
    catch (Exception ex)
    {
        Log(ex);
        throw ex;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 8:41 AM
  • User283528319 posted

    could you simply show how to get exception message in the console of browser instead of logging it (for debug purposes)?

    Friday, August 2, 2019 9:45 AM
  • User475983607 posted

    fatihbarut

    could you simply show how to get exception message in the console of browser instead of logging it (for debug purposes)?

    I write to the console.log() during development and build permanent code error handlers for production code. 

    Are you asking how to write to the console?

     $.get("NK/GonderiyiReddet", function (data) {
    
        }).fail(function (jqXHR, textStatus, errorThrown) { console.log(errorThrown) });

    https://api.jquery.com/jQuery.ajax/

    jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
    An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 11:47 AM
  • User-474980206 posted
    Whether the C# code catches errors or not, the JavaScript still needs to catch Ajax errors. This is because the network request may fail, the site may crash, or have unexpected errors. As the JavaScript must handle errors anyway, I find it simpler for the server to just throw errors if a payload will not be returned.
    Friday, August 2, 2019 3:11 PM
  • User711641945 posted

    Hi fatihbarut,

    As mike said,it's up to you about how to show error message.And you could also use try/catch in js file.Here is a simple working demo:

    <p>Please input a number between 5 and 10:</p>
    <input id="demo" type="text">
    <button type="button" onclick="myFunction()">Test Input</button>
    <p id="p01"></p>
    @section Scripts{
        <script>
            function myFunction() {
                debugger;   //press F11 to debug step by step
                var message, x;
                message = document.getElementById("p01");
                message.innerHTML = "";
                x = document.getElementById("demo").value;
                try {
    //custom error message if (x == "") throw "empty"; if (isNaN(x)) throw "not a number"; x = Number(x); if (x < 5) throw "too low"; if (x > 10) throw "too high"; } catch (err) { message.innerHTML = "Input is " + err; } } </script> }

    Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

    Best Regards,

    Rena

    Monday, August 5, 2019 9:19 AM