rippled
Loading...
Searching...
No Matches
Status.h
1#ifndef XRPL_RPC_STATUS_H_INCLUDED
2#define XRPL_RPC_STATUS_H_INCLUDED
3
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/protocol/ErrorCodes.h>
6#include <xrpl/protocol/TER.h>
7
8namespace xrpl {
9namespace RPC {
10
20struct Status : public std::exception
21{
22public:
23 enum class Type { none, TER, error_code_i };
24 using Code = int;
26
27 static constexpr Code OK = 0;
28
29 Status() = default;
30
31 // The enable_if allows only integers (not enums). Prevents enum narrowing.
32 template <
33 typename T,
35 Status(T code, Strings d = {})
36 : type_(Type::none), code_(code), messages_(std::move(d))
37 {
38 }
39
40 Status(TER ter, Strings d = {})
41 : type_(Type::TER), code_(TERtoInt(ter)), messages_(std::move(d))
42 {
43 }
44
46 : type_(Type::error_code_i), code_(e), messages_(std::move(d))
47 {
48 }
49
52 {
53 }
54
55 /* Returns a representation of the integer status Code as a string.
56 If the Status is OK, the result is an empty string.
57 */
59 codeString() const;
60
62 operator bool() const
63 {
64 return code_ != OK;
65 }
66
68 bool
69 operator!() const
70 {
71 return !bool(*this);
72 }
73
76 TER
77 toTER() const
78 {
79 XRPL_ASSERT(
80 type_ == Type::TER, "xrpl::RPC::Status::toTER : type is TER");
81 return TER::fromInt(code_);
82 }
83
88 {
89 XRPL_ASSERT(
91 "xrpl::RPC::Status::toTER : type is error code");
92 return error_code_i(code_);
93 }
94
97 void
98 inject(Json::Value& object) const
99 {
100 if (auto ec = toErrorCode())
101 {
102 if (messages_.empty())
103 inject_error(ec, object);
104 else
105 inject_error(ec, message(), object);
106 }
107 }
108
109 Strings const&
110 messages() const
111 {
112 return messages_;
113 }
114
117 message() const;
118
119 Type
120 type() const
121 {
122 return type_;
123 }
124
126 toString() const;
127
131 void
133
134private:
138};
139
140} // namespace RPC
141} // namespace xrpl
142
143#endif
Represents a JSON value.
Definition json_value.h:131
static constexpr TERSubset fromInt(int from)
Definition TER.h:414
T empty(T... args)
STL namespace.
void inject_error(error_code_i code, Json::Value &json)
Add or update the json update to reflect the error code.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
constexpr TERUnderlyingType TERtoInt(TELcodes v)
Definition TER.h:356
error_code_i
Definition ErrorCodes.h:21
Status represents the results of an operation that might fail.
Definition Status.h:21
Strings const & messages() const
Definition Status.h:110
Status(error_code_i e, std::string const &s)
Definition Status.h:50
error_code_i toErrorCode() const
Returns the Status as an error_code_i.
Definition Status.h:87
Status(TER ter, Strings d={})
Definition Status.h:40
std::string message() const
Return the first message, if any.
Definition Status.cpp:61
std::string codeString() const
Definition Status.cpp:9
static constexpr Code OK
Definition Status.h:27
std::vector< std::string > Strings
Definition Status.h:25
void inject(Json::Value &object) const
Apply the Status to a JsonObject.
Definition Status.h:98
Status(T code, Strings d={})
Definition Status.h:35
TER toTER() const
Returns the Status as a TER.
Definition Status.h:77
bool operator!() const
Returns true if the Status is OK.
Definition Status.h:69
void fillJson(Json::Value &)
Fill a Json::Value with an RPC 2.0 response.
Definition Status.cpp:42
Type type() const
Definition Status.h:120
Strings messages_
Definition Status.h:137
Status(error_code_i e, Strings d={})
Definition Status.h:45
std::string toString() const
Definition Status.cpp:75