RMS SDK for C++  0.2.1
A client library for using Microsoft RMS from Linux.
RMSExceptions.h
1 /*
2  * ======================================================================
3  * Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
4  * Licensed under the MIT License.
5  * See LICENSE.md in the project root for license information.
6  * ======================================================================
7  */
8 
9 #ifndef _RMS_LIB_EXCEPTIONS_H
10 #define _RMS_LIB_EXCEPTIONS_H
11 #include <string>
12 #include <cstring>
13 #include <exception>
14 #include <algorithm>
15 
16 #ifndef _NOEXCEPT
17 # if __GNUC__ >= 4
18 # define _NOEXCEPT _GLIBCXX_USE_NOEXCEPT
19 # endif // if __GNUC__ >= 4
20 #endif // ifndef _NOEXCEPT
21 
22 namespace rmscore {
23 namespace exceptions {
24 #define MAX_EXCEPTION_MESSAGE_LENGTH 255
25 
29 class RMSException : public std::exception {
30 public:
31 
36  LogicError = 0
37  };
38 
42  enum ErrorTypes {
43  InvalidArgument = 0,
44  NullPointer,
45  NotFound,
46  NetworkError,
47  CryptoError,
48  StreamError,
49  PFileError,
50  RightsError
51  };
52 
56  RMSException(const ExceptionTypes type,
57  const int error,
58  const std::string & message) _NOEXCEPT
59  : type_(type), error_(error)
60  {
61  CopyMessage(message.c_str(), message.length());
62  }
63 
67  RMSException(const ExceptionTypes type,
68  const int error,
69  const char *const & message) _NOEXCEPT
70  : type_(type), error_(error)
71  {
72  CopyMessage(message, strlen(message));
73  }
74 
78  virtual ~RMSException() _NOEXCEPT {}
79 
83  virtual const char* what() const _NOEXCEPT override {
84  return message_;
85  }
86 
90  virtual ExceptionTypes type() const _NOEXCEPT {
91  return type_;
92  }
93 
97  virtual int error() const _NOEXCEPT {
98  return error_;
99  }
100 
101 private:
102 
103  void CopyMessage(const char *msg, const size_t len) {
104  size_t mlen =
105  (std::min)(len, static_cast<const size_t>(MAX_EXCEPTION_MESSAGE_LENGTH - 1));
106 
107  memset(message_, 0, sizeof(message_));
108 
109  if (mlen > 0) {
110  #ifdef Q_OS_WIN32
111  memcpy_s(message_, mlen, msg, mlen);
112  #else // ifdef Q_OS_WIN32
113  memcpy(message_, msg, mlen);
114  #endif // ifdef Q_OS_WIN32
115  }
116  }
117 
118  ExceptionTypes type_;
119  int error_;
120  char message_[MAX_EXCEPTION_MESSAGE_LENGTH];
121 };
122 
127 public:
128 
132  RMSLogicException(const int error,
133  const std::string& message) _NOEXCEPT
134  : RMSException(LogicError, error, message) {}
135 
139  RMSLogicException(const int error,
140  const char *const& message) _NOEXCEPT
141  : RMSException(LogicError, error, message) {}
142 
146  virtual ~RMSLogicException() _NOEXCEPT {}
147 };
148 
153 public:
154 
158  RMSInvalidArgumentException(const std::string& message) _NOEXCEPT
159  : RMSLogicException(InvalidArgument, message) {}
160 
164  RMSInvalidArgumentException(const char *const& message) _NOEXCEPT
165  : RMSLogicException(InvalidArgument, message) {}
166 
170  virtual ~RMSInvalidArgumentException() _NOEXCEPT {}
171 };
172 
177 public:
178 
182  RMSNullPointerException(const std::string& message) _NOEXCEPT
183  : RMSLogicException(NullPointer, message) {}
184 
188  RMSNullPointerException(const char *const& message) _NOEXCEPT
189  : RMSLogicException(NullPointer, message) {}
190 
194  virtual ~RMSNullPointerException() _NOEXCEPT {}
195 };
196 
201 public:
202 
206  RMSNotFoundException(const std::string& message) _NOEXCEPT
207  : RMSLogicException(NullPointer, message) {}
208 
212  RMSNotFoundException(const char *const& message) _NOEXCEPT
213  : RMSLogicException(NullPointer, message) {}
214 
218  virtual ~RMSNotFoundException() _NOEXCEPT {}
219 };
220 
225 public:
226 
230  enum Reason {
231  ServerError = 0,
232  UserNotConsented,
233  ServiceNotAvailable,
234  OnPremNotSupported,
235  InvalidPL,
236  ServiceDisabled,
237  DeviceRejected,
238  NeedsOnline
239  };
240 
244  RMSNetworkException(const std::string& message, Reason reason) _NOEXCEPT
245  : RMSLogicException(NetworkError, message), reason_(reason) {}
246 
250  RMSNetworkException(const char *const& message, Reason reason) _NOEXCEPT
251  : RMSLogicException(NetworkError, message), reason_(reason) {}
252 
256  virtual ~RMSNetworkException() _NOEXCEPT {}
257 
261  virtual Reason reason() const _NOEXCEPT {
262  return reason_;
263  }
264 
265 private:
266 
267  Reason reason_; // additional reason for this error
268 };
269 
274 public:
275 
279  RMSCryptographyException(const std::string& message) _NOEXCEPT
280  : RMSLogicException(NetworkError, message) {}
281 
285  RMSCryptographyException(const char *const& message) _NOEXCEPT
286  : RMSLogicException(NetworkError, message) {}
287 
291  virtual ~RMSCryptographyException() _NOEXCEPT {}
292 };
293 
298 public:
299 
303  RMSStreamException(const std::string& message) _NOEXCEPT
304  : RMSLogicException(StreamError, message) {}
305 
309  RMSStreamException(const char *const& message) _NOEXCEPT
310  : RMSLogicException(StreamError, message) {}
311 
315  virtual ~RMSStreamException() _NOEXCEPT {}
316 };
317 
322 public:
323 
327  enum Reason {
328  NotPFile = 0,
329  NotSupportedVersion,
330  BadArguments,
331  };
332 
336  RMSPFileException(const std::string& message, Reason reason) _NOEXCEPT
337  : RMSLogicException(PFileError, message), reason_(reason) {}
338 
342  RMSPFileException(const char *const& message, Reason reason) _NOEXCEPT
343  : RMSLogicException(PFileError, message), reason_(reason) {}
344 
348  virtual ~RMSPFileException() _NOEXCEPT {}
349 
353  virtual Reason reason() const _NOEXCEPT {
354  return reason_;
355  }
356 
357 private:
358 
359  Reason reason_; // additional reason for this error
360 };
361 
366 public:
367 
371  RMSRightsException(const std::string& message) _NOEXCEPT
372  : RMSLogicException(PFileError, message) {}
373 
377  RMSRightsException(const char *const& message) _NOEXCEPT
378  : RMSLogicException(PFileError, message) {}
379 
383  virtual ~RMSRightsException() _NOEXCEPT {}
384 };
385 } // namespace exceptions
386 } // namespace rmscore
387 #endif // _RMS_LIB_EXCEPTIONS_H
Network error.
Definition: RMSExceptions.h:224
Logic error.
Definition: RMSExceptions.h:126
Stream error.
Definition: RMSExceptions.h:297
RMS Error.
Definition: RMSExceptions.h:29
Rights error.
Definition: RMSExceptions.h:365
Definition: AuthenticationCallbackImpl.h:16
Reason
Possible seasons for network error.
Definition: RMSExceptions.h:230
ErrorTypes
Type of error.
Definition: RMSExceptions.h:42
File error.
Definition: RMSExceptions.h:321
Invalid argument.
Definition: RMSExceptions.h:152
Reason
Reasons for file error.
Definition: RMSExceptions.h:327
Null pointer.
Definition: RMSExceptions.h:176
Cryptographic error.
Definition: RMSExceptions.h:273
Not found.
Definition: RMSExceptions.h:200
ExceptionTypes
Type of exception.
Definition: RMSExceptions.h:35