Преглед на файлове

Moved errorCode_ from RecoverableException to Exception and added errorCode to
stackTrace.

Also changed errno to errNum in stackTrace.

Tatsuhiro Tsujikawa преди 15 години
родител
ревизия
476ba70a9f
променени са 6 файла, в които са добавени 70 реда и са изтрити 42 реда
  1. 8 8
      src/AbstractCommand.cc
  2. 25 6
      src/Exception.cc
  3. 15 3
      src/Exception.h
  4. 13 12
      src/RecoverableException.cc
  5. 3 9
      src/RecoverableException.h
  6. 6 4
      test/ExceptionTest.cc

+ 8 - 8
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;

+ 25 - 6
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<Exception> 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();

+ 15 - 3
src/Exception.h

@@ -41,6 +41,7 @@
 #include <string>
 
 #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<Exception> cause_;
+  // This is application-level error code.
+  error_code::Value errorCode_;
 protected:
   virtual SharedHandle<Exception> 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

+ 13 - 12
src/RecoverableException.cc

@@ -43,24 +43,25 @@ SharedHandle<Exception> 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) {}
+ const Exception& cause)
+  : Exception(file, line, msg, cause)
+{}
 
 RecoverableException::RecoverableException
-(const char* file, int line, int errNum, const std::string& msg):
-  Exception(file, line, errNum, msg),
-  code_(error_code::UNKNOWN_ERROR) {}
+(const char* file, int line, const std::string& msg,
+ error_code::Value errorCode)
+  : Exception(file, line, msg, errorCode)
+{}
 
 RecoverableException::RecoverableException
-(const char* file, int line, const std::string& msg,
- error_code::Value result):
-  Exception(file, line, msg), code_(result) {}
+(const char* file, int line, int errNum, const std::string& msg)
+  : Exception(file, line, errNum, msg)
+{}
 
 } // namespace aria2

+ 3 - 9
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<Exception> 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, int errNum, const std::string& msg);
-  
   RecoverableException(const char* file, int line, const std::string& msg,
-                       error_code::Value result);
+                       error_code::Value errorCode);
 
-  error_code::Value getCode() const { return code_; }
+  RecoverableException
+  (const char* file, int line, int errNum, const std::string& msg);
 };
 
 } // namespace aria2

+ 6 - 4
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());
 }