Browse Source

Throw exception when unrecognized URI, bad Metalink or bad .torrent
file are given in command-line and exits with status non-zero.

Tatsuhiro Tsujikawa 15 years ago
parent
commit
c03ab007a3
3 changed files with 42 additions and 24 deletions
  1. 34 21
      src/download_helper.cc
  2. 7 2
      src/download_helper.h
  3. 1 1
      src/main.cc

+ 34 - 21
src/download_helper.cc

@@ -366,12 +366,16 @@ private:
   ProtocolDetector detector_;
   SharedHandle<Option> option_;
   bool ignoreLocalPath_;
+  bool throwOnError_;
 public:
   AccRequestGroup(std::vector<SharedHandle<RequestGroup> >& requestGroups,
                   const SharedHandle<Option>& option,
-                  bool ignoreLocalPath = false):
+                  bool ignoreLocalPath = false,
+                  bool throwOnError = false):
     requestGroups_(requestGroups), option_(option),
-    ignoreLocalPath_(ignoreLocalPath) {}
+    ignoreLocalPath_(ignoreLocalPath),
+    throwOnError_(throwOnError)
+  {}
 
   void
   operator()(const std::string& uri)
@@ -390,23 +394,21 @@ public:
     }
 #ifdef ENABLE_BITTORRENT
     else if(detector_.guessTorrentMagnet(uri)) {
-      try {
-        SharedHandle<RequestGroup> group =
-          createBtMagnetRequestGroup(uri, option_);
-        requestGroups_.push_back(group);
-      } catch(RecoverableException& e) {
-        // error occurred while parsing torrent file.
-        // We simply ignore it. 
-        A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
-      }
+      SharedHandle<RequestGroup> group =
+        createBtMagnetRequestGroup(uri, option_);
+      requestGroups_.push_back(group);
     } else if(!ignoreLocalPath_ && detector_.guessTorrentFile(uri)) {
       try {
         requestGroups_.push_back
           (createBtRequestGroup(uri, option_, std::vector<std::string>()));
       } catch(RecoverableException& e) {
-        // error occurred while parsing torrent file.
-        // We simply ignore it. 
-        A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
+        if(throwOnError_) {
+          throw;
+        } else {
+          // error occurred while parsing torrent file.
+          // We simply ignore it.
+          A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
+        }
       }
     } 
 #endif // ENABLE_BITTORRENT
@@ -415,14 +417,22 @@ public:
       try {
         Metalink2RequestGroup().generate(requestGroups_, uri, option_);
       } catch(RecoverableException& e) {
-        // error occurred while parsing metalink file.
-        // We simply ignore it.
-        A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
+        if(throwOnError_) {
+          throw;
+        } else {
+          // error occurred while parsing metalink file.
+          // We simply ignore it.
+          A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
+        }
       }
     }
 #endif // ENABLE_METALINK
     else {
-      A2_LOG_ERROR(fmt(MSG_UNRECOGNIZED_URI, (uri).c_str()));
+      if(throwOnError_) {
+        throw DL_ABORT_EX(fmt(MSG_UNRECOGNIZED_URI, uri.c_str()));
+      } else {
+        A2_LOG_ERROR(fmt(MSG_UNRECOGNIZED_URI, uri.c_str()));
+      }
     }
   }
 };
@@ -444,7 +454,8 @@ void createRequestGroupForUri
  const SharedHandle<Option>& option,
  const std::vector<std::string>& uris,
  bool ignoreForceSequential,
- bool ignoreLocalPath)
+ bool ignoreLocalPath,
+ bool throwOnError)
 {
   std::vector<std::string> nargs;
   if(option->get(PREF_PARAMETERIZED_URI) == A2_V_TRUE) {
@@ -454,7 +465,8 @@ void createRequestGroupForUri
   }
   if(!ignoreForceSequential && option->get(PREF_FORCE_SEQUENTIAL) == A2_V_TRUE) {
     std::for_each(nargs.begin(), nargs.end(),
-                  AccRequestGroup(result, option, ignoreLocalPath));
+                  AccRequestGroup(result, option, ignoreLocalPath,
+                                  throwOnError));
   } else {
     std::vector<std::string>::iterator strmProtoEnd =
       std::stable_partition(nargs.begin(), nargs.end(), StreamProtocolFilter());
@@ -471,7 +483,8 @@ void createRequestGroupForUri
     }
     // process remaining URIs(local metalink, BitTorrent files)
     std::for_each(strmProtoEnd, nargs.end(),
-                  AccRequestGroup(result, option, ignoreLocalPath));
+                  AccRequestGroup(result, option, ignoreLocalPath,
+                                  throwOnError));
   }
 }
 

+ 7 - 2
src/download_helper.h

@@ -87,13 +87,18 @@ void createRequestGroupForUriList
  const SharedHandle<Option>& option);
 
 // Create RequestGroup object using provided uris.  If ignoreLocalPath
-// is true, a path to torrent file abd metalink file are ignored.
+// is true, a path to torrent file abd metalink file are ignored.  If
+// throwOnError is true, exception will be thrown when Metalink
+// Document or .torrent file cannot be parsed or unrecognized URI is
+// given. If throwOnError is false, these errors are just logged as
+// error.
 void createRequestGroupForUri
 (std::vector<SharedHandle<RequestGroup> >& result,
  const SharedHandle<Option>& option,
  const std::vector<std::string>& uris,
  bool ignoreForceSequential = false,
- bool ignoreLocalPath = false);
+ bool ignoreLocalPath = false,
+ bool throwOnError = false);
 
 template<typename InputIterator>
 void setMetadataInfo

+ 1 - 1
src/main.cc

@@ -250,7 +250,7 @@ error_code::Value main(int argc, char* argv[])
         return exitStatus;
 #endif // ENABLE_METALINK || ENABLE_METALINK
       } else {
-        createRequestGroupForUri(requestGroups, op, args);
+        createRequestGroupForUri(requestGroups, op, args, false, false, true);
       }
 
   // Remove option values which is only valid for URIs specified in