Skip to content

Commit 74f9152

Browse files
authored
Refined exception handling
1 parent 3ede4fa commit 74f9152

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/bitcoinapi/exception.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
#include <jsoncpp/json/json.h>
1717
#include <jsoncpp/json/reader.h>
1818
#include <jsoncpp/json/value.h>
19+
#include <jsonrpccpp/client.h>
1920

2021
using Json::Value;
2122
using Json::Reader;
23+
using jsonrpc::Errors;
2224

2325

2426
class BitcoinException: public std::exception
@@ -29,9 +31,22 @@ class BitcoinException: public std::exception
2931

3032
public:
3133
explicit BitcoinException(int errcode, const std::string& message) {
32-
this->code = parseCode(message);
33-
this->msg = parseMessage(message);
34+
35+
/* Connection error */
36+
if(errcode == Errors::ERROR_CLIENT_CONNECTOR){
37+
this->code = errcode;
38+
this->msg = removePrefix(message, " -> ");
39+
/* Authentication error */
40+
}else if(errcode == Errors::ERROR_RPC_INTERNAL_ERROR && message.size() == 18){
41+
this->code = errcode;
42+
this->msg = "Failed to authenticate successfully";
43+
/* Miscellaneous error */
44+
}else{
45+
this->code = parseCode(message);
46+
this->msg = parseMessage(message);
47+
}
3448
}
49+
3550
~BitcoinException() throw() { };
3651

3752
int getCode(){
@@ -55,7 +70,7 @@ class BitcoinException: public std::exception
5570
return ret;
5671
}
5772

58-
73+
/* Auxiliary JSON parsing */
5974
int parseCode(const std::string& in){
6075
Value root;
6176
Reader reader;

0 commit comments

Comments
 (0)