Exception.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // Copyright (c) 2013-2020 Josh Blum
  3. // SPDX-License-Identifier: BSL-1.0
  4. //
  5. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  6. // and Contributors.
  7. //
  8. // Permission is hereby granted, free of charge, to any person or organization
  9. // obtaining a copy of the software and accompanying documentation covered by
  10. // this license (the "Software") to use, reproduce, display, distribute,
  11. // execute, and transmit the Software, and to prepare derivative works of the
  12. // Software, and to permit third-parties to whom the Software is furnished to
  13. // do so, all subject to the following:
  14. //
  15. // The copyright notices in the Software and this entire statement, including
  16. // the above license grant, this restriction and the following disclaimer,
  17. // must be included in all copies of the Software, in whole or in part, and
  18. // all derivative works of the Software, unless such copies or derivative
  19. // works are solely in the form of machine-executable object code generated by
  20. // a source language processor.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  25. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  26. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  27. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. // DEALINGS IN THE SOFTWARE.
  29. //
  30. #include <Pothos/Exception.hpp>
  31. #include <Poco/String.h>
  32. namespace Pothos {
  33. Exception::Exception(int code): _pNested(0), _code(code)
  34. {
  35. }
  36. Exception::Exception(const std::string& msg, int code): _msg(msg), _pNested(0), _code(code)
  37. {
  38. }
  39. Exception::Exception(const std::string& msg, const std::string& arg, int code): _msg(msg), _pNested(0), _code(code)
  40. {
  41. if (!arg.empty())
  42. {
  43. _msg.append(": ");
  44. _msg.append(arg);
  45. }
  46. }
  47. Exception::Exception(const std::string& msg, const Exception& nested, int code): _msg(msg), _pNested(nested.clone()), _code(code)
  48. {
  49. _msg.append("\n -> {");
  50. _msg.append(Poco::trim(Poco::replace(_pNested->_msg, "\n", "\n ")));
  51. _msg.append("}");
  52. }
  53. Exception::Exception(const Exception& exc):
  54. std::exception(exc),
  55. _msg(exc._msg),
  56. _code(exc._code)
  57. {
  58. _pNested = exc._pNested ? exc._pNested->clone() : 0;
  59. }
  60. Exception::~Exception() throw()
  61. {
  62. delete _pNested;
  63. }
  64. Exception& Exception::operator = (const Exception& exc)
  65. {
  66. if (&exc != this)
  67. {
  68. delete _pNested;
  69. _msg = exc._msg;
  70. _pNested = exc._pNested ? exc._pNested->clone() : 0;
  71. _code = exc._code;
  72. }
  73. return *this;
  74. }
  75. const char* Exception::name() const throw()
  76. {
  77. return "Exception";
  78. }
  79. const char* Exception::className() const throw()
  80. {
  81. const static auto name = Pothos::Util::typeInfoToString(typeid(*this));
  82. return name.c_str();
  83. }
  84. const char* Exception::what() const throw()
  85. {
  86. return name();
  87. }
  88. std::string Exception::displayText() const
  89. {
  90. std::string txt = name();
  91. if (!_msg.empty())
  92. {
  93. txt.append(": ");
  94. txt.append(_msg);
  95. }
  96. return txt;
  97. }
  98. void Exception::extendedMessage(const std::string& arg)
  99. {
  100. if (!arg.empty())
  101. {
  102. if (!_msg.empty()) _msg.append(": ");
  103. _msg.append(arg);
  104. }
  105. }
  106. Exception* Exception::clone() const
  107. {
  108. return new Exception(*this);
  109. }
  110. void Exception::rethrow() const
  111. {
  112. throw *this;
  113. }
  114. POTHOS_IMPLEMENT_EXCEPTION(LogicException, Exception, "Logic exception")
  115. POTHOS_IMPLEMENT_EXCEPTION(AssertionViolationException, LogicException, "Assertion violation")
  116. POTHOS_IMPLEMENT_EXCEPTION(NullPointerException, LogicException, "Null pointer")
  117. POTHOS_IMPLEMENT_EXCEPTION(NullValueException, LogicException, "Null value")
  118. POTHOS_IMPLEMENT_EXCEPTION(BugcheckException, LogicException, "Bugcheck")
  119. POTHOS_IMPLEMENT_EXCEPTION(InvalidArgumentException, LogicException, "Invalid argument")
  120. POTHOS_IMPLEMENT_EXCEPTION(NotImplementedException, LogicException, "Not implemented")
  121. POTHOS_IMPLEMENT_EXCEPTION(RangeException, LogicException, "Out of range")
  122. POTHOS_IMPLEMENT_EXCEPTION(IllegalStateException, LogicException, "Illegal state")
  123. POTHOS_IMPLEMENT_EXCEPTION(InvalidAccessException, LogicException, "Invalid access")
  124. POTHOS_IMPLEMENT_EXCEPTION(SignalException, LogicException, "Signal received")
  125. POTHOS_IMPLEMENT_EXCEPTION(UnhandledException, LogicException, "Unhandled exception")
  126. POTHOS_IMPLEMENT_EXCEPTION(RuntimeException, Exception, "Runtime exception")
  127. POTHOS_IMPLEMENT_EXCEPTION(NotFoundException, RuntimeException, "Not found")
  128. POTHOS_IMPLEMENT_EXCEPTION(ExistsException, RuntimeException, "Exists")
  129. POTHOS_IMPLEMENT_EXCEPTION(TimeoutException, RuntimeException, "Timeout")
  130. POTHOS_IMPLEMENT_EXCEPTION(SystemException, RuntimeException, "System exception")
  131. POTHOS_IMPLEMENT_EXCEPTION(RegularExpressionException, RuntimeException, "Error in regular expression")
  132. POTHOS_IMPLEMENT_EXCEPTION(LibraryLoadException, RuntimeException, "Cannot load library")
  133. POTHOS_IMPLEMENT_EXCEPTION(LibraryAlreadyLoadedException, RuntimeException, "Library already loaded")
  134. POTHOS_IMPLEMENT_EXCEPTION(NoThreadAvailableException, RuntimeException, "No thread available")
  135. POTHOS_IMPLEMENT_EXCEPTION(PropertyNotSupportedException, RuntimeException, "Property not supported")
  136. POTHOS_IMPLEMENT_EXCEPTION(PoolOverflowException, RuntimeException, "Pool overflow")
  137. POTHOS_IMPLEMENT_EXCEPTION(NoPermissionException, RuntimeException, "No permission")
  138. POTHOS_IMPLEMENT_EXCEPTION(OutOfMemoryException, RuntimeException, "Out of memory")
  139. POTHOS_IMPLEMENT_EXCEPTION(DataException, RuntimeException, "Data error")
  140. POTHOS_IMPLEMENT_EXCEPTION(DataFormatException, DataException, "Bad data format")
  141. POTHOS_IMPLEMENT_EXCEPTION(SyntaxException, DataException, "Syntax error")
  142. POTHOS_IMPLEMENT_EXCEPTION(CircularReferenceException, DataException, "Circular reference")
  143. POTHOS_IMPLEMENT_EXCEPTION(PathSyntaxException, SyntaxException, "Bad path syntax")
  144. POTHOS_IMPLEMENT_EXCEPTION(IOException, RuntimeException, "I/O error")
  145. POTHOS_IMPLEMENT_EXCEPTION(ProtocolException, IOException, "Protocol error")
  146. POTHOS_IMPLEMENT_EXCEPTION(FileException, IOException, "File access error")
  147. POTHOS_IMPLEMENT_EXCEPTION(FileExistsException, FileException, "File exists")
  148. POTHOS_IMPLEMENT_EXCEPTION(FileNotFoundException, FileException, "File not found")
  149. POTHOS_IMPLEMENT_EXCEPTION(PathNotFoundException, FileException, "Path not found")
  150. POTHOS_IMPLEMENT_EXCEPTION(FileReadOnlyException, FileException, "File is read-only")
  151. POTHOS_IMPLEMENT_EXCEPTION(FileAccessDeniedException, FileException, "Access to file denied")
  152. POTHOS_IMPLEMENT_EXCEPTION(CreateFileException, FileException, "Cannot create file")
  153. POTHOS_IMPLEMENT_EXCEPTION(OpenFileException, FileException, "Cannot open file")
  154. POTHOS_IMPLEMENT_EXCEPTION(WriteFileException, FileException, "Cannot write file")
  155. POTHOS_IMPLEMENT_EXCEPTION(ReadFileException, FileException, "Cannot read file")
  156. POTHOS_IMPLEMENT_EXCEPTION(UnknownURISchemeException, RuntimeException, "Unknown URI scheme")
  157. POTHOS_IMPLEMENT_EXCEPTION(ApplicationException, Exception, "Application exception")
  158. POTHOS_IMPLEMENT_EXCEPTION(BadCastException, RuntimeException, "Bad cast exception")
  159. } // namespace Pothos