rippled
Loading...
Searching...
No Matches
Status.h
1#pragma once
2
3#include <xrpl/beast/utility/instrumentation.h>
4#include <xrpl/protocol/ErrorCodes.h>
5#include <xrpl/protocol/TER.h>
6
7namespace xrpl {
8namespace RPC {
9
19struct Status : public std::exception
20{
21public:
22 enum class Type { none, TER, error_code_i };
23 using Code = int;
25
26 static constexpr Code OK = 0;
27
28 Status() = default;
29
30 // The enable_if allows only integers (not enums). Prevents enum narrowing.
31 template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
32 Status(T code, Strings d = {}) : type_(Type::none), code_(code), messages_(std::move(d))
33 {
34 }
35
36 Status(TER ter, Strings d = {}) : type_(Type::TER), code_(TERtoInt(ter)), messages_(std::move(d))
37 {
38 }
39
41 {
42 }
43
45 {
46 }
47
48 /* Returns a representation of the integer status Code as a string.
49 If the Status is OK, the result is an empty string.
50 */
52 codeString() const;
53
55 operator bool() const
56 {
57 return code_ != OK;
58 }
59
61 bool
62 operator!() const
63 {
64 return !bool(*this);
65 }
66
69 TER
70 toTER() const
71 {
72 XRPL_ASSERT(type_ == Type::TER, "xrpl::RPC::Status::toTER : type is TER");
73 return TER::fromInt(code_);
74 }
75
80 {
81 XRPL_ASSERT(type_ == Type::error_code_i, "xrpl::RPC::Status::toTER : type is error code");
82 return error_code_i(code_);
83 }
84
87 void
88 inject(Json::Value& object) const
89 {
90 if (auto ec = toErrorCode())
91 {
92 if (messages_.empty())
93 inject_error(ec, object);
94 else
95 inject_error(ec, message(), object);
96 }
97 }
98
99 Strings const&
100 messages() const
101 {
102 return messages_;
103 }
104
107 message() const;
108
109 Type
110 type() const
111 {
112 return type_;
113 }
114
116 toString() const;
117
121 void
123
124private:
128};
129
130} // namespace RPC
131} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
static constexpr TERSubset fromInt(int from)
Definition TER.h:413
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:5
constexpr TERUnderlyingType TERtoInt(TELcodes v)
Definition TER.h:355
error_code_i
Definition ErrorCodes.h:20
Status represents the results of an operation that might fail.
Definition Status.h:20
Strings const & messages() const
Definition Status.h:100
Status(error_code_i e, std::string const &s)
Definition Status.h:44
error_code_i toErrorCode() const
Returns the Status as an error_code_i.
Definition Status.h:79
Status(TER ter, Strings d={})
Definition Status.h:36
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:26
std::vector< std::string > Strings
Definition Status.h:24
void inject(Json::Value &object) const
Apply the Status to a JsonObject.
Definition Status.h:88
Status(T code, Strings d={})
Definition Status.h:32
TER toTER() const
Returns the Status as a TER.
Definition Status.h:70
bool operator!() const
Returns true if the Status is OK.
Definition Status.h:62
void fillJson(Json::Value &)
Fill a Json::Value with an RPC 2.0 response.
Definition Status.cpp:42
Type type() const
Definition Status.h:110
Strings messages_
Definition Status.h:127
Status(error_code_i e, Strings d={})
Definition Status.h:40
std::string toString() const
Definition Status.cpp:75