Fault.h
1 /* Copyright (C) 2020 Marc Boris Duerner
2 
3  This library is free software; you can redistribute it and/or
4  modify it under the terms of the GNU Lesser General Public
5  License as published by the Free Software Foundation; either
6  version 2.1 of the License, or (at your option) any later version.
7 
8  As a special exception, you may use this file as part of a free
9  software library without restriction. Specifically, if other files
10  instantiate templates or use macros or inline functions from this
11  file, or you compile this file and link it with other files to
12  produce an executable, this file does not by itself cause the
13  resulting executable to be covered by the GNU General Public
14  License. This exception does not however invalidate any other
15  reasons why the executable file might be covered by the GNU Library
16  General Public License.
17 
18  This library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with this library; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26  MA 02110-1301 USA
27 */
28 
29 #ifndef Pt_JsonRpc_Fault_h
30 #define Pt_JsonRpc_Fault_h
31 
32 #include <Pt/JsonRpc/Api.h>
33 #include <Pt/Remoting/Fault.h>
34 #include <Pt/SerializationInfo.h>
35 #include <string>
36 
37 namespace Pt {
38 
39 namespace JsonRpc {
40 
43 class PT_JSONRPC_API Fault : public Remoting::Fault
44 {
45  friend void operator >>=(const Pt::SerializationInfo&, Fault&);
46 
47  public:
50  enum ErrorCodes
51  {
52  ParseError = -32700,
53  InvalidRequest = -32600,
54  MethodNotFound = -32601,
55  InvalidParameters = -32602,
56  InternalError = -32603,
57  };
58 
61  Fault(const std::string& msg, int ec);
62 
65  Fault(const char* msg, int rc);
66 
69  ~Fault() throw()
70  { }
71 
74  int code() const
75  { return _code; }
76 
77  private:
78  int _code;
79 };
80 
82 inline void operator >>=(const Pt::SerializationInfo& si, Fault& fault)
83 {
84  int code = 0;
85  std::string msg;
86 
87  si.getMember("code") >>= code;
88  si.getMember("message").getString(msg );
89 
90  fault = Fault(msg, code);
91 }
92 
94 inline void operator <<=(Pt::SerializationInfo& si, const Fault& fault)
95 {
96  si.addMember("code") <<= static_cast<Pt::int32_t>( fault.code() );
97  si.addMember("message").setString( fault.what() );
98 }
99 
100 } // namespace
101 
102 } // namespace
103 
104 #endif
Core module.
Definition: Allocator.h:33
Remoting fault exception.
Definition: Fault.h:43
Fault(const char *msg, int rc)
Construct with message and error code.
ErrorCodes
JSON-RPC fault error codes.
Definition: Fault.h:51
SerializationInfo & addMember(const std::string &name)
Add a struct member.
Definition: SerializationInfo.h:449
void getString(std::string &s, const TextCodec< Pt::Char, char > &codec) const
Get value as a string.
const SerializationInfo & getMember(const std::string &name) const
Get a struct member.
Definition: SerializationInfo.h:496
void setString(const char *s)
Set to string value.
int code() const
Returns the error code.
Definition: Fault.h:74
~Fault()
Destructor.
Definition: Fault.h:69
Fault(const std::string &msg, int ec)
Construct with message and error code.
int_type int32_t
Signed 32-bit integer type.
Definition: Api-Types.h:41
JSON-RPC fault exception.
Definition: Fault.h:44
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:59