ctkServiceException.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "ctkServiceException.h"
  16. #include <QDebug>
  17. ctkServiceException::ctkServiceException(const QString& msg, const Type& type, const std::exception* cause)
  18. : std::runtime_error(msg.toStdString()),
  19. type(type)
  20. {
  21. if (cause)
  22. {
  23. this->cause = QString(cause->what());
  24. }
  25. }
  26. ctkServiceException::ctkServiceException(const QString& msg, const std::exception* cause)
  27. : std::runtime_error(msg.toStdString()),
  28. type(UNSPECIFIED)
  29. {
  30. if (cause)
  31. {
  32. this->cause = QString(cause->what());
  33. }
  34. }
  35. ctkServiceException::ctkServiceException(const ctkServiceException& o)
  36. : std::runtime_error(o.what()), type(o.type), cause(o.cause)
  37. {
  38. }
  39. ctkServiceException& ctkServiceException::operator=(const ctkServiceException& o)
  40. {
  41. std::runtime_error::operator=(o);
  42. type = o.type;
  43. cause = o.cause;
  44. return *this;
  45. }
  46. QString ctkServiceException::getCause() const
  47. {
  48. return cause;
  49. }
  50. void ctkServiceException::setCause(const QString& cause) throw(std::logic_error)
  51. {
  52. if (!this->cause.isEmpty()) throw std::logic_error("The cause for this ctkServiceException instance is already set");
  53. this->cause = cause;
  54. }
  55. ctkServiceException::Type ctkServiceException::getType() const
  56. {
  57. return type;
  58. }
  59. const char* ctkServiceException::what() const throw()
  60. {
  61. static std::string fullMsg;
  62. fullMsg = std::string(std::runtime_error::what());
  63. QString causeMsg = getCause();
  64. if (!causeMsg.isEmpty()) fullMsg += std::string("\n Caused by: ") + causeMsg.toStdString();
  65. return fullMsg.c_str();
  66. }
  67. QDebug operator<<(QDebug dbg, const ctkServiceException& exc)
  68. {
  69. dbg << "ctkServiceException:" << exc.what();
  70. return dbg.maybeSpace();
  71. }