Http-client library behaviour

Enonic version: 6.3
OS: Windows 7 x64

Currently I have some problems regarding handling errors after calling httpClientLib.request({}). Currently if I am trying to connect to non existing service(just for example) I am getting ConnectionRefused error, but it returned not like httpClientLib.request({}) response, but handled in default error.js file. Is it possible to get one response endpoint?

What do you mean by “one response endpoint”?
Ala controlled handling of the httpClientLib.request? Did you try using javascript try/catch?

Yeah, I mean handling of all responses from httpClientLib.request. Yes I have tried, but in case of ConnectionRefused error it goes to error handler - error.js.

Seems to be it works in all cases besides the case it it falls somewhere inside jax-rs code, and than it goes to error.js handler. At least it is my vision, but not sure about that =).

I see that when a timeout occurs, then http.request throws an exception. When an exception occurs it will automatically trigger the error page to show. But if you try/catch around the block it will be controlled by you. Like this:

try {
  httpClientLib.request({...});
} catch (e) {
  // Handle the error
}
1 Like

Strange, try-catch was the very first way how I have tried to do that, and it worked in the same way(went to error.js).

But I have tried to do it one more time just to be 100% sure that it’s not working, and it started to work. Seems to be I shouldn’t work late and create posts especially. Thank you for help.

We do not use jaxrs for the portal anymore. It’s only done in the admin part (especially rest services).

Enonic 6.4.2

If I don’t enclose httpClientLib.request in a try catch block, I get:

java.net.ConnectException: Connection refused (java.lang.RuntimeException)```

If I ```catch(e)```, then e is undefined.

Which makes it kinda difficult to handle various exceptions differently...

Like this:
https://www.safaribooksonline.com/library/view/javascript-the-definitive/9781449393854/ch11s06.html
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/try...catch

Let me swallow my words :blush:

I have managed to catch these so far:

  • java.net.SocketTimeoutException: connect timed out
  • java.net.SocketTimeoutException: timeout
  • java.net.ConnectException: Connection refused

I think e.name should work though:

var json = {};
    try {
        json = httpClientLib.request({
            connectionTimeout: 10000, // milliseconds
            readTimeout: 10000, // milliseconds
            url: url,
            method: 'GET',
            headers: {
                Authorization: auth,
                'Accept-Charset': 'utf-8'
            }
        }).body;
    } catch(e) {
        log.info('catch after httpClientLib.request')
        log.info(e); // java.net.SocketTimeoutException: connect timed out
        log.info(e.toString()); // java.net.SocketTimeoutException: connect timed out
        log.info(typeof e); // object
        log.info(e.message); // connect timed out || timeout
        if(e instanceof java.net.ConnectException) {
            log.info('Yes instanceof java.net.ConnectException');
        }
        if( e instanceof java.net.SocketTimeoutException) {
            log.info('Yes instanceof java.net.SocketTimeoutException');
        }
        log.info(e.name); // undefined
        log.info(e.fileName); // undefined
        log.info(e.lineNumber); // undefined
        log.info(e.columnNumber); // undefined
        log.info(e.stack); // undefined
        log.info(JSON.stringify(e, null, 4)); // undefined
        //log.info(e.toSource()); // TypeError: e.toSource is not a function (com.enonic.xp.resource.ResourceProblemException)
    }
    return json;

Perhaps the docs should contain something like this:

var connectionTimeout = 10000; // milliseconds
    var readTimeout = 10000; // milliseconds
    var json = {};
    try {
        json = httpClientLib.request({
            connectionTimeout: connectionTimeout,
            readTimeout: readTimeout,
            url: url,
            method: 'GET',
            headers: {
                Authorization: auth,
                'Accept-Charset': 'utf-8'
            }
        }).body;
    } catch(e if e instanceof java.net.ConnectException) {
        log.error(e.message + ': on url ' + url);
        // do something
    } catch(e if e instanceof java.net.SocketTimeoutException ) {
        if(e.message === 'connect timed out') {
            log.error(e.message + ': connectionTimeout ' + connectionTimeout + 'ms exceeded on url ' + url);
        } else if (e.message === 'timeout') {
            log.error(e.message + ': readTimeout ' + readTimeout + 'ms exceeded on url ' + url);
        } else {
            log.error('java.net.SocketTimeoutException with unknown message:' + e.message);
            log.error(e);
        }
        // do something
    } catch (e) {
        log.error(e.message + ': unhandled error on url ' + url);
        // do something
    }
    return json;