MIRA
JSONRPCResponse.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_JSONRPCRESPONSE_H_
48 #define _MIRA_JSONRPCRESPONSE_H_
49 
50 #include <json/JSON.h>
51 #include <rpc/RPCError.h>
52 
53 namespace mira {
54 
56 
62 {
63 public:
65  JSONRPCResponse(const json::Value& iResponse) :
66  response(iResponse) {}
67 
74  {
75  auto it = response.get_obj().find("error");
76  if(it!=response.get_obj().end()) {
77  auto& obj = it->second;
78  auto messageit = obj.get_obj().find("message");
79  if(messageit==obj.get_obj().end()) {
80  MIRA_THROW(XRPC, "RPC client response has no correct error format: message missing");
81  }
82  auto stackit = obj.get_obj().find("callstack");
83  // if there is no callstack, throw with local callstack info
84  if(stackit==obj.get_obj().end())
85  MIRA_THROW(XRPC, messageit->second.get_str());
86 
87  auto threadit = obj.get_obj().find("thread");
88  if(threadit==obj.get_obj().end())
89  MIRA_THROW(XRPC, "RPC client response has no correct error format: callstack without thread id");
90 
91  CallStack stack;
92  ThreadID thread;
93 
94  try {
95  JSONDeserializer js(stackit->second);
96  js.deserialize(stack);
97  }
98  catch(XIO&) {
99  MIRA_THROW(XRPC, "RPC client response has no correct error format: callstack unreadable");
100  }
101 
102  try {
103  thread = threadit->second.get_uint64();
104  }
105  catch(std::runtime_error&) {
106  MIRA_THROW(XRPC, "RPC client response has no correct error format: thread id unreadable");
107  }
108 
109  XRPC ex(messageit->second.get_str());
110  ex.addExternalStackInfo<XRPC>(stack, thread);
111 
112  // try to deserialize and add the original exception
113  auto excit = obj.get_obj().find("exception");
114  if(excit!=obj.get_obj().end()) {
115  try {
116  SerializableException* origEx;
117  JSONDeserializer js(excit->second);
118  js.deserialize(origEx);
119  ex.setOrigException(origEx);
120  }
121  catch(...) {} // ignore errors, it could e.g. be an unknown exception type
122  }
123 
124  throw ex;
125  }
126 
127  it = response.get_obj().find("result");
128  if(it==response.get_obj().end()) {
129  MIRA_THROW(XRPC, "RPC client response has no correct result format");
130  }
131  return it->second;
132  }
133 
135 };
136 
138 
139 }
140 
141 #endif
void deserialize(T &value)
Definition: JSONSerializer.h:427
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Error codes for reasons of errors/exceptions while processing an rpc call.
uint32 ThreadID
Platform independent thread ID.
Definition: ThreadID.h:68
json::Value getResult() const
Return the result of the RPC call.
Definition: JSONRPCResponse.h:73
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
Wraps a JSON RPC call response.
Definition: JSONRPCResponse.h:61
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:400
An exception that is thrown by the RPCServer if an RPC call fails.
Definition: RPCError.h:76
json_spirit::mValue Value
A value is an abstract description of data in JSON (underlying data can either be one of the JSON bas...
Definition: JSON.h:176
JSONRPCResponse()
Definition: JSONRPCResponse.h:64
Encapsulates unix call stack functionality.
Definition: CallStack.h:86
json::Value response
The complete JSON RPC 2.0 response.
Definition: JSONRPCResponse.h:134
Wrappers for JSON.
Definition: Exceptions.h:85
JSONRPCResponse(const json::Value &iResponse)
Definition: JSONRPCResponse.h:65