ctkPluginException.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkPluginException.h"
  16. ctkPluginException::ctkPluginException(const QString& msg, const Type& type, const std::exception* cause)
  17. : std::runtime_error(msg.toStdString()),
  18. type(type)
  19. {
  20. if (cause)
  21. {
  22. this->cause = QString(cause->what());
  23. }
  24. }
  25. ctkPluginException::ctkPluginException(const QString& msg, const std::exception* cause)
  26. : std::runtime_error(msg.toStdString()),
  27. type(UNSPECIFIED)
  28. {
  29. if (cause)
  30. {
  31. this->cause = QString(cause->what());
  32. }
  33. }
  34. ctkPluginException::ctkPluginException(const ctkPluginException& o)
  35. : std::runtime_error(o.what()), type(o.type), cause(o.cause)
  36. {
  37. }
  38. ctkPluginException& ctkPluginException::operator=(const ctkPluginException& o)
  39. {
  40. std::runtime_error::operator=(o);
  41. type = o.type;
  42. cause = o.cause;
  43. return *this;
  44. }
  45. QString ctkPluginException::getCause() const
  46. {
  47. return cause;
  48. }
  49. void ctkPluginException::setCause(const QString& cause) throw(std::logic_error)
  50. {
  51. if (!this->cause.isEmpty()) throw std::logic_error("The cause for this ctkPluginException instance is already set");
  52. this->cause = cause;
  53. }
  54. ctkPluginException::Type ctkPluginException::getType() const
  55. {
  56. return type;
  57. }
  58. const char* ctkPluginException::what() const throw()
  59. {
  60. static std::string fullMsg;
  61. fullMsg = std::string(std::runtime_error::what());
  62. QString causeMsg = getCause();
  63. if (!causeMsg.isEmpty()) fullMsg += std::string("\n Caused by: ") + causeMsg.toStdString();
  64. return fullMsg.c_str();
  65. }
  66. QDebug operator<<(QDebug dbg, const ctkPluginException& exc)
  67. {
  68. dbg << "ctkPluginException:" << exc.what();
  69. return dbg.maybeSpace();
  70. }