| 1 | /************************************************************************* |
|---|---|
| 2 | * libjson-rpc-cpp |
| 3 | ************************************************************************* |
| 4 | * @file exception.cpp |
| 5 | * @date 31.12.2012 |
| 6 | * @author Peter Spiess-Knafl <dev@spiessknafl.at> |
| 7 | * @license See attached LICENSE.txt |
| 8 | ************************************************************************/ |
| 9 | |
| 10 | #include "exception.h" |
| 11 | |
| 12 | using namespace jsonrpc; |
| 13 | |
| 14 | JsonRpcException::JsonRpcException(int code) : code(code), message(Errors::GetErrorMessage(code)) { this->setWhatMessage(); } |
| 15 | |
| 16 | JsonRpcException::JsonRpcException(int code, const std::string &message) : code(code), message(Errors::GetErrorMessage(code)) { |
| 17 | if (!this->message.empty()) |
| 18 | this->message = this->message + ": "; |
| 19 | this->message = this->message + message; |
| 20 | this->setWhatMessage(); |
| 21 | } |
| 22 | |
| 23 | JsonRpcException::JsonRpcException(int code, const std::string &message, const Json::Value &data) |
| 24 | : code(code), message(Errors::GetErrorMessage(code)), data(data) { |
| 25 | if (!this->message.empty()) |
| 26 | this->message = this->message + ": "; |
| 27 | this->message = this->message + message; |
| 28 | this->setWhatMessage(); |
| 29 | } |
| 30 | |
| 31 | JsonRpcException::JsonRpcException(const std::string &message) : code(0), message(message) { this->setWhatMessage(); } |
| 32 | |
| 33 | JsonRpcException::~JsonRpcException() throw() {} |
| 34 | |
| 35 | int JsonRpcException::GetCode() const { return code; } |
| 36 | |
| 37 | const std::string &JsonRpcException::GetMessage() const { return message; } |
| 38 | |
| 39 | const Json::Value &JsonRpcException::GetData() const { return data; } |
| 40 | |
| 41 | const char *JsonRpcException::what() const throw() { return this->whatString.c_str(); } |
| 42 | |
| 43 | void JsonRpcException::setWhatMessage() { |
| 44 | if (this->code != 0) { |
| 45 | std::stringstream ss; |
| 46 | ss << "Exception "<< this->code << " : "<< this->message; |
| 47 | if (data != Json::nullValue) |
| 48 | ss << ", data: "<< data.toStyledString(); |
| 49 | this->whatString = ss.str(); |
| 50 | } else { |
| 51 | this->whatString = this->message; |
| 52 | } |
| 53 | } |
| 54 |