ZIO
Python and C++ interface to ZeroMQ and Zyre
exceptions.hpp
Go to the documentation of this file.
1 #ifndef ZIO_EXCEPTIONS_HPP_SEEN
2 #define ZIO_EXCEPTIONS_HPP_SEEN
3 
4 #include <stdexcept>
5 #include <cstring>
6 #include <string>
7 
8 namespace zio {
9 
10  // This is heavily cribbed from nlohmann::json!
11 
16  class exception : public std::exception
17  {
18  public:
20  const char* what() const noexcept override {
21  return m.what();
22  }
24  const int id;
25 
26  protected:
27  exception(int id_, const char* what_arg) : id(id_), m(what_arg) {}
28  static std::string name(const std::string& ename, int id_) {
29  return "[zio.exception." + ename + "." + std::to_string(id_) + "] ";
30  }
31 
32  private:
33  std::runtime_error m;
34  };
35 
42  class socket_error : public exception {
43  public:
44 
46  static socket_error create(int id_, const char* errmsg, const char* extra=NULL) {
47  std::string w = exception::name("socket_error", id_) + errmsg;
48  if (extra) {
49  w += ": ";
50  w += extra;
51  }
52  return socket_error(id_, w.c_str());
53  }
54 
57  static socket_error create(const char* extra=NULL) {
58  return create(errno, strerror(errno), extra);
59  }
60 
61  private:
62  socket_error(int id_, const char* what_arg)
63  : exception(id_, what_arg) { }
64  };
65 
76  class message_error : public exception {
77  public:
78  static message_error create(int id_, const char* errmsg,
79  const char* extra=NULL) {
80  std::string w = exception::name("message_error", id_) + errmsg;
81  if (extra) {
82  w += ": ";
83  w += extra;
84  }
85  return message_error(id_, w.c_str());
86  }
87 
88  private:
89  message_error(int id_, const char* what_arg)
90  : exception(id_, what_arg) { }
91 
92  };
93 }
94 
95 #endif
exception(int id_, const char *what_arg)
Definition: exceptions.hpp:27
static message_error create(int id_, const char *errmsg, const char *extra=NULL)
Definition: exceptions.hpp:78
exception indicating a socket error
Definition: exceptions.hpp:42
exception indicating message error
Definition: exceptions.hpp:76
const int id
the id of the exception
Definition: exceptions.hpp:24
static socket_error create(const char *extra=NULL)
Definition: exceptions.hpp:57
const char * what() const noexcept override
returns the explanatory string
Definition: exceptions.hpp:20
general exception class for zio
Definition: exceptions.hpp:16
static socket_error create(int id_, const char *errmsg, const char *extra=NULL)
Create a socket error with all data and optional extra message.
Definition: exceptions.hpp:46
static std::string name(const std::string &ename, int id_)
Definition: exceptions.hpp:28
implementation of ZIO data flow protocol endpoints
Definition: actor.hpp:14