From 476ba70a9f64f88396f70bdd7636e0b86bbeb18e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 28 Nov 2010 21:36:01 +0900 Subject: [PATCH] Moved errorCode_ from RecoverableException to Exception and added errorCode to stackTrace. Also changed errno to errNum in stackTrace. --- src/AbstractCommand.cc | 16 ++++++++-------- src/Exception.cc | 31 +++++++++++++++++++++++++------ src/Exception.h | 18 +++++++++++++++--- src/RecoverableException.cc | 27 ++++++++++++++------------- src/RecoverableException.h | 12 +++--------- test/ExceptionTest.cc | 10 ++++++---- 6 files changed, 71 insertions(+), 43 deletions(-) diff --git a/src/AbstractCommand.cc b/src/AbstractCommand.cc index 9cc0f8da..7a3b0ce8 100644 --- a/src/AbstractCommand.cc +++ b/src/AbstractCommand.cc @@ -262,9 +262,9 @@ bool AbstractCommand::execute() { req_->getUri().c_str()), DL_ABORT_EX2(fmt("URI=%s", req_->getCurrentUri().c_str()), err)); - fileEntry_->addURIResult(req_->getUri(), err.getCode()); - requestGroup_->setLastErrorCode(err.getCode()); - if(err.getCode() == error_code::CANNOT_RESUME) { + fileEntry_->addURIResult(req_->getUri(), err.getErrorCode()); + requestGroup_->setLastErrorCode(err.getErrorCode()); + if(err.getErrorCode() == error_code::CANNOT_RESUME) { requestGroup_->increaseResumeFailureCount(); } } @@ -290,9 +290,9 @@ bool AbstractCommand::execute() { getCuid(), req_->getUri().c_str()), err); - fileEntry_->addURIResult(req_->getUri(), err.getCode()); - requestGroup_->setLastErrorCode(err.getCode()); - if(err.getCode() == error_code::CANNOT_RESUME) { + fileEntry_->addURIResult(req_->getUri(), err.getErrorCode()); + requestGroup_->setLastErrorCode(err.getErrorCode()); + if(err.getErrorCode() == error_code::CANNOT_RESUME) { requestGroup_->increaseResumeFailureCount(); } onAbort(); @@ -304,8 +304,8 @@ bool AbstractCommand::execute() { } catch(DownloadFailureException& err) { A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, err); if(req_) { - fileEntry_->addURIResult(req_->getUri(), err.getCode()); - requestGroup_->setLastErrorCode(err.getCode()); + fileEntry_->addURIResult(req_->getUri(), err.getErrorCode()); + requestGroup_->setLastErrorCode(err.getErrorCode()); } requestGroup_->setHaltRequested(true); return true; diff --git a/src/Exception.cc b/src/Exception.cc index f34261f2..f1e1f972 100644 --- a/src/Exception.cc +++ b/src/Exception.cc @@ -45,7 +45,8 @@ Exception::Exception : file_(file), line_(line), errNum_(0), - msg_(msg) + msg_(msg), + errorCode_(error_code::UNKNOWN_ERROR) {} Exception::Exception @@ -57,7 +58,20 @@ Exception::Exception line_(line), errNum_(0), msg_(msg), - cause_(cause.copy()) + cause_(cause.copy()), + errorCode_(cause.errorCode_) +{} + +Exception::Exception +(const char* file, + int line, + const std::string& msg, + error_code::Value errorCode) + : file_(file), + line_(line), + errNum_(0), + msg_(msg), + errorCode_(errorCode) {} Exception::Exception @@ -68,7 +82,8 @@ Exception::Exception : file_(file), line_(line), errNum_(errNum), - msg_(msg) + msg_(msg), + errorCode_(error_code::UNKNOWN_ERROR) {} Exception::~Exception() throw() {} @@ -78,13 +93,17 @@ std::string Exception::stackTrace() const std::stringstream s; s << "Exception: " << "[" << file_ << ":" << line_ << "] "; if(errNum_) { - s << "errno=" << errNum_ << " "; + s << "errNum=" << errNum_ << " "; } + s << "errorCode=" << errorCode_ << " "; s << what() << "\n"; SharedHandle e = cause_; while(e) { - s << " -> " << "[" << e->file_ << ":" << e->line_ << "] " - << e->what() << "\n"; + s << " -> " << "[" << e->file_ << ":" << e->line_ << "] "; + if(e->getErrNum()) { + s << "errNum=" << e->getErrNum() << " "; + } + s << "errorCode=" << e->getErrorCode() << " " << e->what() << "\n"; e = e->cause_; } return s.str(); diff --git a/src/Exception.h b/src/Exception.h index b17803ae..22799236 100644 --- a/src/Exception.h +++ b/src/Exception.h @@ -41,6 +41,7 @@ #include #include "SharedHandle.h" +#include "error_code.h" namespace aria2 { @@ -49,21 +50,27 @@ private: const char* file_; int line_; - + // This is low-level system error code, typically errno in Linux. int errNum_; std::string msg_; - + // Exception that this object wraps. Normally this cause_ is the + // root cause of this exception. SharedHandle cause_; + // This is application-level error code. + error_code::Value errorCode_; protected: virtual SharedHandle copy() const = 0; public: Exception(const char* file, int line, const std::string& msg); - + // errorCode_ is initializedwith cause.errorCode_. Exception(const char* file, int line, const std::string& msg, const Exception& cause); + Exception(const char* file, int line, const std::string& msg, + error_code::Value errorCode); + Exception(const char* file, int line, int errNum, const std::string& msg); virtual ~Exception() throw(); @@ -76,6 +83,11 @@ public: { return errNum_; } + + error_code::Value getErrorCode() const + { + return errorCode_; + } }; } // namespace aria2 diff --git a/src/RecoverableException.cc b/src/RecoverableException.cc index d4164bf0..6a380f78 100644 --- a/src/RecoverableException.cc +++ b/src/RecoverableException.cc @@ -43,24 +43,25 @@ SharedHandle RecoverableException::copy() const } RecoverableException::RecoverableException -(const char* file, int line, const std::string& msg): - Exception(file, line, msg), - code_(error_code::UNKNOWN_ERROR) {} +(const char* file, int line, const std::string& msg) + : Exception(file, line, msg) +{} RecoverableException::RecoverableException (const char* file, int line, const std::string& msg, - const Exception& cause): - Exception(file, line, msg, cause), - code_(error_code::UNKNOWN_ERROR) {} - -RecoverableException::RecoverableException -(const char* file, int line, int errNum, const std::string& msg): - Exception(file, line, errNum, msg), - code_(error_code::UNKNOWN_ERROR) {} + const Exception& cause) + : Exception(file, line, msg, cause) +{} RecoverableException::RecoverableException (const char* file, int line, const std::string& msg, - error_code::Value result): - Exception(file, line, msg), code_(result) {} + error_code::Value errorCode) + : Exception(file, line, msg, errorCode) +{} + +RecoverableException::RecoverableException +(const char* file, int line, int errNum, const std::string& msg) + : Exception(file, line, errNum, msg) +{} } // namespace aria2 diff --git a/src/RecoverableException.h b/src/RecoverableException.h index fe0b4b80..9873758e 100644 --- a/src/RecoverableException.h +++ b/src/RecoverableException.h @@ -35,14 +35,10 @@ #ifndef D_RECOVERABLE_EXCEPTION_H #define D_RECOVERABLE_EXCEPTION_H #include "Exception.h" -#include "error_code.h" namespace aria2 { class RecoverableException:public Exception { -private: - error_code::Value code_; - protected: virtual SharedHandle copy() const; public: @@ -51,13 +47,11 @@ public: RecoverableException(const char* file, int line, const std::string& msg, const Exception& cause); + RecoverableException(const char* file, int line, const std::string& msg, + error_code::Value errorCode); + RecoverableException (const char* file, int line, int errNum, const std::string& msg); - - RecoverableException(const char* file, int line, const std::string& msg, - error_code::Value result); - - error_code::Value getCode() const { return code_; } }; } // namespace aria2 diff --git a/test/ExceptionTest.cc b/test/ExceptionTest.cc index 86238c4e..40d8950e 100644 --- a/test/ExceptionTest.cc +++ b/test/ExceptionTest.cc @@ -25,15 +25,17 @@ CPPUNIT_TEST_SUITE_REGISTRATION(ExceptionTest); void ExceptionTest::testStackTrace() { - DownloadFailureException c1 = DOWNLOAD_FAILURE_EXCEPTION("cause1"); + DownloadFailureException c1 = DOWNLOAD_FAILURE_EXCEPTION2 + ("cause1", error_code::TIME_OUT); DownloadFailureException c2 = DOWNLOAD_FAILURE_EXCEPTION2("cause2", c1); DownloadFailureException e = DOWNLOAD_FAILURE_EXCEPTION2("exception thrown", c2); CPPUNIT_ASSERT_EQUAL - (std::string("Exception: [ExceptionTest.cc:31] exception thrown\n" - " -> [ExceptionTest.cc:29] cause2\n" - " -> [ExceptionTest.cc:28] cause1\n"), + (std::string + ("Exception: [ExceptionTest.cc:32] errorCode=2 exception thrown\n" + " -> [ExceptionTest.cc:30] errorCode=2 cause2\n" + " -> [ExceptionTest.cc:29] errorCode=2 cause1\n"), e.stackTrace()); }