locked
Stop Execution on Error RRS feed

  • Question

  • User438705957 posted

    I have the code below to control the flow of execution.
    The goal is to prevent execution of the login() function, if there is an error in getAssociations() or getDefaultControllers().
    In both of those functions I have a throw statement which throws a custom error if an error occurs - throw new Error("A serious error occurred");  

    The problem is, it never gets to the catch statement to clear the timeout that run login() off the queue, and it just continues on to execute login().

    Here's the code

     var executeLogin = setTimeout(function () {
            model = 'Audit';
            login();
        }, 0);
    
        try {
            getAssociations();     
            getDefaultControllers(); 
        } catch (e) {
            clearTimeout(executeLogin);
            return false;
        }

    Thanks for considering

    Tuesday, February 19, 2019 1:31 AM

Answers

  • User438705957 posted

    Thanks for replying PatriceSc 

    I ended up solving the issue by using JQuery promises.
    I stripped down the functions calling the Web API controller, getAssociations() and getDefaultControllers() to a bare minimum:

    function getAssociations() {
        var uriMetaFinal = "api/foundations/manytomany";
        return $.ajax({
            url: uriMetaFinal + "?" + $.param({ "key": "star", key2: "metadata" }),
            type: 'GET',
            async: false
        });
    }

    The code to continue with on success of these calls I set off on a timeout set to zero. This placed it on the task queue, waiting for everything else to finish.

    var executeLogin = setTimeout(function () { 
            model = 'Audit';
            login();
        }, 0);

    I then ran getAssociations() and getDefaultControllers() returning a promise, which is what an async call returns anyway. The promises were wrapped in a try catch, so any problem in the calls returned false and cleared the executeLogin timeout, which prevented login() from running.

    var promise = getAssociations();
    var promise2 = getDefaultControllers();
        try {
            promise.done(function (data) {
                if (data.CustomErrorMessage) {
                    errorCallback("Error in getting Association", "Failure", data.CustomErrorMessage + "</br> Please inform the Senior Analyst.");
                    clearTimeout(executeLogin);
                    throw 'A serious error occurred';                
                }
                else {
                    $.each(data, function (key, item) {
                        metaAssociations[key] = item;
                    });
                    sessionStorage.setItem("metaAssociations", JSON.stringify(metaAssociations));       // Put the object into storage	
                }
            });
            promise2.done(function (data) {
                if (data.CustomErrorMessage) {
                    errorCallback("Error in getting Association", "Failure", data.CustomErrorMessage + "</br> Please inform the Senior Analyst.");
                    clearTimeout(executeLogin);
                    throw 'A serious error occurred';                
                }
                else {
                    $.each(data, function (key, item) {
                        defaultControllers[key] = item;
                    });
                }
            });
        } catch (e) {
            return false;
        }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 19, 2019 9:49 PM

All replies

  • User753101303 posted

    Hi,

    What if using 1000 maybe rather then 0 ? It seems you could do :

        try {
            getAssociations();     
            getDefaultControllers(); 
            model='Audit';
            login();
        } catch (e) {
            //clearTimeout(executeLogin);
            return false;
        }

    though I never turn an exception into a return code (the caller doesn't know anything about what happens).

    Tuesday, February 19, 2019 9:33 AM
  • User438705957 posted

    Thanks for replying PatriceSc 

    I ended up solving the issue by using JQuery promises.
    I stripped down the functions calling the Web API controller, getAssociations() and getDefaultControllers() to a bare minimum:

    function getAssociations() {
        var uriMetaFinal = "api/foundations/manytomany";
        return $.ajax({
            url: uriMetaFinal + "?" + $.param({ "key": "star", key2: "metadata" }),
            type: 'GET',
            async: false
        });
    }

    The code to continue with on success of these calls I set off on a timeout set to zero. This placed it on the task queue, waiting for everything else to finish.

    var executeLogin = setTimeout(function () { 
            model = 'Audit';
            login();
        }, 0);

    I then ran getAssociations() and getDefaultControllers() returning a promise, which is what an async call returns anyway. The promises were wrapped in a try catch, so any problem in the calls returned false and cleared the executeLogin timeout, which prevented login() from running.

    var promise = getAssociations();
    var promise2 = getDefaultControllers();
        try {
            promise.done(function (data) {
                if (data.CustomErrorMessage) {
                    errorCallback("Error in getting Association", "Failure", data.CustomErrorMessage + "</br> Please inform the Senior Analyst.");
                    clearTimeout(executeLogin);
                    throw 'A serious error occurred';                
                }
                else {
                    $.each(data, function (key, item) {
                        metaAssociations[key] = item;
                    });
                    sessionStorage.setItem("metaAssociations", JSON.stringify(metaAssociations));       // Put the object into storage	
                }
            });
            promise2.done(function (data) {
                if (data.CustomErrorMessage) {
                    errorCallback("Error in getting Association", "Failure", data.CustomErrorMessage + "</br> Please inform the Senior Analyst.");
                    clearTimeout(executeLogin);
                    throw 'A serious error occurred';                
                }
                else {
                    $.each(data, function (key, item) {
                        defaultControllers[key] = item;
                    });
                }
            });
        } catch (e) {
            return false;
        }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 19, 2019 9:49 PM