rippled
Loading...
Searching...
No Matches
rpc.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 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_TEST_JTX_RPC_H_INCLUDED
21#define RIPPLE_TEST_JTX_RPC_H_INCLUDED
22
23#include <test/jtx/Env.h>
24
25#include <tuple>
26
27namespace ripple {
28namespace test {
29namespace jtx {
30
34class rpc
35{
36private:
41
42public:
45 : code_(code), errorMessage_(m)
46 {
47 }
48
50 explicit rpc(
51 std::string error,
52 std::optional<std::string> exceptionMessage = {})
53 : error_(error), errorException_(exceptionMessage)
54 {
55 }
56
57 void
58 operator()(Env&, JTx& jt) const
59 {
60 // The RPC request should fail. RPC errors result in telENV_RPC_FAILED.
62 if (code_)
63 {
64 auto const& errorInfo = RPC::get_error_info(*code_);
65 // When an RPC request returns an error code ('error_code'), it
66 // always includes an error message ('error_message'), and sometimes
67 // includes an error token ('error'). If it does, the error token is
68 // always obtained from the lookup into the ErrorInfo lookup table.
69 //
70 // Take advantage of that fact to populate jt.rpcException. The
71 // check will be aware of whether the rpcExcpetion can be safely
72 // ignored.
73 jt.rpcCode = {
74 *code_,
75 errorMessage_ ? *errorMessage_ : errorInfo.message.c_str()};
76 jt.rpcException = {errorInfo.token.c_str(), std::nullopt};
77 }
78 if (error_)
80 }
81};
82
83} // namespace jtx
84} // namespace test
85} // namespace ripple
86
87#endif
A transaction testing environment.
Definition: Env.h:121
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition: rpc.h:35
void operator()(Env &, JTx &jt) const
Definition: rpc.h:58
std::optional< std::string > error_
Definition: rpc.h:39
std::optional< std::string > errorException_
Definition: rpc.h:40
rpc(error_code_i code, std::optional< std::string > m={})
If there's an error code, we expect an error message.
Definition: rpc.h:44
std::optional< std::string > errorMessage_
Definition: rpc.h:38
std::optional< error_code_i > code_
Definition: rpc.h:37
rpc(std::string error, std::optional< std::string > exceptionMessage={})
If there is not a code, we expect an exception message.
Definition: rpc.h:50
ErrorInfo const & get_error_info(error_code_i code)
Returns an ErrorInfo that reflects the error code.
Definition: ErrorCodes.cpp:179
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
@ telENV_RPC_FAILED
Definition: TER.h:68
error_code_i
Definition: ErrorCodes.h:40
Execution context for applying a JSON transaction.
Definition: JTx.h:45
std::optional< std::pair< error_code_i, std::string > > rpcCode
Definition: JTx.h:49
std::optional< std::pair< std::string, std::optional< std::string > > > rpcException
Definition: JTx.h:51
std::optional< TER > ter
Definition: JTx.h:48