From d4a7fae2735037e8acee9b779db6b5d2cacae963 Mon Sep 17 00:00:00 2001 From: Nestal Wan Date: Sun, 28 Apr 2013 23:50:20 +0800 Subject: [PATCH] refactored exception handling --- CMakeLists.txt | 1 - grive/src/main.cc | 2 +- libgrive/src/http/CurlAgent.cc | 4 ++-- libgrive/src/http/Error.hh | 7 +++++-- libgrive/src/protocol/Json.cc | 2 +- libgrive/src/protocol/Json.hh | 1 + libgrive/src/util/Config.cc | 2 +- libgrive/src/util/Config.hh | 2 +- libgrive/src/util/Crypt.cc | 5 ++++- libgrive/src/util/Crypt.hh | 6 ++++++ libgrive/src/util/Exception.cc | 2 +- libgrive/src/util/Exception.hh | 5 +---- libgrive/src/util/File.cc | 2 +- libgrive/src/xml/Node.cc | 2 +- libgrive/src/xml/Node.hh | 4 ++++ libgrive/src/xml/NodeSet.cc | 2 +- libgrive/src/xml/NodeSet.hh | 4 ++++ libgrive/src/xml/TreeBuilder.cc | 4 ++-- libgrive/src/xml/TreeBuilder.hh | 6 ++++++ 19 files changed, 43 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d16f2d..48f1460 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,6 @@ set( GRIVE_VERSION "0.3.0-pre" ) # common compile options add_definitions( -DVERSION="${GRIVE_VERSION}" ) add_definitions( -D_FILE_OFFSET_BITS=64 ) -add_definitions( -DPROJ_NS=gr ) add_subdirectory( libgrive ) add_subdirectory( grive ) diff --git a/grive/src/main.cc b/grive/src/main.cc index cf3a3d1..b2c4319 100644 --- a/grive/src/main.cc +++ b/grive/src/main.cc @@ -54,7 +54,7 @@ namespace po = boost::program_options; void InitGCrypt() { if ( !gcry_check_version(GCRYPT_VERSION) ) - throw Exception() << expt::ErrMsg( "libgcrypt version mismatch" ) ; + throw std::runtime_error( "libgcrypt version mismatch" ) ; // disable secure memory gcry_control(GCRYCTL_DISABLE_SECMEM, 0); diff --git a/libgrive/src/http/CurlAgent.cc b/libgrive/src/http/CurlAgent.cc index 2cf78b8..87b49c1 100644 --- a/libgrive/src/http/CurlAgent.cc +++ b/libgrive/src/http/CurlAgent.cc @@ -150,7 +150,7 @@ long CurlAgent::ExecCurl( dest->Clear() ; CURLcode curl_code = ::curl_easy_perform(curl); - // get the HTTTP response code + // get the HTTP response code long http_code = 0; ::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); Trace( "HTTP response %1%", http_code ) ; @@ -165,7 +165,7 @@ long CurlAgent::ExecCurl( Error() << CurlCode( curl_code ) << Url( url ) - << expt::ErrMsg( error ) + << CurlErrMsg( error ) << HttpHeader( hdr ) ) ; } diff --git a/libgrive/src/http/Error.hh b/libgrive/src/http/Error.hh index 9b41376..815ee04 100644 --- a/libgrive/src/http/Error.hh +++ b/libgrive/src/http/Error.hh @@ -27,13 +27,16 @@ namespace gr { namespace http { struct Error : virtual Exception {} ; // CURL error code -typedef boost::error_info CurlCode ; +typedef boost::error_info CurlCode ; + +// CURL error message +typedef boost::error_info CurlErrMsg ; // URL typedef boost::error_info Url ; // HTTP headers -typedef boost::error_info HttpHeader ; +typedef boost::error_info HttpHeader ; // HTTP response code typedef boost::error_info HttpResponse ; diff --git a/libgrive/src/protocol/Json.cc b/libgrive/src/protocol/Json.cc index 58da219..d290924 100644 --- a/libgrive/src/protocol/Json.cc +++ b/libgrive/src/protocol/Json.cc @@ -515,7 +515,7 @@ Json Json::Parse( DataStream *in ) BOOST_THROW_EXCEPTION( Error() << JsonCApi_( "json_tokener_parse" ) - << expt::ErrMsg( ::json_tokener_error_desc(err) ) + << ErrMsg_( ::json_tokener_error_desc(err) ) ) ; } diff --git a/libgrive/src/protocol/Json.hh b/libgrive/src/protocol/Json.hh index a1f6bf0..1ebf173 100644 --- a/libgrive/src/protocol/Json.hh +++ b/libgrive/src/protocol/Json.hh @@ -48,6 +48,7 @@ public : typedef boost::error_info KeyNotFound_ ; typedef boost::error_info JsonCApi_ ; typedef boost::error_info Value_ ; + typedef boost::error_info ErrMsg_ ; template struct Val_ diff --git a/libgrive/src/util/Config.cc b/libgrive/src/util/Config.cc index 6111b1d..b5a9f15 100644 --- a/libgrive/src/util/Config.cc +++ b/libgrive/src/util/Config.cc @@ -28,7 +28,7 @@ namespace po = boost::program_options; -namespace PROJ_NS { +namespace gr { const std::string default_filename = ".grive"; const char *env_name = "GR_CONFIG"; diff --git a/libgrive/src/util/Config.hh b/libgrive/src/util/Config.hh index 6057f14..e2c0423 100644 --- a/libgrive/src/util/Config.hh +++ b/libgrive/src/util/Config.hh @@ -31,7 +31,7 @@ namespace boost } } -namespace PROJ_NS { +namespace gr { class Config { diff --git a/libgrive/src/util/Crypt.cc b/libgrive/src/util/Crypt.cc index f0049f5..5fb8ebe 100644 --- a/libgrive/src/util/Crypt.cc +++ b/libgrive/src/util/Crypt.cc @@ -45,7 +45,10 @@ MD5::MD5() : m_impl( new Impl ) ::gcry_error_t err = ::gcry_md_open( &m_impl->hd, GCRY_MD_MD5, 0 ) ; if ( err != GPG_ERR_NO_ERROR ) { - BOOST_THROW_EXCEPTION( Exception() << expt::ErrMsg( ::gcry_strerror(err) ) ) ; + BOOST_THROW_EXCEPTION( Exception() + << GCryptErr_( ::gcry_strerror(err) ) + << GCryptApi_( "gcry_md_open" ) + ) ; } } diff --git a/libgrive/src/util/Crypt.hh b/libgrive/src/util/Crypt.hh index 6cd6ceb..dcd8dd3 100644 --- a/libgrive/src/util/Crypt.hh +++ b/libgrive/src/util/Crypt.hh @@ -19,6 +19,8 @@ #pragma once +#include "util/Exception.hh" + #include #include @@ -32,6 +34,10 @@ namespace crypt { class MD5 { +public : + typedef boost::error_info GCryptErr_ ; + typedef boost::error_info GCryptApi_ ; + public : MD5() ; ~MD5() ; diff --git a/libgrive/src/util/Exception.cc b/libgrive/src/util/Exception.cc index a410bbb..9cf6681 100644 --- a/libgrive/src/util/Exception.cc +++ b/libgrive/src/util/Exception.cc @@ -28,7 +28,7 @@ #include #include -namespace PROJ_NS { +namespace gr { class Backtrace ; diff --git a/libgrive/src/util/Exception.hh b/libgrive/src/util/Exception.hh index 164f9dd..f151584 100644 --- a/libgrive/src/util/Exception.hh +++ b/libgrive/src/util/Exception.hh @@ -25,7 +25,7 @@ #include #include -namespace PROJ_NS { +namespace gr { class Backtrace ; @@ -51,9 +51,6 @@ namespace expt { // back-trace information. should be present for all exceptions typedef boost::error_info Backtrace_ ; - - /// generic error message - typedef boost::error_info ErrMsg ; } } // end of namespace diff --git a/libgrive/src/util/File.cc b/libgrive/src/util/File.cc index e6c047b..375cd62 100644 --- a/libgrive/src/util/File.cc +++ b/libgrive/src/util/File.cc @@ -78,7 +78,7 @@ struct stat FStat( int fd ) } // end of local functions -namespace PROJ_NS { +namespace gr { File::File( ) : m_fd( -1 ) { diff --git a/libgrive/src/xml/Node.cc b/libgrive/src/xml/Node.cc index be12d31..abed0c4 100644 --- a/libgrive/src/xml/Node.cc +++ b/libgrive/src/xml/Node.cc @@ -92,7 +92,7 @@ public : // cannot allow duplicate attribute nodes if ( child->m_type == attr && p.first != p.second ) - BOOST_THROW_EXCEPTION( Error() << expt::ErrMsg( "duplicate attribute " + child->m_name ) ) ; + BOOST_THROW_EXCEPTION( Error() << DupAttr_( child->m_name ) ) ; vec.insert( p.second, child ) ; } diff --git a/libgrive/src/xml/Node.hh b/libgrive/src/xml/Node.hh index 85c2a6f..fb4b6c4 100644 --- a/libgrive/src/xml/Node.hh +++ b/libgrive/src/xml/Node.hh @@ -19,6 +19,8 @@ #pragma once +#include "util/Exception.hh" + #include #include @@ -39,6 +41,8 @@ private : public : class iterator ; + typedef boost::error_info DupAttr_ ; + public : Node() ; Node( const Node& node ) ; diff --git a/libgrive/src/xml/NodeSet.cc b/libgrive/src/xml/NodeSet.cc index 8ccc82f..78b7493 100644 --- a/libgrive/src/xml/NodeSet.cc +++ b/libgrive/src/xml/NodeSet.cc @@ -124,7 +124,7 @@ NodeSet NodeSet::operator[]( const std::string& name ) const Node NodeSet::front() const { if ( empty() ) - throw Error() << expt::ErrMsg( "empty node set" ) ; + BOOST_THROW_EXCEPTION( Error() << EmptyNodeSet_(0) ) ; return *m_first ; } diff --git a/libgrive/src/xml/NodeSet.hh b/libgrive/src/xml/NodeSet.hh index 98cb97c..8820a8f 100644 --- a/libgrive/src/xml/NodeSet.hh +++ b/libgrive/src/xml/NodeSet.hh @@ -19,6 +19,8 @@ #pragma once +#include "util/Exception.hh" + #include "Node.hh" #include @@ -32,6 +34,8 @@ class NodeSet public : typedef Node::iterator iterator ; + typedef boost::error_info EmptyNodeSet_ ; + public : NodeSet() ; NodeSet( const NodeSet& n ) ; diff --git a/libgrive/src/xml/TreeBuilder.cc b/libgrive/src/xml/TreeBuilder.cc index 66be334..3a31b65 100644 --- a/libgrive/src/xml/TreeBuilder.cc +++ b/libgrive/src/xml/TreeBuilder.cc @@ -69,7 +69,7 @@ Node TreeBuilder::ParseFile( const std::string& file ) void TreeBuilder::ParseData( const char *data, std::size_t count, bool last ) { if ( ::XML_Parse( m_impl->psr, data, count, last ) == 0 ) - throw Error() << expt::ErrMsg( "XML parse error" ) ; + BOOST_THROW_EXCEPTION( Error() << ExpatApiError("XML_Parse") ); } Node TreeBuilder::Parse( const std::string& xml ) @@ -85,7 +85,7 @@ Node TreeBuilder::Result() const assert( m_impl->stack.size() == 1 ) ; if ( m_impl->stack.front().size() != 1 ) - throw Error() << expt::ErrMsg( "invalid node" ) ; + BOOST_THROW_EXCEPTION( Error() << LogicError(0) ) ; return *m_impl->stack.front().begin() ; } diff --git a/libgrive/src/xml/TreeBuilder.hh b/libgrive/src/xml/TreeBuilder.hh index 184b55f..c2a04f1 100644 --- a/libgrive/src/xml/TreeBuilder.hh +++ b/libgrive/src/xml/TreeBuilder.hh @@ -19,6 +19,8 @@ #pragma once +#include "util/Exception.hh" + #include #include @@ -28,6 +30,10 @@ class Node ; class TreeBuilder { +public : + typedef boost::error_info ExpatApiError ; + typedef boost::error_info LogicError ; + public : TreeBuilder() ; ~TreeBuilder() ;