rippled
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 <ripple/protocol/ErrorCodes.h>
24 #include <ripple/protocol/TER.h>
25 #include <cassert>
26 
27 namespace ripple {
28 namespace RPC {
29 
39 struct Status : public std::exception
40 {
41 public:
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 
70  : type_(Type::error_code_i), code_(e), messages_({s})
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  assert(type_ == Type::TER);
99  return TER::fromInt(code_);
100  }
101 
105  toErrorCode() const
106  {
107  assert(type_ == Type::error_code_i);
108  return error_code_i(code_);
109  }
110 
113  template <class Object>
114  void
115  inject(Object& object) const
116  {
117  if (auto ec = toErrorCode())
118  {
119  if (messages_.empty())
120  inject_error(ec, object);
121  else
122  inject_error(ec, message(), object);
123  }
124  }
125 
126  Strings const&
127  messages() const
128  {
129  return messages_;
130  }
131 
134  message() const;
135 
136  Type
137  type() const
138  {
139  return type_;
140  }
141 
143  toString() const;
144 
148  void
150 
151 private:
155 };
156 
157 } // namespace RPC
158 } // namespace ripple
159 
160 #endif
ripple::RPC::Status::messages_
Strings messages_
Definition: Status.h:154
std::string
STL class.
std::exception
STL class.
ripple::RPC::Status::Status
Status(T code, Strings d={})
Definition: Status.h:54
std::vector< std::string >
ripple::RPC::Status::codeString
std::string codeString() const
Definition: Status.cpp:27
ripple::TERtoInt
constexpr TERUnderlyingType TERtoInt(TELcodes v)
Definition: TER.h:301
ripple::RPC::Status::messages
Strings const & messages() const
Definition: Status.h:127
ripple::RPC::Status::operator!
bool operator!() const
Returns true if the Status is OK.
Definition: Status.h:88
ripple::RPC::Status::Status
Status(error_code_i e, std::string const &s)
Definition: Status.h:69
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::RPC::Status::Type
Type
Definition: Status.h:42
ripple::RPC::Status::OK
static constexpr Code OK
Definition: Status.h:46
ripple::RPC::Status::Status
Status()=default
ripple::RPC::Status::toErrorCode
error_code_i toErrorCode() const
Returns the Status as an error_code_i.
Definition: Status.h:105
std::enable_if_t
ripple::RPC::Status::Type::TER
@ TER
ripple::RPC::Status::Status
Status(TER ter, Strings d={})
Definition: Status.h:59
ripple::TERSubset< CanCvtToTER >
ripple::RPC::Status::Strings
std::vector< std::string > Strings
Definition: Status.h:44
ripple::RPC::Status::Type::error_code_i
@ error_code_i
ripple::RPC::Status::code_
Code code_
Definition: Status.h:153
ripple::RPC::Status::Code
int Code
Definition: Status.h:43
ripple::RPC::Status::inject
void inject(Object &object) const
Apply the Status to a JsonObject.
Definition: Status.h:115
ripple::RPC::Status
Status represents the results of an operation that might fail.
Definition: Status.h:39
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RPC::Status::toTER
TER toTER() const
Returns the Status as a TER.
Definition: Status.h:96
ripple::RPC::Status::type_
Type type_
Definition: Status.h:152
ripple::RPC::Status::message
std::string message() const
Return the first message, if any.
Definition: Status.cpp:78
ripple::RPC::Status::type
Type type() const
Definition: Status.h:137
ripple::TERSubset< CanCvtToTER >::fromInt
static constexpr TERSubset fromInt(int from)
Definition: TER.h:359
cassert
std::vector::empty
T empty(T... args)
ripple::RPC::Status::Status
Status(error_code_i e, Strings d={})
Definition: Status.h:64
ripple::RPC::Status::Type::none
@ none
ripple::RPC::Status::fillJson
void fillJson(Json::Value &)
Fill a Json::Value with an RPC 2.0 response.
Definition: Status.cpp:59
ripple::RPC::inject_error
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition: ErrorCodes.h:212
ripple::RPC::Status::toString
std::string toString() const
Definition: Status.cpp:92
Json::Value
Represents a JSON value.
Definition: json_value.h:145