1 // DBusException.hh --- DBUS interface
2 //
3 // Copyright (C) 2007, 2012, 2013 Rob Caelers <robc@krandor.nl>
4 // All rights reserved.
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 //
19 
20 #ifndef WORKRAVE_DBUS_DBUSEXCEPTION_HH
21 #define WORKRAVE_DBUS_DBUSEXCEPTION_HH
22 
23 #include <boost/exception/all.hpp>
24 
25 #include <iostream>
26 
27 #include "Exception.hh"
28 
29 namespace workrave
30 {
31   namespace dbus
32   {
33     extern const char *DBUS_ERROR_FAILED;
34     extern const char *DBUS_ERROR_NOT_SUPPORTED;
35     extern const char *DBUS_ERROR_INVALID_ARGS;
36     extern const char *DBUS_ERROR_UNKNOWN_METHOD;
37 
38     class DBusException : public Exception
39     {
40     public:
DBusException(const std::string & detail)41       explicit DBusException(const std::string &detail)
42         : Exception(detail)
43       {
44       }
45 
~DBusException()46       ~DBusException() override
47       {
48       }
49     };
50 
51     typedef boost::error_info<struct tag_message_info, std::string> message_info;
52     typedef boost::error_info<struct tag_error_code_info, std::string> error_code_info;
53     typedef boost::error_info<struct tag_oject_info, std::string> object_info;
54     typedef boost::error_info<struct tag_interface_info, std::string> interface_info;
55     typedef boost::error_info<struct tag_method_info, std::string> method_info;
56     typedef boost::error_info<struct tag_argument_info, std::string> argument_info;
57     typedef boost::error_info<struct tag_type_info, std::string> actual_type_info;
58     typedef boost::error_info<struct tag_expected_type_info, std::string> expected_type_info;
59     typedef boost::error_info<struct tag_field_info, std::string> field_info;
60     typedef boost::error_info<struct tag_parameter_info, std::string> parameter_info;
61 
62     typedef boost::error_info<struct tag_field_path_info, std::string> field_path_info;
63 
64     class DBusRemoteException : public virtual boost::exception, public virtual std::exception
65     {
66     public:
error()67       std::string error()
68       {
69         std::string ret;
70         if (const std::string* msg = boost::get_error_info<error_code_info>(*this))
71           {
72             ret = *msg;
73           }
74         return ret;
75       }
76 
operator <<(const field_info & rhs)77       DBusRemoteException& operator<< (const field_info& rhs)
78       {
79         if (const std::string* msg = boost::get_error_info<field_path_info>(*this))
80           {
81             *this << field_path_info(rhs.value() + "." + *msg);
82           }
83         else
84           {
85             *this << field_path_info(rhs.value());
86           }
87 
88         return *this;
89       }
90 
prepend_field(const std::string & field)91       void prepend_field(const std::string &field)
92       {
93         if (const std::string* msg = boost::get_error_info<field_path_info>(*this))
94           {
95             *this << field_path_info(field + "." + *msg);
96           }
97         else
98           {
99             *this << field_path_info(field);
100           }
101       }
102 
diag()103       std::string diag()
104       {
105         std::string ret;
106 
107         if (const std::string* msg = boost::get_error_info<message_info>(*this))
108           {
109             ret += *msg;
110           }
111         if (const std::string* msg = boost::get_error_info<object_info>(*this))
112           {
113             ret += " object path=" + *msg;
114           }
115         if (const std::string* msg = boost::get_error_info<interface_info>(*this))
116           {
117             ret += " interface=" + *msg;
118           }
119         if (const std::string* msg = boost::get_error_info<method_info>(*this))
120           {
121             ret += " method=" + *msg;
122           }
123         if (const std::string* msg = boost::get_error_info<argument_info>(*this))
124           {
125             ret += " argument=" + *msg;
126           }
127         if (const std::string* msg = boost::get_error_info<actual_type_info>(*this))
128           {
129             ret += " type=" + *msg;
130           }
131         if (const std::string* msg = boost::get_error_info<expected_type_info>(*this))
132           {
133             ret += " expected_type=" + *msg;
134           }
135         if (const std::string* msg = boost::get_error_info<field_path_info>(*this))
136           {
137             ret += " fieldpath=" + *msg;
138           }
139         if (const std::string* msg = boost::get_error_info<parameter_info>(*this))
140           {
141             ret += " parameter=" + *msg;
142           }
143 
144         return ret;
145       }
146     };
147   }
148 }
149 
150 #endif // WORKRAVE_DBUS_DBUSEXCEPTION_HH
151