Connect express httpClient never throws an error

Hi,

I’m using this code for various api calls and no matter what I do to create an error, the error parameter is always null and the actual error can be found in result.body.

I’ve tried things like querying non existing endpoints. Updating issues with invalid data. Updating non existing projects.

Also sometimes the error value in the result.body returns as different type.
JSON as an object
JSON as a string
HTML as a string

Where is the consistency? Am I simply doing something wrong or is the api shot to pieces?

This applies to put, post and delete requests which I am using, not just get which is in this example.

httpClient.get(
    {
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json'
        },
        ...options
    },
    (error, result) => {
        if (error) {
            console.error(error);
        } else {
            console.log(result.body);
        }
    }
);

Hi @RichardWattie,

The error parameter will be set if there are errors making the request or receiving a response, not due to a specific features of the response such as the status code or body contents. To observe this you could, for example, set this option: { proxy: 'example.com'} which for me gives an error parameter of {"code":"ECONNRESET"}.

To your other question, unfortunately there is some inconsistency in the format of errors depending on where the error happens while processing your request.

I hope that helped, and please feel free to post again if there are specific errors tripping you up.

Regards,
James Hazelwood

Senior developer, Ecosystem Engineering, Atlassian

1 Like

Is there any plans to improve this? It’s not very helpful to only receive an error if there’s an error making the request or receiving a response. How is someone supposed to easily handle failure cases if there is an issue within the request body for example? With the inconsistency of the response body it is painful to code to handle errors.

It should be possible to tell from the response code most of the time that there’s an error. I’m not aware of any work going on at present to bring consistency to REST errors or to write a client that smooths over the differences, sorry.