Actively looking to catch an ECONNREFUSED

Hi
I’m changing our server port through our API and I want to check that when I change the port, the old one is actually closed, however when I send a request on the old port, and get the “Error: connect ECONNREFUSED”, it seems that the testing code is skipped.

Also, the execution stops when this error is thrown.

How can I catch this error and handle it myself?

Hi @Alexandre.marquis! Welcome to the community :tada:

Test scripts are executed after request is sent and pre-request scripts are executed in prior.
As the request itself is returning the error, test scripts are not executed in your case.

Instead of including your test in Tests, you can include in Pre-request scripts, e.g.

pm.sendRequest('localhost:4000', (err, res) => {
    if (err) {
        throw new Error('No connection')
    } else {
        console.log(res)
    }
}) 

This returns:
Screen Shot 2021-02-08 at 4.45.28 pm

Hope this helps!