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 = {}) : code_(code), messages_(std::move(d))
33 {
34 }
35
36 Status(TER ter, Strings d = {})
37 : type_(Type::TER), code_(TERtoInt(ter)), messages_(std::move(d))
38 {
39 }
40
42 : type_(Type::error_code_i), code_(e), messages_(std::move(d))
43 {
44 }
45
48 {
49 }
50
51 /* Returns a representation of the integer status Code as a string.
52 If the Status is OK, the result is an empty string.
53 */
55 codeString() const;
56
58 operator bool() const
59 {
60 return code_ != OK;
61 }
62
64 bool
65 operator!() const
66 {
67 return !bool(*this);
68 }
69
72 TER
73 toTER() const
74 {
75 XRPL_ASSERT(type_ == Type::TER, "xrpl::RPC::Status::toTER : type is TER");
76 return TER::fromInt(code_);
77 }
78
83 {
84 XRPL_ASSERT(type_ == Type::error_code_i, "xrpl::RPC::Status::toTER : type is error code");
85 return error_code_i(code_);
86 }
87
90 void
91 inject(Json::Value& object) const
92 {
93 if (auto ec = toErrorCode())
94 {
95 if (messages_.empty())
96 inject_error(ec, object);
97 else
98 inject_error(ec, message(), object);
99 }
100 }
101
102 Strings const&
103 messages() const
104 {
105 return messages_;
106 }
107
110 message() const;
111
112 Type
113 type() const
114 {
115 return type_;
116 }
117
119 toString() const;
120
124 void
126
127private:
131};
132
133} // namespace RPC
134} // 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:103
Status(error_code_i e, std::string const &s)
Definition Status.h:46
error_code_i toErrorCode() const
Returns the Status as an error_code_i.
Definition Status.h:82
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:91
Status(T code, Strings d={})
Definition Status.h:32
TER toTER() const
Returns the Status as a TER.
Definition Status.h:73
bool operator!() const
Returns true if the Status is OK.
Definition Status.h:65
void fillJson(Json::Value &)
Fill a Json::Value with an RPC 2.0 response.
Definition Status.cpp:42
Type type() const
Definition Status.h:113
Strings messages_
Definition Status.h:130
Status(error_code_i e, Strings d={})
Definition Status.h:41
std::string toString() const
Definition Status.cpp:75