rippled
Status.cpp
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 #include <ripple/rpc/Status.h>
21 #include <sstream>
22 
23 namespace ripple {
24 namespace RPC {
25 
28 {
29  if (!*this)
30  return "";
31 
32  if (type_ == Type::none)
33  return std::to_string(code_);
34 
35  if (type_ == Status::Type::TER)
36  {
37  std::string s1, s2;
38 
39  auto success = transResultInfo(toTER(), s1, s2);
40  assert(success);
41  (void)success;
42 
43  return s1 + ": " + s2;
44  }
45 
47  {
48  auto info = get_error_info(toErrorCode());
49  std::ostringstream sStr;
50  sStr << info.token.c_str() << ": " << info.message.c_str();
51  return sStr.str();
52  }
53 
54  assert(false);
55  return "";
56 }
57 
58 void
60 {
61  if (!*this)
62  return;
63 
64  auto& error = value[jss::error];
65  error[jss::code] = code_;
66  error[jss::message] = codeString();
67 
68  // Are there any more messages?
69  if (!messages_.empty())
70  {
71  auto& messages = error[jss::data];
72  for (auto& i : messages_)
73  messages.append(i);
74  }
75 }
76 
79 {
80  std::string result;
81  for (auto& m : messages_)
82  {
83  if (!result.empty())
84  result += '/';
85  result += m;
86  }
87 
88  return result;
89 }
90 
93 {
94  if (*this)
95  return codeString() + ":" + message();
96  return "";
97 }
98 
99 } // namespace RPC
100 } // namespace ripple
ripple::RPC::Status::messages_
Strings messages_
Definition: Status.h:154
sstream
std::string
STL class.
ripple::RPC::get_error_info
ErrorInfo const & get_error_info(error_code_i code)
Returns an ErrorInfo that reflects the error code.
Definition: ErrorCodes.cpp:170
ripple::RPC::Status::codeString
std::string codeString() const
Definition: Status.cpp:27
ripple::RPC::Status::messages
Strings const & messages() const
Definition: Status.h:127
ripple::RPC::Status::toErrorCode
error_code_i toErrorCode() const
Returns the Status as an error_code_i.
Definition: Status.h:105
ripple::RPC::Status::Type::TER
@ TER
std::to_string
T to_string(T... args)
ripple::RPC::Status::Type::error_code_i
@ error_code_i
ripple::RPC::Status::code_
Code code_
Definition: Status.h:153
std::ostringstream
STL class.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::transResultInfo
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition: TER.cpp:195
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
std::vector::empty
T empty(T... args)
ripple::RPC::Status::Type::none
@ none
std::ostringstream::str
T str(T... args)
ripple::RPC::Status::fillJson
void fillJson(Json::Value &)
Fill a Json::Value with an RPC 2.0 response.
Definition: Status.cpp:59
ripple::RPC::Status::toString
std::string toString() const
Definition: Status.cpp:92
Json::Value
Represents a JSON value.
Definition: json_value.h:145