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#include <tuple>
25
26namespace ripple {
27namespace test {
28namespace jtx {
29
33class rpc
34{
35private:
40
41public:
44 : code_(code), errorMessage_(m)
45 {
46 }
47
49 explicit rpc(
50 std::string error,
51 std::optional<std::string> exceptionMessage = {})
52 : error_(error), errorException_(exceptionMessage)
53 {
54 }
55
56 void
57 operator()(Env&, JTx& jt) const
58 {
59 // The RPC request should fail. RPC errors result in telENV_RPC_FAILED.
61 if (code_)
62 {
63 auto const& errorInfo = RPC::get_error_info(*code_);
64 // When an RPC request returns an error code ('error_code'), it
65 // always includes an error message ('error_message'), and sometimes
66 // includes an error token ('error'). If it does, the error token is
67 // always obtained from the lookup into the ErrorInfo lookup table.
68 //
69 // Take advantage of that fact to populate jt.rpcException. The
70 // check will be aware of whether the rpcExcpetion can be safely
71 // ignored.
72 jt.rpcCode = {
73 *code_,
74 errorMessage_ ? *errorMessage_ : errorInfo.message.c_str()};
75 jt.rpcException = {errorInfo.token.c_str(), std::nullopt};
76 }
77 if (error_)
79 }
80};
81
82} // namespace jtx
83} // namespace test
84} // namespace ripple
85
86#endif
A transaction testing environment.
Definition: Env.h:117
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition: rpc.h:34
void operator()(Env &, JTx &jt) const
Definition: rpc.h:57
std::optional< std::string > error_
Definition: rpc.h:38
std::optional< std::string > errorException_
Definition: rpc.h:39
rpc(error_code_i code, std::optional< std::string > m={})
If there's an error code, we expect an error message.
Definition: rpc.h:43
std::optional< std::string > errorMessage_
Definition: rpc.h:37
std::optional< error_code_i > code_
Definition: rpc.h:36
rpc(std::string error, std::optional< std::string > exceptionMessage={})
If there is not a code, we expect an exception message.
Definition: rpc.h:49
ErrorInfo const & get_error_info(error_code_i code)
Returns an ErrorInfo that reflects the error code.
Definition: ErrorCodes.cpp:173
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:44
std::optional< std::pair< error_code_i, std::string > > rpcCode
Definition: JTx.h:48
std::optional< std::pair< std::string, std::optional< std::string > > > rpcException
Definition: JTx.h:50
std::optional< TER > ter
Definition: JTx.h:47