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/TER.h>
24 #include <ripple/protocol/ErrorCodes.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 <typename T,
53  Status (T code, Strings d = {})
54  : type_ (Type::none), code_ (code), messages_ (std::move (d))
55  {
56  }
57 
58  Status (TER ter, Strings d = {})
59  : type_ (Type::TER), code_ (TERtoInt (ter)), messages_ (std::move (d))
60  {
61  }
62 
64  : type_ (Type::error_code_i), code_ (e), messages_ (std::move (d))
65  {
66  }
67 
69  : type_ (Type::error_code_i), code_ (e), messages_ ({s})
70  {
71  }
72 
73  /* Returns a representation of the integer status Code as a string.
74  If the Status is OK, the result is an empty string.
75  */
76  std::string codeString () const;
77 
79  operator bool() const
80  {
81  return code_ != OK;
82  }
83 
85  bool operator !() const
86  {
87  return ! bool (*this);
88  }
89 
92  TER toTER () const
93  {
94  assert (type_ == Type::TER);
95  return TER::fromInt (code_);
96  }
97 
101  {
102  assert (type_ == Type::error_code_i);
103  return error_code_i (code_);
104  }
105 
108  template <class Object>
109  void inject (Object& object) const
110  {
111  if (auto ec = toErrorCode())
112  {
113  if (messages_.empty())
114  inject_error (ec, object);
115  else
116  inject_error (ec, message(), object);
117  }
118  }
119 
120  Strings const& messages() const
121  {
122  return messages_;
123  }
124 
126  std::string message() const;
127 
128  Type type() const
129  {
130  return type_;
131  }
132 
133  std::string toString() const;
134 
138  void fillJson(Json::Value&);
139 
140 private:
144 };
145 
146 } // namespace RPC
147 } // ripple
148 
149 #endif
ripple::RPC::Status::OK
static constexpr Code OK
Definition: Status.h:46
ripple::RPC::Status::messages_
Strings messages_
Definition: Status.h:143
std::string
STL class.
std::exception
STL class.
ripple::RPC::Status::Status
Status(T code, Strings d={})
Definition: Status.h:53
std::vector< std::string >
ripple::RPC::Status::codeString
std::string codeString() const
Definition: Status.cpp:28
ripple::TERtoInt
constexpr TERUnderlyingType TERtoInt(TELcodes v)
Definition: TER.h:289
ripple::RPC::Status::messages
Strings const & messages() const
Definition: Status.h:120
ripple::RPC::Status::operator!
bool operator!() const
Returns true if the Status is OK.
Definition: Status.h:85
ripple::RPC::Status::Status
Status(error_code_i e, std::string const &s)
Definition: Status.h:68
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::RPC::Status::Type
Type
Definition: Status.h:42
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:100
std::enable_if_t
ripple::RPC::Status::Type::TER
@ TER
ripple::RPC::Status::Status
Status(TER ter, Strings d={})
Definition: Status.h:58
ripple::TERSubset< CanCvtToTER >
ripple::RPC::Status::Type::error_code_i
@ error_code_i
ripple::RPC::Status::code_
Code code_
Definition: Status.h:142
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:109
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:92
ripple::RPC::Status::type_
Type type_
Definition: Status.h:141
ripple::RPC::Status::message
std::string message() const
Return the first message, if any.
Definition: Status.cpp:77
ripple::RPC::Status::type
Type type() const
Definition: Status.h:128
ripple::TERSubset< CanCvtToTER >::fromInt
static constexpr TERSubset fromInt(int from)
Definition: TER.h:323
cassert
std::vector::empty
T empty(T... args)
ripple::RPC::Status::Status
Status(error_code_i e, Strings d={})
Definition: Status.h:63
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:185
ripple::RPC::Status::toString
std::string toString() const
Definition: Status.cpp:89
Json::Value
Represents a JSON value.
Definition: json_value.h:141