Monday, 15 May 2017

ErrorHandler not being called with HystrixJS Commands wrapped around SailsJS controller calls to DB

I'm having some difficulting getting HystrixJS to work with my SailsJS controller calls to the database. I want to wrap each request to the database in a hystrix command. I seem to be able to wrap the calls in a command and have a successfully response. But when I force return an error, the isErrorHandler is not called, placing some logs in the Command.js lib folder shows it is still calling the successHandler. How would I go about making sure the isErrorHandler is being called properly when an error is returned from my wrapped function? Below is some example code I'm working with for getting a list of users. Thank you for any help.

getUsers: function(req, res, next) {
    // call to wrapped by hystrix command
    function promiseCall() {
        var deferred = Q.defer();

        User.find().exec(function (err, users) {
            // i know users will be returned, trying fo force the isErrorHandlers
            if(users) {
                deferred.reject(new Error("No Users"));
                return res.serverError();
            }

            deferred.resolve();
            res.json(users);
        })

        return deferred.promise; 
    }

    var isErrorHandler = function(error) {
        console.log('error caught', error);
        if (error) {
            return error;
        }
        if (error.statusCode == 503) {
            var unavailableError = new Error();
            unavailableError.name = "ServiceUnavailableError";
            return unavailableError;
        }
        return null;
    };

    var fallback = function (error) {
        console.log('fallback', error);
    }

    var serviceCommand = CommandsFactory.getOrCreate("get users")     
        .circuitBreakerErrorThresholdPercentage(50)     
        .timeout(10000)     
        .run(promiseCall)
        .circuitBreakerRequestVolumeThreshold(10)     
        .circuitBreakerSleepWindowInMilliseconds(3000)            
        .errorHandler(isErrorHandler)
        .fallbackTo(fallback)     
        .build();
    console.log('call promise');

    var promise = serviceCommand.execute();
}



via Trevor

No comments:

Post a Comment