Unable to catch errors with httpclient lib... help? :|

Enonic version: 6.8
OS: Windows

I was wondering if anyone had a good tip on why I seem unable to catch errors from the http client lib? I am using the following snippet to get some data from a backend service, but if the backend service is unavailable (no port open on the url) I want to be able to handle this in a better way than a big red error page.

I am using the following code:

function getData(url, token) {
    token = token || "";
    var siteConfig = portal.getSiteConfig();

    log.info("Requesting " + siteConfig.backendUrl + url);
    var res = {};

    try {
        var resp = httpClient.request({
            url: siteConfig.backendUrl + url,
            method: 'GET',
            headers: {
                'Authorization': basicAuth,
                'x-auth-token': token
            },
            contentType: 'application/json',
            connectTimeout: 1000,
            readTimeout: 1000
        });

        res = JSON.parse(resp.body);
    }
    catch(e) {
        log.error("Unable to get data from " + url + ": " + e.message);
    }

    return {};
}

At the moment I am getting this error even with the try/catch:

The try catch in your code should capture the exception. Are you sure that is the code you are running?

Can you double check that the app deployed is the one you think it is, and there are no other versions in the deploy directory?

DOH! I believe I figured out what is wrong. I have two command line windows open, one with gradle deploy -t and one running bin\server.bat.

Before starting the server I did ran set XP_HOME=c:… Since I had already opened the command line for the gradle part this had already fetched its environment variables already, with the result of Gradle and Enonic running with different XP_HOME variables, hehe.

Thanks for the pointer in the right direction, I would assume that you can close this one :wink:

1 Like