rippled
Loading...
Searching...
No Matches
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 <xrpld/rpc/Status.h>
21
22#include <sstream>
23
24namespace ripple {
25namespace RPC {
26
29{
30 if (!*this)
31 return "";
32
33 if (type_ == Type::none)
34 return std::to_string(code_);
35
37 {
38 std::string s1, s2;
39
40 [[maybe_unused]] auto const success = transResultInfo(toTER(), s1, s2);
41 XRPL_ASSERT(success, "ripple::RPC::codeString : valid TER result");
42
43 return s1 + ": " + s2;
44 }
45
47 {
48 auto info = get_error_info(toErrorCode());
50 sStr << info.token.c_str() << ": " << info.message.c_str();
51 return sStr.str();
52 }
53
54 UNREACHABLE("ripple::RPC::codeString : invalid type");
55 return "";
56}
57
58void
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
Represents a JSON value.
Definition json_value.h:149
T empty(T... args)
ErrorInfo const & get_error_info(error_code_i code)
Returns an ErrorInfo that reflects the error code.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition TER.cpp:249
T str(T... args)
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
std::string toString() const
Definition Status.cpp:92
std::string message() const
Return the first message, if any.
Definition Status.cpp:78
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
T to_string(T... args)