enhanced exception handling, getting rid of old error codes
authorJiri Srain <jsrain@suse.cz>
Wed, 14 Dec 2005 12:19:51 +0000 (12:19 +0000)
committerJiri Srain <jsrain@suse.cz>
Wed, 14 Dec 2005 12:19:51 +0000 (12:19 +0000)
zypp/media/MediaAccess.cc
zypp/media/MediaCD.cc
zypp/media/MediaException.cc
zypp/media/MediaException.h
zypp/media/MediaHandler.cc
zypp/media/Mount.cc

index 1b386fd..1e16ca8 100644 (file)
@@ -84,7 +84,6 @@ MediaAccess::open (const Url& url, const Pathname & preferred_attach_point)
         _handler = new MediaCurl (url,preferred_attach_point);
     else
     {
-       ERR << "Error::E_bad_media_type opening " << url << endl;
        ZYPP_THROW(MediaUnsupportedUrlSchemeException(url));
     }
 
@@ -150,8 +149,7 @@ MediaAccess::close ()
 void MediaAccess::attach (bool next)
 {
   if ( !_handler ) {
-    INT << "Error::E_media_not_open" << endl;
-    ZYPP_THROW(MediaNotOpenException());
+    ZYPP_THROW(MediaNotOpenException("attach"));
   }
   _handler->attach(next);
 }
@@ -189,7 +187,7 @@ void
 MediaAccess::disconnect()
 {
   if ( !_handler )
-    ZYPP_THROW(MediaNotOpenException());
+    ZYPP_THROW(MediaNotOpenException("disconnect"));
 
   _handler->disconnect();
 }
@@ -222,8 +220,7 @@ MediaAccess::provideFile( const Pathname & filename, bool cached, bool checkonly
     ZYPP_THROW(MediaFileNotFoundException(url(), filename));
 
   if ( !_handler ) {
-    INT << "Error::E_media_not_open" << " on provideFile(" << filename << ")" << endl;
-    ZYPP_THROW(MediaNotOpenException());
+    ZYPP_THROW(MediaNotOpenException("provideFile(" + filename.asString() + ")"));
   }
 
   _handler->provideFile( filename );
@@ -246,8 +243,7 @@ void
 MediaAccess::provideDir( const Pathname & dirname ) const
 {
   if ( !_handler ) {
-    INT << "Error::E_media_not_open" << " on provideDir(" << dirname << ")" << endl;
-    ZYPP_THROW(MediaNotOpenException());
+    ZYPP_THROW(MediaNotOpenException("provideDir(" + dirname.asString() + ")"));
   }
 
   _handler->provideDir( dirname );
@@ -257,8 +253,7 @@ void
 MediaAccess::provideDirTree( const Pathname & dirname ) const
 {
   if ( !_handler ) {
-    INT << "Error::E_media_not_open" << " on provideDirTree(" << dirname << ")" << endl;
-    ZYPP_THROW(MediaNotOpenException());
+    ZYPP_THROW(MediaNotOpenException("provideDirTree(" + dirname.asString() + ")"));
   }
 
   _handler->provideDirTree( dirname );
@@ -289,8 +284,7 @@ MediaAccess::dirInfo( list<string> & retlist, const Pathname & dirname, bool dot
   retlist.clear();
 
   if ( !_handler ) {
-    INT << "Error::E_media_not_open" << " on dirInfo(" << dirname << ")" << endl;
-    ZYPP_THROW(MediaNotOpenException());
+    ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
   }
 
   _handler->dirInfo( retlist, dirname, dots );
@@ -303,8 +297,7 @@ MediaAccess::dirInfo( filesystem::DirContent & retlist, const Pathname & dirname
   retlist.clear();
 
   if ( !_handler ) {
-    INT << "Error::E_media_not_open" << " on dirInfo(" << dirname << ")" << endl;
-    ZYPP_THROW(MediaNotOpenException());
+    ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
   }
 
   _handler->dirInfo( retlist, dirname, dots );
index 9490eb5..91badd6 100644 (file)
@@ -16,6 +16,7 @@
 #include "zypp/ExternalProgram.h"
 #include "zypp/media/Mount.h"
 #include "zypp/media/MediaCD.h"
+#include "zypp/Url.h"
 
 #include <cstring> // strerror
 
@@ -145,7 +146,7 @@ namespace zypp {
     void MediaCD::attachTo(bool next)
     {
       Mount mount;
-      const char *mountpoint = attachPoint().asString().c_str();
+      string mountpoint = attachPoint().asString();
       bool mountsucceeded = false;
       int count = 0;
     
@@ -190,7 +191,7 @@ namespace zypp {
            ; ++fsit)
        {
          try {
-           mount.mount (*it, mountpoint, *fsit, options);
+           mount.mount (*it, mountpoint.c_str(), *fsit, options);
            _mounteddevice = *it;
            _lastdev = count;
            mountsucceeded = true;
@@ -206,10 +207,7 @@ namespace zypp {
       {
        _mounteddevice.erase();
        _lastdev = -1;
-       // FIXME not suitable for MediaMountExeption, as device cannot be
-       // specified here
-       // errors might be different as well
-       ZYPP_THROW(MediaException("Error::E_mount_failed"));
+        ZYPP_THROW(MediaMountException(_url.toString(), mountpoint, "Mounting media failed"));
       }
       DBG << _lastdev << " " << count << endl;
     }
index a7ac548..6c3cb86 100644 (file)
@@ -42,7 +42,7 @@ namespace zypp
 
     std::ostream & MediaNotOpenException::dumpOn( std::ostream & str ) const
     {
-      return str << "Media not opened." << endl;
+      return str << "Media not opened while performing action " << _action << endl;
     }
 
     std::ostream & MediaFileNotFoundException::dumpOn( std::ostream & str) const
@@ -61,6 +61,11 @@ namespace zypp
       return str << "Media not attached: " << _url << endl;
     }
 
+    std::ostream & MediaBadAttachPointException::dumpOn( std::ostream & str) const
+    {
+      return str << "Bad media attach point: " << _url << endl;
+    }
+
     std::ostream & MediaSystemException::dumpOn( std::ostream & str) const
     {
       return str << "System exception: " << _message
index 8c285e8..44860c3 100644 (file)
@@ -112,13 +112,15 @@ namespace zypp
     class MediaNotOpenException : public MediaException
     {
     public:
-      MediaNotOpenException()
+      MediaNotOpenException(const std::string & action_r)
       : MediaException()
+      , _action(action_r)
       {}
       virtual ~MediaNotOpenException() throw() {};
     protected:
       virtual std::ostream & dumpOn( std::ostream & str ) const;
     private:
+      std::string _action;
     };
 
     class MediaFileNotFoundException : public MediaException
@@ -166,6 +168,20 @@ namespace zypp
       std::string _url;
     };
 
+    class MediaBadAttachPointException : public MediaException
+    {
+    public:
+      MediaBadAttachPointException(const Url & url_r)
+      : MediaException()
+      , _url(url_r.toString())
+      {}
+      virtual ~MediaBadAttachPointException() throw() {};
+    protected:
+      virtual std::ostream & dumpOn( std::ostream & str ) const;
+    private:
+      std::string _url;
+    };
+
     class MediaSystemException : public MediaException
     {
     public:
index 87cbd49..e4315ad 100644 (file)
@@ -152,18 +152,11 @@ void MediaHandler::attach( bool next )
     return;
 
   if ( _attachPoint.empty() ) {
-    ERR << "Error::E_bad_attachpoint" << endl;
-    ZYPP_THROW( MediaException("Error::E_bad_attachpoint") );
+    ERR << "Bad Attachpoint" << endl;
+    ZYPP_THROW( MediaBadAttachPointException(url()));
   }
 
-  try {
-    attachTo( next ); // pass to concrete handler
-  }
-  catch (const MediaException & excpt_r)
-  {
-    WAR << "Attach failed: " << excpt_r << " " << *this << endl;
-    ZYPP_RETHROW(excpt_r);
-  }
+  attachTo( next ); // pass to concrete handler
   _isAttached = true;
   MIL << "Attached: " << *this << endl;
 }
@@ -203,14 +196,7 @@ void MediaHandler::disconnect()
   if ( !_isAttached )
     return;
 
-  try {
-    disconnectFrom(); // pass to concrete handler
-  }
-  catch (const MediaException & excpt_r)
-  {
-    WAR << "Disconnect failed: " << excpt_r << " " << *this << endl;
-    ZYPP_RETHROW(excpt_r);
-  }
+  disconnectFrom(); // pass to concrete handler
   MIL << "Disconnected: " << *this << endl;
 }
 
@@ -230,14 +216,7 @@ void MediaHandler::release( bool eject )
     return;
   }
 
-  try {
-    releaseFrom( eject ); // pass to concrete handler
-  }
-  catch (const MediaException & excpt_r)
-  {
-    WAR << "Release failed: " << excpt_r << " " << *this << endl;
-    ZYPP_RETHROW(excpt_r);
-  }
+  releaseFrom( eject ); // pass to concrete handler
   _isAttached = false;
   MIL << "Released: " << *this << endl;
 }
@@ -254,19 +233,12 @@ void MediaHandler::provideFileCopy( Pathname srcFilename,
                                        Pathname targetFilename ) const
 {
   if ( !_isAttached ) {
-    INT << "Error::E_not_attached" << " on provideFileCopy(" << srcFilename
+    INT << "Media not_attached on provideFileCopy(" << srcFilename
         << "," << targetFilename << ")" << endl;
     ZYPP_THROW(MediaNotAttachedException(url()));
   }
 
-  try {
-    getFileCopy( srcFilename, targetFilename ); // pass to concrete handler
-  }
-  catch (const MediaException & excpt_r)
-  {
-    WAR << "provideFileCopy(" << srcFilename << "," << targetFilename << "): " << excpt_r << endl;
-    ZYPP_RETHROW(excpt_r);
-  }
+  getFileCopy( srcFilename, targetFilename ); // pass to concrete handler
   DBG << "provideFileCopy(" << srcFilename << "," << targetFilename  << ")" << endl;
 }
 
@@ -277,14 +249,7 @@ void MediaHandler::provideFile( Pathname filename ) const
     ZYPP_THROW(MediaNotAttachedException(url()));
   }
 
-  try {
-    getFile( filename ); // pass to concrete handler
-  }
-  catch (const MediaException & excpt_r)
-  {
-    WAR << "provideFile(" << filename << "): " << excpt_r << endl;
-    ZYPP_RETHROW(excpt_r);
-  }
+  getFile( filename ); // pass to concrete handler
   DBG << "provideFile(" << filename << ")" << endl;
 }
 
@@ -303,14 +268,8 @@ void MediaHandler::provideDir( Pathname dirname ) const
     INT << "Error: Not attached on provideDir(" << dirname << ")" << endl;
     ZYPP_THROW(MediaNotAttachedException(url()));
   }
-  try {
-    getDir( dirname, /*recursive*/false ); // pass to concrete handler
-  }
-  catch (const MediaException & excpt_r)
-  {
-    WAR << "provideDir(" << dirname << "): " << excpt_r << endl;
-    ZYPP_RETHROW(excpt_r);
-  }
+
+  getDir( dirname, /*recursive*/false ); // pass to concrete handler
   MIL << "provideDir(" << dirname << ")" << endl;
 }
 
@@ -329,15 +288,7 @@ void MediaHandler::provideDirTree( Pathname dirname ) const
     ZYPP_THROW(MediaNotAttachedException(url()));
   }
 
-  try {
-    getDir( dirname, /*recursive*/true ); // pass to concrete handler
-  }
-  catch (const MediaException & excpt_r)
-  {
-    WAR << "provideDirTree(" << dirname << "): " << excpt_r << endl;
-    ZYPP_RETHROW(excpt_r);
-  }
-
+  getDir( dirname, /*recursive*/true ); // pass to concrete handler
   MIL << "provideDirTree(" << dirname << ")" << endl;
 }
 
@@ -385,14 +336,7 @@ void MediaHandler::dirInfo( list<string> & retlist,
     ZYPP_THROW(MediaNotAttachedException(url()));
   }
 
-  try {
-    getDirInfo( retlist, dirname, dots ); // pass to concrete handler
-  }
-  catch (const MediaException & excpt_r)
-  {
-    WAR << "dirInfo(" << dirname << "): " << excpt_r << endl;
-    ZYPP_RETHROW(excpt_r);
-  }
+  getDirInfo( retlist, dirname, dots ); // pass to concrete handler
   MIL << "dirInfo(" << dirname << ")" << endl;
 }
 
@@ -414,14 +358,7 @@ void MediaHandler::dirInfo( filesystem::DirContent & retlist,
     ZYPP_THROW(MediaNotAttachedException(url()));
   }
 
-  try {
-    getDirInfo( retlist, dirname, dots ); // pass to concrete handler
-  }
-  catch (const MediaException & excpt_r)
-  {
-    WAR << "dirInfo(" << dirname << "): " << excpt_r << endl;
-    ZYPP_RETHROW(excpt_r);
-  }
+  getDirInfo( retlist, dirname, dots ); // pass to concrete handler
   MIL << "dirInfo(" << dirname << ")" << endl;
 }
 
@@ -437,13 +374,7 @@ void MediaHandler::getDirectoryYast( std::list<std::string> & retlist,
   retlist.clear();
 
   filesystem::DirContent content;
-  try {
-    getDirectoryYast( content, dirname, dots );
-  }
-  catch (const MediaException & excpt_r)
-  {
-    ZYPP_RETHROW(excpt_r);
-  }
+  getDirectoryYast( content, dirname, dots );
 
   // convert to std::list<std::string>
   for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
@@ -464,14 +395,7 @@ void MediaHandler::getDirectoryYast( filesystem::DirContent & retlist,
 
   // look for directory.yast
   Pathname dirFile = dirname + "directory.yast";
-  try {
-    getFile( dirFile );
-  }
-  catch (const MediaException & excpt_r)
-  {
-    ERR << "provideFile(" << dirFile << "): " << excpt_r << endl;
-    ZYPP_RETHROW(excpt_r);
-  }
+  getFile( dirFile );
   DBG << "provideFile(" << dirFile << "): " << "OK" << endl;
 
   // using directory.yast
@@ -543,13 +467,7 @@ void MediaHandler::getFile( const Pathname & filename ) const
 
 void MediaHandler::getFileCopy ( const Pathname & srcFilename, const Pathname & targetFilename ) const
 {
-  try {
-    getFile(srcFilename);
-  }
-  catch (const MediaException & excpt_r)
-  {
-    ZYPP_RETHROW(excpt_r);
-  }
+  getFile(srcFilename);
 
   if ( copy( localPath( srcFilename ), targetFilename ) != 0 ) {
     ZYPP_THROW(MediaWriteException(targetFilename));
index 3dfd8d5..e75af98 100644 (file)
@@ -70,7 +70,7 @@ void Mount::mount ( const string& source,
 
     if ( process == NULL )
     {
-      ZYPP_THROW(MediaMountException(source, target, "Error::E_mount_failed"));
+      ZYPP_THROW(MediaMountException(source, target, "Mounting media failed"));
     }
 
     string value;
@@ -96,15 +96,15 @@ void Mount::mount ( const string& source,
 
        if  ( value.find ( "is already mounted on" ) != string::npos )
        {
-           err = "Error::E_already_mounted";
+           err = "Media already mounted";
        }
        else if  ( value.find ( "ermission denied" ) != string::npos )
        {
-           err = "Error::E_no_permission";
+           err = "Permission denied";
        }
        else if  ( value.find ( "wrong fs type" ) != string::npos )
        {
-           err = "Error::E_invalid_filesystem";
+           err = "Invalid filesystem on media";
        }
 
        output = process->receiveLine();
@@ -119,7 +119,7 @@ void Mount::mount ( const string& source,
     }
     else if ( status != 0 && err == "" )
     {
-        err = "Error::E_mount_failed";
+        err = "Mounting media failed";
     }
 
     if ( err != "" ) {
@@ -175,7 +175,7 @@ void Mount::umount (const string& path)
 
        if  ( value.find ( "device is busy" ) != string::npos )
        {
-           err = "Error::E_busy";
+           err = "Device is busy";
        }
 
        output = process->receiveLine();
@@ -190,7 +190,7 @@ void Mount::umount (const string& path)
     }
     else if ( status != 0 && err == "" )
     {
-       err = "Error::E_umount_failed";
+       err = "Unmounting media failed";
     }
 
     if ( err != "") {