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
12using namespace jsonrpc;
13
14JsonRpcException::JsonRpcException(int code) : code(code), message(Errors::GetErrorMessage(code)) { this->setWhatMessage(); }
15
16JsonRpcException::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
23JsonRpcException::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
31JsonRpcException::JsonRpcException(const std::string &message) : code(0), message(message) { this->setWhatMessage(); }
32
33JsonRpcException::~JsonRpcException() throw() {}
34
35int JsonRpcException::GetCode() const { return code; }
36
37const std::string &JsonRpcException::GetMessage() const { return message; }
38
39const Json::Value &JsonRpcException::GetData() const { return data; }
40
41const char *JsonRpcException::what() const throw() { return this->whatString.c_str(); }
42
43void 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