Exception.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "Exception.h"
  36. #include <sstream>
  37. namespace aria2 {
  38. Exception::Exception
  39. (const char* file,
  40. int line,
  41. const std::string& msg)
  42. : file_(file),
  43. line_(line),
  44. errNum_(0),
  45. msg_(msg),
  46. errorCode_(error_code::UNKNOWN_ERROR)
  47. {}
  48. Exception::Exception
  49. (const char* file,
  50. int line,
  51. const std::string& msg,
  52. error_code::Value errorCode,
  53. const Exception& cause)
  54. : file_(file),
  55. line_(line),
  56. errNum_(0),
  57. msg_(msg),
  58. errorCode_(errorCode),
  59. cause_(cause.copy())
  60. {}
  61. Exception::Exception
  62. (const char* file,
  63. int line,
  64. const std::string& msg,
  65. const Exception& cause)
  66. : file_(file),
  67. line_(line),
  68. errNum_(0),
  69. msg_(msg),
  70. errorCode_(cause.errorCode_),
  71. cause_(cause.copy())
  72. {}
  73. Exception::Exception
  74. (const char* file,
  75. int line,
  76. const std::string& msg,
  77. error_code::Value errorCode)
  78. : file_(file),
  79. line_(line),
  80. errNum_(0),
  81. msg_(msg),
  82. errorCode_(errorCode)
  83. {}
  84. Exception::Exception
  85. (const char* file,
  86. int line,
  87. int errNum,
  88. const std::string& msg)
  89. : file_(file),
  90. line_(line),
  91. errNum_(errNum),
  92. msg_(msg),
  93. errorCode_(error_code::UNKNOWN_ERROR)
  94. {}
  95. Exception::Exception
  96. (const char* file,
  97. int line,
  98. int errNum,
  99. const std::string& msg,
  100. error_code::Value errorCode)
  101. : file_(file),
  102. line_(line),
  103. errNum_(errNum),
  104. msg_(msg),
  105. errorCode_(errorCode)
  106. {}
  107. Exception::~Exception() throw() {}
  108. std::string Exception::stackTrace() const
  109. {
  110. std::stringstream s;
  111. s << "Exception: " << "[" << file_ << ":" << line_ << "] ";
  112. if(errNum_) {
  113. s << "errNum=" << errNum_ << " ";
  114. }
  115. s << "errorCode=" << errorCode_ << " ";
  116. s << what() << "\n";
  117. SharedHandle<Exception> e = cause_;
  118. while(e) {
  119. s << " -> " << "[" << e->file_ << ":" << e->line_ << "] ";
  120. if(e->getErrNum()) {
  121. s << "errNum=" << e->getErrNum() << " ";
  122. }
  123. s << "errorCode=" << e->getErrorCode() << " " << e->what() << "\n";
  124. e = e->cause_;
  125. }
  126. return s.str();
  127. }
  128. const char* Exception::what() const throw()
  129. {
  130. return msg_.c_str();
  131. }
  132. } // namespace aria2