rippled
Loading...
Searching...
No Matches
Status.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_RPC_STATUS_H_INCLUDED
21#define RIPPLE_RPC_STATUS_H_INCLUDED
22
23#include <xrpl/beast/utility/instrumentation.h>
24#include <xrpl/protocol/ErrorCodes.h>
25#include <xrpl/protocol/TER.h>
26
27namespace ripple {
28namespace RPC {
29
39struct Status : public std::exception
40{
41public:
42 enum class Type { none, TER, error_code_i };
43 using Code = int;
45
46 static constexpr Code OK = 0;
47
48 Status() = default;
49
50 // The enable_if allows only integers (not enums). Prevents enum narrowing.
51 template <
52 typename T,
54 Status(T code, Strings d = {})
55 : type_(Type::none), code_(code), messages_(std::move(d))
56 {
57 }
58
59 Status(TER ter, Strings d = {})
60 : type_(Type::TER), code_(TERtoInt(ter)), messages_(std::move(d))
61 {
62 }
63
65 : type_(Type::error_code_i), code_(e), messages_(std::move(d))
66 {
67 }
68
71 {
72 }
73
74 /* Returns a representation of the integer status Code as a string.
75 If the Status is OK, the result is an empty string.
76 */
78 codeString() const;
79
81 operator bool() const
82 {
83 return code_ != OK;
84 }
85
87 bool
88 operator!() const
89 {
90 return !bool(*this);
91 }
92
95 TER
96 toTER() const
97 {
98 XRPL_ASSERT(
99 type_ == Type::TER, "ripple::RPC::Status::toTER : type is TER");
100 return TER::fromInt(code_);
101 }
102
107 {
108 XRPL_ASSERT(
110 "ripple::RPC::Status::toTER : type is error code");
111 return error_code_i(code_);
112 }
113
116 template <class Object>
117 void
118 inject(Object& object) const
119 {
120 if (auto ec = toErrorCode())
121 {
122 if (messages_.empty())
123 inject_error(ec, object);
124 else
125 inject_error(ec, message(), object);
126 }
127 }
128
129 Strings const&
130 messages() const
131 {
132 return messages_;
133 }
134
137 message() const;
138
139 Type
140 type() const
141 {
142 return type_;
143 }
144
146 toString() const;
147
151 void
153
154private:
158};
159
160} // namespace RPC
161} // namespace ripple
162
163#endif
Represents a JSON value.
Definition: json_value.h:148
static constexpr TERSubset fromInt(int from)
Definition: TER.h:411
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:223
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
error_code_i
Definition: ErrorCodes.h:40
constexpr TERUnderlyingType TERtoInt(TELcodes v)
Definition: TER.h:353
Status represents the results of an operation that might fail.
Definition: Status.h:40
static constexpr Code OK
Definition: Status.h:46
Strings const & messages() const
Definition: Status.h:130
error_code_i toErrorCode() const
Returns the Status as an error_code_i.
Definition: Status.h:106
Status(error_code_i e, std::string const &s)
Definition: Status.h:69
std::vector< std::string > Strings
Definition: Status.h:44
Status(TER ter, Strings d={})
Definition: Status.h:59
Status(error_code_i e, Strings d={})
Definition: Status.h:64
std::string toString() const
Definition: Status.cpp:92
void inject(Object &object) const
Apply the Status to a JsonObject.
Definition: Status.h:118
std::string message() const
Return the first message, if any.
Definition: Status.cpp:78
Status(T code, Strings d={})
Definition: Status.h:54
bool operator!() const
Returns true if the Status is OK.
Definition: Status.h:88
Type type() const
Definition: Status.h:140
Strings messages_
Definition: Status.h:157
TER toTER() const
Returns the Status as a TER.
Definition: Status.h:96
std::string codeString() const
Definition: Status.cpp:28
void fillJson(Json::Value &)
Fill a Json::Value with an RPC 2.0 response.
Definition: Status.cpp:59