dpyError.h
1 /*
2  * dpyError.h
3  */
4 #pragma once
5 
6 #ifndef DPY_ERROR
7 #define DPY_ERROR
8 
9 #include <boost/system/error_code.hpp>
10 #include <boost/version.hpp>
11 
12 
13 #ifdef TESTING
14 #include "gtest/gtest.h"
15 #endif
16 
17 #define XSTR(x) STR(x)
18 #define STR(x) #x
19 
20 #if BOOST_VERSION < 107000
21 #define BOOST_IO_CONTEXT boost::asio::io_context
22 #define BOOST_GET_CONTEXT(x) x->get_executor().context()
23 #define BOOST_GET_EXECUTOR(x) boost::ref(x)
24 #else
25 #define BOOST_IO_CONTEXT const boost::asio::any_io_executor
26 #define BOOST_GET_CONTEXT(x) x->get_executor()
27 #define BOOST_GET_EXECUTOR(x) x.get_executor()
28 #endif
29 
30 
34 enum dpy_errors
35 {
37  DPY_OK = 0,
38  DPY_NOT_FOUND = 1,
39  DPY_FORMAT_ERROR = 2,
40  DPY_OVERFLOW = 3,
41  DPY_UNAVAILABLE = 4,
42  DPY_TERMINATED = 5,
43  DPY_IO_ERROR = 6,
44  DPY_COMM_ERROR = 7,
45  DPY_TIMEOUT = 8,
46  DPY_OUT_OF_RANGE = 9,
47  DPY_NO_MEMORY = 10,
48  DPY_NOT_PERMITTED = 11,
49  DPY_DUPLICATE = 12,
50  DPY_BAD_PARAMETER = 13,
51  DPY_CLOSED = 14,
52  DPY_BUSY = 15,
53  DPY_UNSUPPORTED = 16,
54  DPY_NOT_IMPLEMENTED = 17,
55  DPY_NO_EFFECT = 18,
56  DPY_COULD_NOT_OBTAIN_VALUE = 19,
57  DPY_EMPTY_MESSAGE = 20,
58  DPY_PARSE_ERROR = 21,
59  DPY_COMMAND_ERROR = 22,
60  DPY_INVALID_VALUE = 23,
61  DPY_INCOMPLETE = 24,
62  DPY_CONFIG_ERROR = 25,
63  DPY_NOT_READY = 26,
64  DPY_INVALID_CHECKSUM = 27,
65  DPY_REQUEST_PROCESSED = 28,
66  DPY_REQUEST_DEFERRED = 29,
67 
69  DPY_FILE_COULD_NOT_BE_OPENED = 30,
70  DPY_FILE_DOES_NOT_EXIST = 31,
71  DPY_FILE_CORRUPTED = 32,
72  DPY_FILE_JSON_PARSE_ERROR = 33,
73 
75  DPY_PORT_OPEN_ERROR = 40,
76  DPY_PORT_CLOSE_ERROR = 41,
77  DPY_PORT_OPEN_LOCK_ERROR = 42,
78 
79 
81  DPY_EXCEPTION = 45,
82 
83 };
84 
85 #define ERROR_PARSE {ec=DPY_FILE_JSON_PARSE_ERROR;return false;}
86 
95 class dpyError: public boost::system::error_category
96 {
97 public:
98 
99 virtual ~dpyError() = default;
104  virtual const char *name() const noexcept
105  {
106  return "DPY_ERRORS";
107  }
113  virtual std::string message(int e) const
114  {
115  switch (e) {
116  case DPY_OK:
117  return "Successful operation.";
118  case DPY_NOT_FOUND:
119  return "Referenced item does not exist or could not be found.";
120  case DPY_FORMAT_ERROR:
121  return "Format obtained does not meet the expected requirements";
122  case DPY_OVERFLOW:
123  return "An overflow occurred or would have occurred.";
124  case DPY_UNAVAILABLE:
125  return "A transient or temporary loss of a service or resource.";
126  case DPY_TERMINATED:
127  return "The process, operation, data stream, session, etc. has stopped.";
128  case DPY_IO_ERROR:
129  return "An IO operation failed.";
130  case DPY_COMM_ERROR:
131  return "Communications error.";
132  case DPY_TIMEOUT:
133  return "A time-out occurred.";
134  case DPY_OUT_OF_RANGE:
135  return "An index or other value is out of range.";
136  case DPY_NO_MEMORY:
137  return "Insufficient memory is available.";
138  case DPY_NOT_PERMITTED:
139  return "Requested action not permitted.";
140  case DPY_DUPLICATE:
141  return "Duplicate entry found or operation already performed.";
142  case DPY_BAD_PARAMETER:
143  return "Invalid parameter (out of range or invalid type).";
144  case DPY_CLOSED:
145  return "The resource is closed.";
146  case DPY_BUSY:
147  return "The resource is busy.";
148  case DPY_UNSUPPORTED:
149  return "The underlying resource does not support this operation.";
150  case DPY_NOT_IMPLEMENTED:
151  return "Unimplemented functionality.";
152  case DPY_NO_EFFECT:
153  return "Call has no effect.";
154  case DPY_COULD_NOT_OBTAIN_VALUE:
155  return "The value could not be obtained";
156  case DPY_EMPTY_MESSAGE:
157  return "The message is empty";
158  case DPY_PARSE_ERROR:
159  return "There was an error during parsing";
160  case DPY_COMMAND_ERROR:
161  return "Expected command response could not be received";
162  case DPY_INVALID_VALUE:
163  return "The value requested is not valid";
164  case DPY_INCOMPLETE:
165  return "The operation requested is missing one or more parameters";
166  case DPY_CONFIG_ERROR:
167  return "Configuration error occurred";
168  case DPY_NOT_READY:
169  return "The requested service is not ready yet";
170  case DPY_INVALID_CHECKSUM:
171  return "Checksum calculation has thrown an error";
172  case DPY_REQUEST_PROCESSED:
173  return "The request was processed but the result of the operation can't be known";
174  case DPY_REQUEST_DEFERRED:
175  return "The request was received and will be processed as soon as possible ";
176  case DPY_FILE_COULD_NOT_BE_OPENED:
177  return "File could not be opened.";
178  case DPY_FILE_DOES_NOT_EXIST:
179  return "File does not exist";
180  case DPY_FILE_CORRUPTED:
181  return "File is corrupted";
182  case DPY_FILE_JSON_PARSE_ERROR:
183  return "The json file could not be parsed";
184  case DPY_PORT_OPEN_ERROR:
185  return "Could not open the port";
186  case DPY_PORT_CLOSE_ERROR:
187  return "Could not close the port";
188  case DPY_PORT_OPEN_LOCK_ERROR:
189  return "Could not open the port, it is locked by another application";
190  case DPY_EXCEPTION:
191  return "Exception occurred while performing operation";
192  default:
193  return "unknown custom::category error";
194  }
195  return "unknown custom::category error";
196  }
197 };
198 
199 inline const boost::system::error_category& get_dpy_error_category();
200 static const boost::system::error_category& dpy_error_category = get_dpy_error_category();
201 
202 const boost::system::error_category& get_dpy_error_category()
203 {
204  static dpyError dpy_cat;
205  return dpy_cat;
206 }
207 
208 
209 inline boost::system::error_code make_error_code(dpy_errors e)
210 {
211  return boost::system::error_code(static_cast<int>(e), dpy_error_category);
212 }
213 
214 namespace boost
215 {
216 namespace system
217 {
221 template<>
222 struct is_error_code_enum<dpy_errors>
223 {
227  BOOST_STATIC_CONSTANT(bool, value = true);
228 };
229 }
230 } // namespaces
231 
232 #endif
Definition: dpyError.h:214
virtual const char * name() const noexcept
Returns the name of the category.
Definition: dpyError.h:104
Allows to categorize the errors.
Definition: dpyError.h:95
virtual std::string message(int e) const
Return the error message according to the error value.
Definition: dpyError.h:113