cli: extract connection exception handler, -rpcwait logic to ConnectAndCallRPC()
Summary:
This is needed for RPCs that need to be called and handled sequentially, rather
than alone or in a batch.For example, when fetching the balances for each loaded wallet, -getinfo will
call RPC listwallets, and then, depending on the result, RPC getbalances.
This is a backport of Core PR18594 [1/6]
https://github.com/bitcoin/bitcoin/pull/18594/commits/29f2cbdeb7afdde87d108adf80cffad17d112632
Test Plan: ninja all check-all
Reviewers: #bitcoin_abc, majcosta
Reviewed By: #bitcoin_abc, majcosta
Differential Revision: https://reviews.bitcoinabc.org/D9237