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 ripple {
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, "ripple::RPC::Status::toTER : type is TER");
81 return TER::fromInt(code_);
82 }
83
88 {
89 XRPL_ASSERT(
91 "ripple::RPC::Status::toTER : type is error code");
92 return error_code_i(code_);
93 }
94
97 template <class Object>
98 void
99 inject(Object& object) const
100 {
101 if (auto ec = toErrorCode())
102 {
103 if (messages_.empty())
104 inject_error(ec, object);
105 else
106 inject_error(ec, message(), object);
107 }
108 }
109
110 Strings const&
111 messages() const
112 {
113 return messages_;
114 }
115
118 message() const;
119
120 Type
121 type() const
122 {
123 return type_;
124 }
125
127 toString() const;
128
132 void
134
135private:
139};
140
141} // namespace RPC
142} // namespace ripple
143
144#endif
Represents a JSON value.
Definition json_value.h:130
static constexpr TERSubset fromInt(int from)
Definition TER.h:414
T empty(T... args)
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition ErrorCodes.h:214
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
STL namespace.
Status represents the results of an operation that might fail.
Definition Status.h:21
static constexpr Code OK
Definition Status.h:27
Strings const & messages() const
Definition Status.h:111
error_code_i toErrorCode() const
Returns the Status as an error_code_i.
Definition Status.h:87
Status(error_code_i e, std::string const &s)
Definition Status.h:50
std::vector< std::string > Strings
Definition Status.h:25
Status(TER ter, Strings d={})
Definition Status.h:40
Status(error_code_i e, Strings d={})
Definition Status.h:45
std::string toString() const
Definition Status.cpp:75
void inject(Object &object) const
Apply the Status to a JsonObject.
Definition Status.h:99
std::string message() const
Return the first message, if any.
Definition Status.cpp:61
Status(T code, Strings d={})
Definition Status.h:35
bool operator!() const
Returns true if the Status is OK.
Definition Status.h:69
Type type() const
Definition Status.h:121
Strings messages_
Definition Status.h:138
TER toTER() const
Returns the Status as a TER.
Definition Status.h:77
std::string codeString() const
Definition Status.cpp:9
void fillJson(Json::Value &)
Fill a Json::Value with an RPC 2.0 response.
Definition Status.cpp:42