0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Error.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
29 #ifndef HYPERTABLE_ERROR_H
30 #define HYPERTABLE_ERROR_H
31 
32 #include "Common/String.h"
33 #include <ostream>
34 #include <stdexcept>
35 
36 namespace Hypertable {
37 
42  namespace Error {
43  enum Code {
44  UNPOSSIBLE = -3,
45  EXTERNAL = -2,
47  OK = 0,
56  BAD_KEY = 9,
80  CANCELLED = 33,
97  CLOSED = 50,
109  BAD_VALUE = 62,
113 
118 
119  COMM_NOT_CONNECTED = 0x00010001,
121  COMM_CONNECT_ERROR = 0x00010003,
123 
124  COMM_SEND_ERROR = 0x00010006,
125  COMM_RECEIVE_ERROR = 0x00010007,
126  COMM_POLL_ERROR = 0x00010008,
128  COMM_SOCKET_ERROR = 0x0001000A,
129  COMM_BIND_ERROR = 0x0001000B,
130  COMM_LISTEN_ERROR = 0x0001000C,
133  COMM_BAD_HEADER = 0x0001000F,
134  COMM_INVALID_PROXY = 0x00010010,
135 
137  FSBROKER_IO_ERROR = 0x00020002,
139  FSBROKER_BAD_FILENAME = 0x00020004,
143  FSBROKER_EOF = 0x00020008,
144 
145  HYPERSPACE_IO_ERROR = 0x00030001,
160  HYPERSPACE_NOT_LOCKED = 0x00030010,
166  HYPERSPACE_FILE_OPEN = 0x00030016,
170 
188 
190 
191  MASTER_TABLE_EXISTS = 0x00040001,
192  MASTER_BAD_SCHEMA = 0x00040002,
193  MASTER_NOT_RUNNING = 0x00040003,
204 
240 
242  HQL_BAD_COMMAND = 0x00060002,
243 
244  METALOG_ERROR = 0x00070000,
246  METALOG_BAD_RS_HEADER = 0x00070002,
247  METALOG_BAD_HEADER = 0x00070003,
254  METALOG_READ_ERROR = 0x0007000A,
255 
260 
265  };
266 
272  const char *get_text(int error);
273 
278  void generate_html_error_code_documentation(std::ostream &out);
279 
280  } // namespace Error
281 
282 
283  class Exception;
284 
288 
289  std::ostream &render(std::ostream &out) const;
290 
291  const Exception &ex;
292  };
293 
300  ExceptionMessagesRenderer(const Exception &e, const char *sep = ": ")
301  : ex(e), separator(sep) { }
302 
303  std::ostream &render(std::ostream &out) const;
304 
305  const Exception &ex;
306  const char *separator;
307  };
308 
314  class Exception : public std::runtime_error {
316  const Exception &operator=(const Exception &);
317 
319  int m_error;
320 
322  int m_line;
323 
325  const char *m_func;
326 
328  const char *m_file;
329 
330  public:
331  typedef std::runtime_error Parent;
332 
340  Exception(int error, int l = 0, const char *fn = 0, const char *fl = 0)
341  : Parent(""), m_error(error), m_line(l), m_func(fn), m_file(fl), prev(0) {
342  }
343 
352  Exception(int error, const String &msg, int l = 0, const char *fn = 0,
353  const char *fl = 0)
354  : Parent(msg), m_error(error), m_line(l), m_func(fn), m_file(fl),
355  prev(0) {
356  }
357 
367  Exception(int error, const String &msg, const Exception &ex, int l = 0,
368  const char *fn = 0, const char *fl = 0)
369  : Parent(msg), m_error(error), m_line(l), m_func(fn), m_file(fl),
370  prev(new Exception(ex)) {
371  }
372 
377  Exception(const Exception &ex)
378  : Parent(ex), m_error(ex.m_error), m_line(ex.m_line), m_func(ex.m_func),
379  m_file(ex.m_file) {
380  prev = ex.prev ? new Exception(*ex.prev) : 0;
381  }
382 
384  ~Exception() throw() { delete prev; prev = 0; }
385 
391  int code() const { return m_error; }
392 
397  int line() const { return m_line; }
398 
403  const char *func() const { return m_func; }
404 
409  const char *file() const { return m_file; }
410 
415  virtual std::ostream &render_message(std::ostream &out) const {
416  return out << what(); // override for custom exceptions
417  }
418 
419  // render messages for the entire exception chain
425  virtual std::ostream &render_messages(std::ostream &out,
426  const char *sep) const;
427 
430  return ExceptionMessageRenderer(*this);
431  }
432 
434  ExceptionMessagesRenderer messages(const char *sep = ": ") const {
435  return ExceptionMessagesRenderer(*this, sep);
436  }
437 
440  };
441 
443  std::ostream &operator<<(std::ostream &out, const Exception &);
444 
446  inline std::ostream &
447  ExceptionMessageRenderer::render(std::ostream &out) const {
448  return ex.render_message(out);
449  }
450 
452  inline std::ostream &
453  ExceptionMessagesRenderer::render(std::ostream &out) const {
454  return ex.render_messages(out, separator);
455  }
456 
458  inline std::ostream &
459  operator<<(std::ostream &out, const ExceptionMessageRenderer &r) {
460  return r.render(out);
461  }
462 
464  inline std::ostream &
465  operator<<(std::ostream &out, const ExceptionMessagesRenderer &r) {
466  return r.render(out);
467  }
468 
469 /* Convenience macro to create an exception stack trace */
470 #define HT_EXCEPTION(_code_, _msg_) \
471  Exception(_code_, _msg_, __LINE__, HT_FUNC, __FILE__)
472 
473 /* Convenience macro to create an chained exception */
474 #define HT_EXCEPTION2(_code_, _ex_, _msg_) \
475  Exception(_code_, _msg_, _ex_, __LINE__, HT_FUNC, __FILE__)
476 
477 /* Convenience macro to throw an exception */
478 #define HT_THROW(_code_, _msg_) throw HT_EXCEPTION(_code_, _msg_)
479 
480 /* Convenience macro to throw an exception */
481 #define HT_THROW_(_code_) HT_THROW(_code_, "")
482 
483 /* Convenience macro to throw a chained exception */
484 #define HT_THROW2(_code_, _ex_, _msg_) throw HT_EXCEPTION2(_code_, _ex_, _msg_)
485 
486 /* Convenience macro to throw a chained exception */
487 #define HT_THROW2_(_code_, _ex_) HT_THROW2(_code_, _ex_, "")
488 
489 /* Convenience macro to throw an exception with a printf-like message */
490 #define HT_THROWF(_code_, _fmt_, ...) \
491  throw HT_EXCEPTION(_code_, Hypertable::format(_fmt_, __VA_ARGS__))
492 
493 /* Convenience macro to throw a chained exception with a printf-like message */
494 #define HT_THROW2F(_code_, _ex_, _fmt_, ...) \
495  throw HT_EXCEPTION2(_code_, _ex_, Hypertable::format(_fmt_, __VA_ARGS__))
496 
497 /* Convenience macro to catch and rethrow exceptions with a printf-like
498  * message */
499 #define HT_RETHROWF(_fmt_, ...) \
500  catch (Exception &e) { HT_THROW2F(e.code(), e, _fmt_, __VA_ARGS__); } \
501  catch (std::bad_alloc &e) { \
502  HT_THROWF(Error::BAD_MEMORY_ALLOCATION, _fmt_, __VA_ARGS__); \
503  } \
504  catch (std::exception &e) { \
505  HT_THROWF(Error::EXTERNAL, "caught std::exception: %s " _fmt_, e.what(), \
506  __VA_ARGS__); \
507  } \
508  catch (...) { \
509  HT_ERRORF("caught unknown exception " _fmt_, __VA_ARGS__); \
510  throw; \
511  }
512 
513 /* Convenience macro to catch and rethrow exceptions */
514 #define HT_RETHROW(_s_) HT_RETHROWF("%s", _s_)
515 
516 /* Convenience macro to execute a code block and rethrow all exceptions */
517 #define HT_TRY(_s_, _code_) do { \
518  try { _code_; } \
519  HT_RETHROW(_s_) \
520 } while (0)
521 
522 /* Convenience macros for catching and logging exceptions in destructors */
523 #define HT_LOG_EXCEPTION(_s_) \
524  catch (Exception &e) { HT_ERROR_OUT << e <<", "<< _s_ << HT_END; } \
525  catch (std::bad_alloc &e) { \
526  HT_ERROR_OUT << "Out of memory, " << _s_ << HT_END; } \
527  catch (std::exception &e) { \
528  HT_ERROR_OUT << "Caught exception: " << e.what() <<", "<< _s_ << HT_END; } \
529  catch (...) { \
530  HT_ERROR_OUT << "Caught unknown exception, " << _s_ << HT_END; }
531 
532 /* Convenience macro to execute code and log all exceptions */
533 #define HT_TRY_OR_LOG(_s_, _code_) do { \
534  try { _code_; } \
535  HT_LOG_EXCEPTION(_s_) \
536 } while (0)
537 
540 } // namespace Hypertable
541 
542 #endif // HYPERTABLE_ERROR_H
ExceptionMessagesRenderer messages(const char *sep=": ") const
Retrieves a Renderer for chained Exceptions.
Definition: Error.h:434
Exception(int error, int l=0, const char *fn=0, const char *fl=0)
Constructor.
Definition: Error.h:340
~Exception()
Destructor.
Definition: Error.h:384
const char * func() const
Returns the name of the function which threw the Exception.
Definition: Error.h:403
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
virtual std::ostream & render_messages(std::ostream &out, const char *sep) const
Renders multiple Exceptions to an ostream.
Definition: Error.cc:396
virtual std::ostream & render_message(std::ostream &out) const
Renders an Exception to an ostream.
Definition: Error.h:415
void generate_html_error_code_documentation(std::ostream &out)
Generates and print the error documentation as html.
Definition: Error.cc:337
std::runtime_error Parent
Definition: Error.h:331
std::ostream & render(std::ostream &out) const
Global helper function to print an Exception to a std::ostream.
Definition: Error.h:447
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
ExceptionMessageRenderer message() const
Retrieves a Renderer for this Exception.
Definition: Error.h:429
int m_error
The error code.
Definition: Error.h:319
std::ostream & operator<<(std::ostream &os, const crontab_entry &entry)
Helper function to write crontab_entry to an ostream.
Definition: Crontab.cc:301
const char * file() const
Returns the source code line number where the exception was thrown.
Definition: Error.h:409
ExceptionMessageRenderer(const Exception &e)
Definition: Error.h:287
Exception(int error, const String &msg, int l=0, const char *fn=0, const char *fl=0)
Constructor.
Definition: Error.h:352
std::ostream & render(std::ostream &out) const
Global helper function to print an Exception to a std::ostream.
Definition: Error.h:453
Helper class to render an exception message a la IO manipulators.
Definition: Error.h:286
Hypertable definitions
Exception * prev
The previous exception in the exception chain.
Definition: Error.h:439
Exception(int error, const String &msg, const Exception &ex, int l=0, const char *fn=0, const char *fl=0)
Constructor.
Definition: Error.h:367
ExceptionMessagesRenderer(const Exception &e, const char *sep=": ")
Definition: Error.h:300
This is a generic exception class for Hypertable.
Definition: Error.h:314
int m_line
The source code line where the exception was thrown.
Definition: Error.h:322
A String class based on std::string.
const char * m_file
The source code file where the exception was thrown.
Definition: Error.h:328
Exception(const Exception &ex)
Copy constructor.
Definition: Error.h:377
Helper class to render an exception message a la IO manipulators.
Definition: Error.h:299
const char * m_func
The function name where the exception was thrown.
Definition: Error.h:325
int line() const
Returns the source code line number where the exception was thrown.
Definition: Error.h:397
int code() const
Returns the error code.
Definition: Error.h:391
const Exception & operator=(const Exception &)
Do not allow assignments.