Make all exception classes inherit from std::exception

This refactors out a very common pattern of formatting "%s: %s" with
e.function.c_str() and strerror( e.the_errno ) into just the what()
method of NetworkException. It's also a prerequisite for making cleaner
public API for any exceptions we throw, and allows us to more easily
get exceptions passed back to us to handle.
This commit is contained in:
Geoffrey Thomas
2013-08-03 16:32:23 -07:00
committed by John Hood
parent ebecb9bd3a
commit 5721b392ab
11 changed files with 41 additions and 30 deletions
+4 -1
View File
@@ -38,18 +38,21 @@
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <exception>
using std::string; using std::string;
long int myatoi( const char *str ); long int myatoi( const char *str );
namespace Crypto { namespace Crypto {
class CryptoException { class CryptoException : public std::exception {
public: public:
string text; string text;
bool fatal; bool fatal;
CryptoException( string s_text, bool s_fatal = false ) CryptoException( string s_text, bool s_fatal = false )
: text( s_text ), fatal( s_fatal ) {}; : text( s_text ), fatal( s_fatal ) {};
const char *what() const throw () { return text.c_str(); }
~CryptoException() throw () {}
}; };
/* 16-byte-aligned buffer, with length. */ /* 16-byte-aligned buffer, with length. */
+1 -1
View File
@@ -62,7 +62,7 @@ int main( int argc, char *argv[] )
(long)message.nonce.val() ); (long)message.nonce.val() );
cout << message.text; cout << message.text;
} catch ( const CryptoException &e ) { } catch ( const CryptoException &e ) {
cerr << e.text << endl; cerr << e.what() << endl;
exit( 1 ); exit( 1 );
} }
+1 -1
View File
@@ -63,7 +63,7 @@ int main( int argc, char *argv[] )
cout << ciphertext; cout << ciphertext;
} catch ( const CryptoException &e ) { } catch ( const CryptoException &e ) {
cerr << e.text << endl; cerr << e.what() << endl;
exit( 1 ); exit( 1 );
} }
+4 -4
View File
@@ -66,7 +66,7 @@ int main( int argc, char *argv[] )
n = new Transport<UserStream, UserStream>( me, remote, NULL, NULL ); n = new Transport<UserStream, UserStream>( me, remote, NULL, NULL );
} }
} catch ( const CryptoException &e ) { } catch ( const CryptoException &e ) {
fprintf( stderr, "Fatal error: %s\n", e.text.c_str() ); fprintf( stderr, "Fatal error: %s\n", e.what() );
exit( 1 ); exit( 1 );
} }
@@ -98,7 +98,7 @@ int main( int argc, char *argv[] )
} }
} }
} catch ( const CryptoException &e ) { } catch ( const CryptoException &e ) {
fprintf( stderr, "Cryptographic error: %s\n", e.text.c_str() ); fprintf( stderr, "Cryptographic error: %s\n", e.what() );
} }
} }
} else { } else {
@@ -164,10 +164,10 @@ int main( int argc, char *argv[] )
n->recv(); n->recv();
} }
} catch ( const NetworkException &e ) { } catch ( const NetworkException &e ) {
fprintf( stderr, "%s: %s\r\n", e.function.c_str(), strerror( e.the_errno ) ); fprintf( stderr, "%s\n", e.what() );
break; break;
} catch ( const CryptoException &e ) { } catch ( const CryptoException &e ) {
fprintf( stderr, "Cryptographic error: %s\n", e.text.c_str() ); fprintf( stderr, "Cryptographic error: %s\n", e.what() );
} }
} }
+3 -4
View File
@@ -34,7 +34,6 @@
#include "version.h" #include "version.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "stmclient.h" #include "stmclient.h"
@@ -177,11 +176,11 @@ int main( int argc, char *argv[] )
client.shutdown(); client.shutdown();
} catch ( const Network::NetworkException& e ) { } catch ( const Network::NetworkException& e ) {
fprintf( stderr, "Network exception: %s: %s\r\n", fprintf( stderr, "Network exception: %s\r\n",
e.function.c_str(), strerror( e.the_errno ) ); e.what() );
} catch ( const Crypto::CryptoException& e ) { } catch ( const Crypto::CryptoException& e ) {
fprintf( stderr, "Crypto exception: %s\r\n", fprintf( stderr, "Crypto exception: %s\r\n",
e.text.c_str() ); e.what() );
} catch ( const std::string& s ) { } catch ( const std::string& s ) {
fprintf( stderr, "Error: %s\r\n", s.c_str() ); fprintf( stderr, "Error: %s\r\n", s.c_str() );
} }
+8 -8
View File
@@ -311,12 +311,12 @@ int main( int argc, char *argv[] )
try { try {
return run_server( desired_ip, desired_port, command_path, command_argv, colors, verbose, with_motd ); return run_server( desired_ip, desired_port, command_path, command_argv, colors, verbose, with_motd );
} catch ( const Network::NetworkException& e ) { } catch ( const Network::NetworkException& e ) {
fprintf( stderr, "Network exception: %s: %s\n", fprintf( stderr, "Network exception: %s\n",
e.function.c_str(), strerror( e.the_errno ) ); e.what() );
return 1; return 1;
} catch ( const Crypto::CryptoException& e ) { } catch ( const Crypto::CryptoException& e ) {
fprintf( stderr, "Crypto exception: %s\n", fprintf( stderr, "Crypto exception: %s\n",
e.text.c_str() ); e.what() );
return 1; return 1;
} }
} }
@@ -491,11 +491,11 @@ int run_server( const char *desired_ip, const char *desired_port,
try { try {
serve( master, terminal, *network ); serve( master, terminal, *network );
} catch ( const Network::NetworkException& e ) { } catch ( const Network::NetworkException& e ) {
fprintf( stderr, "Network exception: %s: %s\n", fprintf( stderr, "Network exception: %s\n",
e.function.c_str(), strerror( e.the_errno ) ); e.what() );
} catch ( const Crypto::CryptoException& e ) { } catch ( const Crypto::CryptoException& e ) {
fprintf( stderr, "Crypto exception: %s\n", fprintf( stderr, "Crypto exception: %s\n",
e.text.c_str() ); e.what() );
} }
#ifdef HAVE_UTEMPTER #ifdef HAVE_UTEMPTER
@@ -727,13 +727,13 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
network.tick(); network.tick();
} catch ( const Network::NetworkException& e ) { } catch ( const Network::NetworkException& e ) {
fprintf( stderr, "%s: %s\n", e.function.c_str(), strerror( e.the_errno ) ); fprintf( stderr, "%s\n", e.what() );
spin(); spin();
} catch ( const Crypto::CryptoException& e ) { } catch ( const Crypto::CryptoException& e ) {
if ( e.fatal ) { if ( e.fatal ) {
throw; throw;
} else { } else {
fprintf( stderr, "Crypto exception: %s\n", e.text.c_str() ); fprintf( stderr, "Crypto exception: %s\n", e.what() );
} }
} }
} }
+1 -1
View File
@@ -568,7 +568,7 @@ void STMClient::main( void )
throw; throw;
} else { } else {
wchar_t tmp[ 128 ]; wchar_t tmp[ 128 ];
swprintf( tmp, 128, L"Crypto exception: %s", e.text.c_str() ); swprintf( tmp, 128, L"Crypto exception: %s", e.what() );
overlays.get_notification_engine().set_notification_string( wstring( tmp ) ); overlays.get_notification_engine().set_notification_string( wstring( tmp ) );
} }
} }
+3 -2
View File
@@ -40,6 +40,7 @@
#include <vector> #include <vector>
#include <limits.h> #include <limits.h>
#include <exception>
namespace Overlay { namespace Overlay {
using namespace Terminal; using namespace Terminal;
@@ -173,10 +174,10 @@ namespace Overlay {
show_quit_keystroke = s_show_quit_keystroke; show_quit_keystroke = s_show_quit_keystroke;
} }
void set_network_exception( const NetworkException &e ) void set_network_exception( const std::exception &e )
{ {
wchar_t tmp[ 128 ]; wchar_t tmp[ 128 ];
swprintf( tmp, 128, L"%s: %s", e.function.c_str(), strerror( e.the_errno ) ); swprintf( tmp, 128, L"%s", e.what() );
message = tmp; message = tmp;
message_is_network_exception = true; message_is_network_exception = true;
+4 -4
View File
@@ -260,9 +260,9 @@ Connection::Connection( const char *desired_ip, const char *desired_port ) /* se
try { try {
if ( try_bind( desired_ip, desired_port_low, desired_port_high ) ) { return; } if ( try_bind( desired_ip, desired_port_low, desired_port_high ) ) { return; }
} catch ( const NetworkException& e ) { } catch ( const NetworkException& e ) {
fprintf( stderr, "Error binding to IP %s: %s: %s\n", fprintf( stderr, "Error binding to IP %s: %s\n",
desired_ip, desired_ip,
e.function.c_str(), strerror( e.the_errno ) ); e.what() );
} }
} }
@@ -270,8 +270,8 @@ Connection::Connection( const char *desired_ip, const char *desired_port ) /* se
try { try {
if ( try_bind( NULL, desired_port_low, desired_port_high ) ) { return; } if ( try_bind( NULL, desired_port_low, desired_port_high ) ) { return; }
} catch ( const NetworkException& e ) { } catch ( const NetworkException& e ) {
fprintf( stderr, "Error binding to any interface: %s: %s\n", fprintf( stderr, "Error binding to any interface: %s\n",
e.function.c_str(), strerror( e.the_errno ) ); e.what() );
throw; /* this time it's fatal */ throw; /* this time it's fatal */
} }
+11 -3
View File
@@ -41,6 +41,8 @@
#include <math.h> #include <math.h>
#include <vector> #include <vector>
#include <assert.h> #include <assert.h>
#include <exception>
#include <string.h>
#include "crypto.h" #include "crypto.h"
@@ -53,12 +55,18 @@ namespace Network {
uint16_t timestamp16( void ); uint16_t timestamp16( void );
uint16_t timestamp_diff( uint16_t tsnew, uint16_t tsold ); uint16_t timestamp_diff( uint16_t tsnew, uint16_t tsold );
class NetworkException { class NetworkException : public std::exception {
public: public:
string function; string function;
int the_errno; int the_errno;
NetworkException( string s_function, int s_errno ) : function( s_function ), the_errno( s_errno ) {} private:
NetworkException() : function( "<none>" ), the_errno( 0 ) {} string my_what;
public:
NetworkException( string s_function="<none>", int s_errno=0)
: function( s_function ), the_errno( s_errno ),
my_what(function + ": " + strerror(the_errno)) {}
const char *what() const throw () { return my_what.c_str(); }
~NetworkException() throw () {}
}; };
enum Direction { enum Direction {
+1 -1
View File
@@ -144,7 +144,7 @@ int main( int argc, char *argv[] ) {
test_one_session(); test_one_session();
} catch ( const CryptoException& e ) { } catch ( const CryptoException& e ) {
fprintf( stderr, "Crypto exception: %s\r\n", fprintf( stderr, "Crypto exception: %s\r\n",
e.text.c_str() ); e.what() );
fatal_assert( false ); fatal_assert( false );
} }
} }