refactored exception handling

pull/40/head
Nestal Wan 2013-04-28 23:50:20 +08:00
parent 39b2b4fb0b
commit d4a7fae273
19 changed files with 43 additions and 20 deletions

View File

@ -6,7 +6,6 @@ set( GRIVE_VERSION "0.3.0-pre" )
# common compile options # common compile options
add_definitions( -DVERSION="${GRIVE_VERSION}" ) add_definitions( -DVERSION="${GRIVE_VERSION}" )
add_definitions( -D_FILE_OFFSET_BITS=64 ) add_definitions( -D_FILE_OFFSET_BITS=64 )
add_definitions( -DPROJ_NS=gr )
add_subdirectory( libgrive ) add_subdirectory( libgrive )
add_subdirectory( grive ) add_subdirectory( grive )

View File

@ -54,7 +54,7 @@ namespace po = boost::program_options;
void InitGCrypt() void InitGCrypt()
{ {
if ( !gcry_check_version(GCRYPT_VERSION) ) if ( !gcry_check_version(GCRYPT_VERSION) )
throw Exception() << expt::ErrMsg( "libgcrypt version mismatch" ) ; throw std::runtime_error( "libgcrypt version mismatch" ) ;
// disable secure memory // disable secure memory
gcry_control(GCRYCTL_DISABLE_SECMEM, 0); gcry_control(GCRYCTL_DISABLE_SECMEM, 0);

View File

@ -150,7 +150,7 @@ long CurlAgent::ExecCurl(
dest->Clear() ; dest->Clear() ;
CURLcode curl_code = ::curl_easy_perform(curl); CURLcode curl_code = ::curl_easy_perform(curl);
// get the HTTTP response code // get the HTTP response code
long http_code = 0; long http_code = 0;
::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); ::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
Trace( "HTTP response %1%", http_code ) ; Trace( "HTTP response %1%", http_code ) ;
@ -165,7 +165,7 @@ long CurlAgent::ExecCurl(
Error() Error()
<< CurlCode( curl_code ) << CurlCode( curl_code )
<< Url( url ) << Url( url )
<< expt::ErrMsg( error ) << CurlErrMsg( error )
<< HttpHeader( hdr ) << HttpHeader( hdr )
) ; ) ;
} }

View File

@ -27,13 +27,16 @@ namespace gr { namespace http {
struct Error : virtual Exception {} ; struct Error : virtual Exception {} ;
// CURL error code // CURL error code
typedef boost::error_info<struct CurlCodeTag, int> CurlCode ; typedef boost::error_info<struct CurlCodeTag, int> CurlCode ;
// CURL error message
typedef boost::error_info<struct CurlErrMsgTag, std::string> CurlErrMsg ;
// URL // URL
typedef boost::error_info<struct UrlTag, std::string> Url ; typedef boost::error_info<struct UrlTag, std::string> Url ;
// HTTP headers // HTTP headers
typedef boost::error_info<struct HeaderTag, Header> HttpHeader ; typedef boost::error_info<struct HeaderTag, Header> HttpHeader ;
// HTTP response code // HTTP response code
typedef boost::error_info<struct HttpResponseTag, int> HttpResponse ; typedef boost::error_info<struct HttpResponseTag, int> HttpResponse ;

View File

@ -515,7 +515,7 @@ Json Json::Parse( DataStream *in )
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(
Error() Error()
<< JsonCApi_( "json_tokener_parse" ) << JsonCApi_( "json_tokener_parse" )
<< expt::ErrMsg( ::json_tokener_error_desc(err) ) << ErrMsg_( ::json_tokener_error_desc(err) )
) ; ) ;
} }

View File

@ -48,6 +48,7 @@ public :
typedef boost::error_info<struct KeyNotFound, std::string> KeyNotFound_ ; typedef boost::error_info<struct KeyNotFound, std::string> KeyNotFound_ ;
typedef boost::error_info<struct JsonCApi, std::string> JsonCApi_ ; typedef boost::error_info<struct JsonCApi, std::string> JsonCApi_ ;
typedef boost::error_info<struct Value, std::string> Value_ ; typedef boost::error_info<struct Value, std::string> Value_ ;
typedef boost::error_info<struct ErrMsg, std::string> ErrMsg_ ;
template <typename T> template <typename T>
struct Val_ struct Val_

View File

@ -28,7 +28,7 @@
namespace po = boost::program_options; namespace po = boost::program_options;
namespace PROJ_NS { namespace gr {
const std::string default_filename = ".grive"; const std::string default_filename = ".grive";
const char *env_name = "GR_CONFIG"; const char *env_name = "GR_CONFIG";

View File

@ -31,7 +31,7 @@ namespace boost
} }
} }
namespace PROJ_NS { namespace gr {
class Config class Config
{ {

View File

@ -45,7 +45,10 @@ MD5::MD5() : m_impl( new Impl )
::gcry_error_t err = ::gcry_md_open( &m_impl->hd, GCRY_MD_MD5, 0 ) ; ::gcry_error_t err = ::gcry_md_open( &m_impl->hd, GCRY_MD_MD5, 0 ) ;
if ( err != GPG_ERR_NO_ERROR ) 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" )
) ;
} }
} }

View File

@ -19,6 +19,8 @@
#pragma once #pragma once
#include "util/Exception.hh"
#include <string> #include <string>
#include <memory> #include <memory>
@ -32,6 +34,10 @@ namespace crypt {
class MD5 class MD5
{ {
public :
typedef boost::error_info<struct GCryptErr, std::string> GCryptErr_ ;
typedef boost::error_info<struct GCryptApi, std::string> GCryptApi_ ;
public : public :
MD5() ; MD5() ;
~MD5() ; ~MD5() ;

View File

@ -28,7 +28,7 @@
#include <iterator> #include <iterator>
#include <sstream> #include <sstream>
namespace PROJ_NS { namespace gr {
class Backtrace ; class Backtrace ;

View File

@ -25,7 +25,7 @@
#include <exception> #include <exception>
#include <string> #include <string>
namespace PROJ_NS { namespace gr {
class Backtrace ; class Backtrace ;
@ -51,9 +51,6 @@ namespace expt
{ {
// back-trace information. should be present for all exceptions // back-trace information. should be present for all exceptions
typedef boost::error_info<struct BacktraceTag, Backtrace> Backtrace_ ; typedef boost::error_info<struct BacktraceTag, Backtrace> Backtrace_ ;
/// generic error message
typedef boost::error_info<struct MsgTag, std::string> ErrMsg ;
} }
} // end of namespace } // end of namespace

View File

@ -78,7 +78,7 @@ struct stat FStat( int fd )
} // end of local functions } // end of local functions
namespace PROJ_NS { namespace gr {
File::File( ) : m_fd( -1 ) File::File( ) : m_fd( -1 )
{ {

View File

@ -92,7 +92,7 @@ public :
// cannot allow duplicate attribute nodes // cannot allow duplicate attribute nodes
if ( child->m_type == attr && p.first != p.second ) 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 ) ; vec.insert( p.second, child ) ;
} }

View File

@ -19,6 +19,8 @@
#pragma once #pragma once
#include "util/Exception.hh"
#include <boost/iterator_adaptors.hpp> #include <boost/iterator_adaptors.hpp>
#include <iosfwd> #include <iosfwd>
@ -39,6 +41,8 @@ private :
public : public :
class iterator ; class iterator ;
typedef boost::error_info<struct DupAttr, std::string> DupAttr_ ;
public : public :
Node() ; Node() ;
Node( const Node& node ) ; Node( const Node& node ) ;

View File

@ -124,7 +124,7 @@ NodeSet NodeSet::operator[]( const std::string& name ) const
Node NodeSet::front() const Node NodeSet::front() const
{ {
if ( empty() ) if ( empty() )
throw Error() << expt::ErrMsg( "empty node set" ) ; BOOST_THROW_EXCEPTION( Error() << EmptyNodeSet_(0) ) ;
return *m_first ; return *m_first ;
} }

View File

@ -19,6 +19,8 @@
#pragma once #pragma once
#include "util/Exception.hh"
#include "Node.hh" #include "Node.hh"
#include <cstddef> #include <cstddef>
@ -32,6 +34,8 @@ class NodeSet
public : public :
typedef Node::iterator iterator ; typedef Node::iterator iterator ;
typedef boost::error_info<struct EmptyNodeSet, int> EmptyNodeSet_ ;
public : public :
NodeSet() ; NodeSet() ;
NodeSet( const NodeSet& n ) ; NodeSet( const NodeSet& n ) ;

View File

@ -69,7 +69,7 @@ Node TreeBuilder::ParseFile( const std::string& file )
void TreeBuilder::ParseData( const char *data, std::size_t count, bool last ) void TreeBuilder::ParseData( const char *data, std::size_t count, bool last )
{ {
if ( ::XML_Parse( m_impl->psr, data, count, last ) == 0 ) 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 ) Node TreeBuilder::Parse( const std::string& xml )
@ -85,7 +85,7 @@ Node TreeBuilder::Result() const
assert( m_impl->stack.size() == 1 ) ; assert( m_impl->stack.size() == 1 ) ;
if ( m_impl->stack.front().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() ; return *m_impl->stack.front().begin() ;
} }

View File

@ -19,6 +19,8 @@
#pragma once #pragma once
#include "util/Exception.hh"
#include <memory> #include <memory>
#include <string> #include <string>
@ -28,6 +30,10 @@ class Node ;
class TreeBuilder class TreeBuilder
{ {
public :
typedef boost::error_info<struct ExpatApiError_, std::string> ExpatApiError ;
typedef boost::error_info<struct LogicError_, int> LogicError ;
public : public :
TreeBuilder() ; TreeBuilder() ;
~TreeBuilder() ; ~TreeBuilder() ;