How can I retrieve HttpClient error descriptions in JS policy?

I have been working on JS policy for a couple of weeks and wanted to get some clarifications:

1. Is HttpClient the only object that is available to make requests/callouts? When I try to use Promise or ajax, they are not recognized.

2. Can I retrieve the error code from a failed request callout?

    var url = "someurl.com";
    var req = new Request(url, 'POST', headers, payload);
    var exchange = httpClient.send(req);
    exchange.waitForComplete();
    response = exchange.getResponse();
    print(response);



    if (exchange.isError()) {
        print("did not resolve");
        print(exchange.getError().status)
        print(exchange.getError().code)
        print(exchange.getError().message)
        //throw new Error(exchange.getError());
        response = exchange.getError(); // returns Timed out as String
    }

I want to be able to retrieve the HTTP status code of the error. Currently exchange.getError() returns "Timed out" which I know is a 504. But can I get the actual status code?

I tried using .status, .code, .message which all return an undefined.

0 2 579
2 REPLIES 2

Yes, httpClient only . There is no Promise, no ES6 support in the JS used in Apigee callouts.

exchange.getResponse().status.code
exchange.getResponse().status.message

What if the call was not successful and returned status code of 500 and above? Since the response is undefined, the lines you provided would not work.