Use our own exceptions.
authorCarl Hetherington <cth@carlh.net>
Tue, 17 Jul 2012 23:40:20 +0000 (00:40 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 17 Jul 2012 23:40:20 +0000 (00:40 +0100)
src/exceptions.h [new file with mode: 0644]
src/picture_asset.cc
src/sound_asset.cc
src/util.cc
src/wscript

diff --git a/src/exceptions.h b/src/exceptions.h
new file mode 100644 (file)
index 0000000..0dbbec4
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+/** @file  src/exceptions.h
+ *  @brief Exceptions thrown by libdcp.
+ */
+
+namespace libdcp
+{
+
+class FileError : public std::exception
+{
+public:
+       FileError (std::string const & message, std::string const & filename)
+               : _message (message)
+               , _filename (filename)
+       {}
+                           
+       ~FileError () throw () {}
+
+       char const * what () const throw () {
+               return _message.c_str ();
+       }
+       
+       std::string filename () const {
+               return _filename;
+       }
+
+private:
+       std::string _message;
+       std::string _filename;
+};
+
+class MiscError : public std::exception
+{
+public:
+       MiscError (std::string const & message) : _message (message) {}
+       ~MiscError () throw () {}
+
+       char const * what () const throw () {
+               return _message.c_str ();
+       }
+
+private:
+       std::string _message;
+};
+
+}
index 383d218a6440f9799885bef088f902c3ee44c9df..1620a1fa7d2de01ad7bfdc3409ba8decb1747ba3 100644 (file)
@@ -30,6 +30,7 @@
 #include "KM_fileio.h"
 #include "picture_asset.h"
 #include "util.h"
+#include "exceptions.h"
 
 using namespace std;
 using namespace boost;
@@ -77,9 +78,7 @@ PictureAsset::construct (sigc::slot<string, int> get_path)
        ASDCP::JP2K::CodestreamParser j2k_parser;
        ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
        if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).c_str(), frame_buffer))) {
-               stringstream s;
-               s << "could not open " << get_path(0) << " for reading";
-               throw runtime_error (s.str());
+               throw FileError ("could not open JPEG2000 file for reading", get_path (0));
        }
        
        ASDCP::JP2K::PictureDescriptor picture_desc;
@@ -91,7 +90,7 @@ PictureAsset::construct (sigc::slot<string, int> get_path)
        
        ASDCP::JP2K::MXFWriter mxf_writer;
        if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, picture_desc))) {
-               throw runtime_error ("could not open MXF for writing");
+               throw FileError ("could not open MXF file for writing", _mxf_path);
        }
 
        for (int i = 0; i < _length; ++i) {
@@ -99,21 +98,19 @@ PictureAsset::construct (sigc::slot<string, int> get_path)
                string const path = get_path (i);
                
                if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.c_str(), frame_buffer))) {
-                       stringstream s;
-                       s << "could not open " << path << " for reading";
-                       throw runtime_error (s.str());
+                       throw FileError ("could not open JPEG2000 file for reading", path);
                }
 
                /* XXX: passing 0 to WriteFrame ok? */
                if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
-                       throw runtime_error ("error in writing video MXF");
+                       throw MiscError ("error in writing video MXF");
                }
                
                (*_progress) (0.5 * float (i) / _length);
        }
        
        if (ASDCP_FAILURE (mxf_writer.Finalize())) {
-               throw runtime_error ("error in finalising video MXF");
+               throw MiscError ("error in finalising video MXF");
        }
 
        _digest = make_digest (_mxf_path, _progress);
index 0d32db2ab2f833a39eca9a4d8b3a5a344f2e0506..d2c451fb76ac033eb0ac57f64d59da5d8016a859 100644 (file)
@@ -27,6 +27,7 @@
 #include "AS_DCP.h"
 #include "sound_asset.h"
 #include "util.h"
+#include "exceptions.h"
 
 using namespace std;
 using namespace boost;
@@ -65,7 +66,7 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
        
        ASDCP::PCM::WAVParser pcm_parser_channel[_channels];
        if (pcm_parser_channel[0].OpenRead (get_path(LEFT).c_str(), asdcp_fps)) {
-               throw runtime_error ("could not open WAV file for reading");
+               throw FileError ("could not open WAV file for reading", get_path(LEFT));
        }
        
        ASDCP::PCM::AudioDescriptor audio_desc;
@@ -92,7 +93,7 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
                string const path = get_path (channels[i]);
                
                if (ASDCP_FAILURE (pcm_parser_channel[i].OpenRead (path.c_str(), asdcp_fps))) {
-                       throw runtime_error ("could not open WAV file for reading");
+                       throw FileError ("could not open WAV file for reading", path);
                }
 
                pcm_parser_channel[i].FillAudioDescriptor (audio_desc_channel[i]);
@@ -111,7 +112,7 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
 
        ASDCP::PCM::MXFWriter mxf_writer;
        if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, audio_desc))) {
-               throw runtime_error ("could not open audio MXF for writing");
+               throw FileError ("could not open audio MXF for writing", _mxf_path);
        }
 
        for (int i = 0; i < _length; ++i) {
@@ -124,11 +125,11 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
                for (int j = 0; j < _channels; ++j) {
                        memset (frame_buffer_channel[j].Data(), 0, frame_buffer_channel[j].Capacity());
                        if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
-                               throw runtime_error ("could not read audio frame");
+                               throw MiscError ("could not read audio frame");
                        }
                        
                        if (frame_buffer_channel[j].Size() != frame_buffer_channel[j].Capacity()) {
-                               throw runtime_error ("short audio frame");
+                               throw MiscError ("short audio frame");
                        }
                }
 
@@ -142,14 +143,14 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
                }
 
                if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
-                       throw runtime_error ("could not write audio MXF frame");
+                       throw MiscError ("could not write audio MXF frame");
                }
 
                (*_progress) (0.5 * float (i) / _length);
        }
 
        if (ASDCP_FAILURE (mxf_writer.Finalize())) {
-               throw runtime_error ("could not finalise audio MXF");
+               throw MiscError ("could not finalise audio MXF");
        }
 
        _digest = make_digest (_mxf_path, _progress);
index 2968ef55f49885fb4a488f9db5bc54fc52735532..849b7d24b614d267293ae9c6dd027cad712cba71 100644 (file)
@@ -31,6 +31,7 @@
 #include "KM_fileio.h"
 #include "AS_DCP.h"
 #include "util.h"
+#include "exceptions.h"
 
 using namespace std;
 using namespace boost;
@@ -52,7 +53,7 @@ libdcp::make_digest (string filename, sigc::signal1<void, float>* progress)
        
        Kumu::FileReader reader;
        if (ASDCP_FAILURE (reader.OpenRead (filename.c_str ()))) {
-               throw runtime_error ("could not open file to compute digest");
+               throw FileError ("could not open file to compute digest", filename);
        }
        
        SHA_CTX sha;
@@ -67,7 +68,7 @@ libdcp::make_digest (string filename, sigc::signal1<void, float>* progress)
                if (r == Kumu::RESULT_ENDOFFILE) {
                        break;
                } else if (ASDCP_FAILURE (r)) {
-                       throw runtime_error ("could not read file to compute digest");
+                       throw FileError ("could not read file to compute digest", filename);
                }
                
                SHA1_Update (&sha, read_buffer.Data(), read);
index db6906c81fa18ca446ff5a1a20e9103a0c8f13ea..a57bb75b2b1bae8a62555a226769a191f0a56d35 100644 (file)
@@ -18,6 +18,7 @@ def build(bld):
               dcp.h
               metadata.h
               types.h
+              exceptions.h
               """
 
     bld.install_files('${PREFIX}/include/libdcp', headers)