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
37namespace Pt {
38
39namespace JsonRpc {
40
43class PT_JSONRPC_API Fault : public Remoting::Fault
44{
45 friend void operator >>=(const Pt::SerializationInfo&, Fault&);
46
47 public:
51 {
52 ParseError = -32700,
53 InvalidRequest = -32600,
54 MethodNotFound = -32601,
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
82inline 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
94inline 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
Fault(const char *msg, int rc)
Construct with message and error code.
~Fault()
Destructor.
Definition Fault.h:69
ErrorCodes
JSON-RPC fault error codes.
Definition Fault.h:51
@ InvalidRequest
Invalid JSON-RPC.
Definition Fault.h:53
@ ParseError
Parse error.
Definition Fault.h:52
@ MethodNotFound
Method not found in service.
Definition Fault.h:54
@ InvalidParameters
Invalid method parameters.
Definition Fault.h:55
@ InternalError
Internal error.
Definition Fault.h:56
int code() const
Returns the error code.
Definition Fault.h:74
Fault(const std::string &msg, int ec)
Construct with message and error code.
Remoting fault exception.
Definition Fault.h:43
Represents arbitrary types during serialization.
Definition SerializationInfo.h:59
void setString(const char *s)
Set to string value.
void getString(std::string &s, const TextCodec< Pt::Char, char > &codec) const
Get value as a string.
SerializationInfo & addMember(const std::string &name)
Add a struct member.
Definition SerializationInfo.h:449
const SerializationInfo & getMember(const std::string &name) const
Get a struct member.
Definition SerializationInfo.h:496
int_type int32_t
Signed 32-bit integer type.
Definition Api-Types.h:45
JSON-RPC 2.0 services and clients.
Core module.
Definition Allocator.h:33