jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
exception.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2011 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #ifndef JUBATUS_CORE_COMMON_EXCEPTION_HPP_
18 #define JUBATUS_CORE_COMMON_EXCEPTION_HPP_
19 
20 #include <string.h>
21 
22 #include <exception>
23 #include <stdexcept>
24 #include <ios>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 
29 #include "jubatus/util/lang/shared_ptr.h"
30 #include "jubatus/util/lang/demangle.h"
31 #include "exception_info.hpp"
32 
33 namespace jubatus {
34 namespace core {
35 namespace common {
36 namespace exception {
37 
42 inline std::string to_string(const error_errno& info) {
43  char buf[1024];
44 #if defined(__linux__)
45  std::string msg(strerror_r(info.value(), buf, sizeof(buf)));
46 #elif defined(__sparcv8) || defined(__sparcv9) || defined(__APPLE__)
47  strerror_r(info.value(), buf, sizeof(buf));
48  std::string msg(buf);
49 #else
50 #error cpp_strerror_r
51 #endif
52  msg += " (" +
53  jubatus::util::lang::lexical_cast<std::string>(info.value()) + ")";
54  return msg;
55 }
56 
60 
62 
64 };
65 // for exception_thrower
66 
67 #if defined(__GNUC__)
68 #define JUBATUS_ERROR_FUNC \
69  jubatus::core::common::exception::error_at_func(__PRETTY_FUNCTION__)
70 #else
71 #define JUBATUS_ERROR_FUNC \
72  jubatus::core::common::exception::error_at_func(__func__)
73 #endif
74 
75 #define JUBATUS_CURRENT_ERROR_INFO() \
76  jubatus::core::common::exception::error_at_file(__FILE__) \
77  << jubatus::core::common::exception::error_at_line(__LINE__) \
78  << JUBATUS_ERROR_FUNC << jubatus::core::common::exception::error_splitter()
79 #define JUBATUS_EXCEPTION(e) e << \
80  jubatus::core::common::exception::exception_thrower_binder_type() \
81  << JUBATUS_CURRENT_ERROR_INFO()
82 
84 typedef jubatus::util::lang::shared_ptr<exception_thrower_base>
86 
87 typedef std::vector<jubatus::util::lang::shared_ptr<error_info_base> >
89 
90 class jubatus_exception : public std::exception {
91  public:
92  jubatus_exception() throw () {
93  }
94  virtual ~jubatus_exception() throw () {
95  }
96 
97  virtual exception_thrower_ptr thrower() const = 0;
98 
99  template<class Exception>
100  friend const Exception& add_info(
101  const Exception& e,
102  jubatus::util::lang::shared_ptr<error_info_base> info);
103 
104  std::string name() const throw () {
105  // does not assume multithreading
106  if (exception_class_name_.empty()) {
107  exception_class_name_ = jubatus::util::lang::demangle(
108  typeid(*this).name());
109  }
110 
111  return exception_class_name_;
112  }
113 
114  virtual const char* what() const throw () {
115  name();
116  return exception_class_name_.c_str();
117  }
118 
120  std::string diagnostic_information(bool display_what = false) const;
121 
122  private:
123  mutable std::string exception_class_name_;
125 };
126 
127 template<class Exception>
128 inline const Exception& add_info(
129  const Exception& e,
130  jubatus::util::lang::shared_ptr<error_info_base> info) {
131  e.info_list_.push_back(info);
132  return e;
133 }
134 
135 template<class Exception, class Tag, class V>
136 inline const Exception& operator <<(
137  const Exception& e,
138  const error_info<Tag, V>& info) {
139  return add_info(
140  e,
141  jubatus::util::lang::shared_ptr<error_info_base>(
142  new error_info<Tag, V>(info)));
143 }
144 
145 template<class Exception>
146 inline const Exception& operator <<(
147  const Exception& e,
148  jubatus::util::lang::shared_ptr<error_info_base> info) {
149  return add_info(e, info);
150 }
151 
153  public:
155  }
157  }
158 
159  virtual void throw_exception() const = 0;
160 };
161 
162 template<class Exception>
164  public:
165  explicit exception_thrower_impl(const Exception& e)
166  : exception_(e) {
167  }
168 
169  private:
170  // noncopyable
173 
174  public:
175  void throw_exception() const {
176  throw exception_;
177  }
178 
179  private:
180  Exception exception_;
181 };
182 
183 template<class Exception>
185  public:
187  }
188  virtual ~jubaexception() throw () {
189  }
190 
191  exception_thrower_ptr thrower() const {
192  if (thrower_) {
193  return thrower_;
194  } else {
195  return exception_thrower_ptr(
197  *(static_cast<const Exception*>(this))));
198  }
199  }
200 
201  // This is desireble in private
202  void bind_thrower(exception_thrower_ptr thrower) const {
203  thrower_ = thrower;
204  }
205 
206  private:
207  mutable exception_thrower_ptr thrower_;
208 };
209 
210 template<class Exception>
211 inline const Exception& operator <<(
212  const Exception& e,
214  e.bind_thrower(
216  return e;
217 }
218 
219 class unknown_exception : public jubaexception<unknown_exception> {
220  public:
221  explicit unknown_exception() {
222  // TODO(kashihara): push unknown_exception
223  }
224 
225  const char* what() const throw () {
226  return "unknown exception";
227  }
228 };
229 
230 class runtime_error : public jubaexception<runtime_error> {
231  public:
232  explicit runtime_error(const std::string& what)
233  : what_(what) {
234  }
235 
236  ~runtime_error() throw () {
237  }
238 
239  const char* what() const throw () {
240  return what_.c_str();
241  }
242  private:
243  std::string what_;
244 };
245 
246 namespace detail {
247 template<class Exception>
248 exception_thrower_ptr current_std_exception(const Exception& e) {
250 }
251 
252 } // namespace detail
253 
254 // Don't call without catch blocks
255 inline exception_thrower_ptr get_current_exception() {
256  exception_thrower_ptr ptr;
257 
258  try {
259  throw;
260  } catch (const std::bad_alloc& e) { // exception
262  } catch (const std::bad_cast& e) { // exception
264  } catch (const std::bad_exception& e) { // exception
266  } catch (const std::bad_typeid& e) { // exception
268  } catch (const std::domain_error& e) { // logic_error
270  } catch (const std::invalid_argument& e) { // logic_error
272  } catch (const std::length_error& e) { // logic_error
274  } catch (const std::out_of_range& e) { // logic_error
276  } catch (const std::logic_error& e) { // exception
278  } catch (const std::range_error& e) { // runtime_error
280  } catch (const std::overflow_error& e) { // runtime_error
282  } catch (const std::underflow_error& e) { // runtime_error
284  } catch (const std::ios_base::failure& e) { // exception
286  } catch (const std::runtime_error& e) { // exception
288  } catch (const jubatus_exception& e) {
289  ptr = e.thrower();
290  } catch (const std::exception& e) {
292  } catch (...) {
293  ptr = unknown_exception().thrower();
294  }
295 
296  return ptr;
297 }
298 
299 } // namespace exception
300 
301 class config_exception : public exception::jubaexception<config_exception> {
302 };
303 
304 class storage_not_set : public exception::jubaexception<storage_not_set> {
305 };
306 
307 class config_not_set : public exception::jubaexception<config_not_set> {
308  const char* what() const throw () {
309  return "config_not_set";
310  }
311 };
312 
314  public:
315  explicit unsupported_method(const std::string& n)
316  : jubatus::core::common::exception::runtime_error(
317  std::string("unsupported method (") + n + ")") {
318  }
319 };
320 
322  public:
323  explicit bad_storage_type(const std::string& n)
324  : jubatus::core::common::exception::runtime_error(n) {
325  }
326 };
327 
329  public:
330  explicit membership_error(const std::string& n)
331  : jubatus::core::common::exception::runtime_error(n) {
332  }
333 };
334 
335 class not_found : public membership_error {
336  public:
337  explicit not_found(const std::string& n)
338  : membership_error(n) {
339  }
340 };
341 
343  public:
344  explicit argv_error(const std::string& n)
345  : jubatus::core::common::exception::runtime_error(n) {
346  }
347 };
348 
350  public:
351  explicit invalid_parameter(const std::string& expected)
352  : jubatus::core::common::exception::runtime_error(
353  std::string("invalid parameter (expected: ") + expected + ")") {
354  }
355 };
356 
357 } // namespace common
358 } // namespace core
359 } // namespace jubatus
360 
361 #endif // JUBATUS_CORE_COMMON_EXCEPTION_HPP_
const Exception & add_info(const Exception &e, jubatus::util::lang::shared_ptr< error_info_base > info)
Definition: exception.hpp:128
bad_storage_type(const std::string &n)
Definition: exception.hpp:323
std::string to_string(const error_errno &info)
Definition: exception.hpp:42
error_info< struct error_api_func_, std::string > error_api_func
Definition: exception.hpp:58
const Exception & operator<<(const Exception &e, const error_info< Tag, V > &info)
Definition: exception.hpp:136
std::vector< jubatus::util::lang::shared_ptr< error_info_base > > error_info_list_t
Definition: exception.hpp:88
argv_error(const std::string &n)
Definition: exception.hpp:344
void bind_thrower(exception_thrower_ptr thrower) const
Definition: exception.hpp:202
not_found(const std::string &n)
Definition: exception.hpp:337
unsupported_method(const std::string &n)
Definition: exception.hpp:315
error_info< struct error_file_name_, std::string > error_file_name
Definition: exception.hpp:57
exception_thrower_ptr thrower() const
Definition: exception.hpp:191
error_info< struct error_at_line_, int > error_at_line
Definition: exception.hpp:40
virtual exception_thrower_ptr thrower() const =0
jubatus::util::lang::shared_ptr< exception_thrower_base > exception_thrower_ptr
Definition: exception.hpp:83
error_info< struct error_at_file_, std::string > error_at_file
Definition: exception.hpp:38
error_info< struct error_message_, std::string > error_message
Definition: exception.hpp:59
exception_thrower_ptr current_std_exception(const Exception &e)
Definition: exception.hpp:248
membership_error(const std::string &n)
Definition: exception.hpp:330
friend const Exception & add_info(const Exception &e, jubatus::util::lang::shared_ptr< error_info_base > info)
Definition: exception.hpp:128
error_info< struct error_errno_, int > error_errno
Definition: exception.hpp:41
invalid_parameter(const std::string &expected)
Definition: exception.hpp:351
exception_thrower_ptr get_current_exception()
Definition: exception.hpp:255
error_info< struct error_at_func_, std::string > error_at_func
Definition: exception.hpp:39
exception_thrower_impl & operator=(const exception_thrower_impl &)
std::string diagnostic_information(bool display_what=false) const
Definition: exception.cpp:31
error_info< struct error_splitter_, void > error_splitter
Definition: exception.hpp:61