From: Felix Dörre Date: Tue, 26 Jan 2016 22:58:56 +0000 (+0100) Subject: upd: only throwing exceptions now X-Git-Url: http://www.controwiki.de/?a=commitdiff_plain;h=e0713d1539887566e0ea7d85942c305ec727c9dd;p=cassiopeia.git upd: only throwing exceptions now --- diff --git a/src/apps/client.cpp b/src/apps/client.cpp index 9dea0a8..bfb7fb1 100644 --- a/src/apps/client.cpp +++ b/src/apps/client.cpp @@ -45,8 +45,8 @@ void checkCRLs( std::shared_ptr sign ) { try { std::vector serials; std::pair, std::string> rev = sign->revoke( x.second, serials ); - } catch( const char* c ) { - logger::error( "Exception: ", c ); + } catch( const std::exception &e ) { + logger::error( "Exception: ", e.what() ); } } } @@ -185,18 +185,14 @@ int main( int argc, const char* argv[] ) { } continue; - } catch( const char* c ) { - logger::error( "ERROR: ", c ); - } catch( std::string& c ) { - logger::error( "ERROR: ", c ); + } catch( std::exception& c ) { + logger::error( "ERROR: ", c.what() ); } try { jp->failJob( job ); - } catch( const char* c ) { - logger::error( "ERROR: ", c ); - } catch( std::string& c ) { - logger::error( "ERROR: ", c ); + } catch( std::exception& c ) { + logger::error( "ERROR: ", c.what() ); } } else if( job->task == "revoke" ) { try { @@ -212,10 +208,8 @@ int main( int argc, const char* argv[] ) { jp->writeBackRevocation( job, timeToString( time ) ); jp->finishJob( job ); - } catch( const char* c ) { - logger::error( "Exception: ", c ); - } catch( const std::string& c ) { - logger::error( "Exception: ", c ); + } catch( const std::exception& c ) { + logger::error( "Exception: ", c.what() ); } } else { logger::errorf( "Unknown job type (\"%s\")", job->task ); @@ -225,10 +219,6 @@ int main( int argc, const char* argv[] ) { if( !DAEMON || once ) { return 0; } - } catch( const char* c ) { - logger::error( "Exception: ", c ); - } catch( const std::string& c ) { - logger::error( "Exception: ", c ); } catch ( std::exception &e ){ logger::errorf ( "std::exception in mainloop: %s", e.what() ); } diff --git a/src/apps/signer.cpp b/src/apps/signer.cpp index 2b6798c..530c76c 100644 --- a/src/apps/signer.cpp +++ b/src/apps/signer.cpp @@ -59,10 +59,8 @@ int main( int argc, const char* argv[] ) try { dh->handle(); //} catch( const std::exception &ch ) { //std::cout << "Real exception: " << typeid(ch).name() << ", " << ch.what() << std::endl; - } catch( const std::string& ch ) { - logger::error( "Exception: ", ch ); - } catch( char const* ch ) { - logger::error( "Exception: ", ch ); + } catch( const std::exception& e ) { + logger::error( "Exception: ", e.what() ); } } @@ -71,7 +69,4 @@ int main( int argc, const char* argv[] ) try { } catch( std::exception& e ) { logger::fatalf( "Fatal Error: %s!\n", e.what() ); return -1; -} catch( ... ) { - logger::fatal( "Fatal Error: Unknown Exception!\n" ); - return -1; } diff --git a/src/config.cpp b/src/config.cpp index 4440d09..164e0bb 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -23,7 +23,7 @@ std::shared_ptr> parseConf( std::st if( !config.is_open() ) { logger::notef( "Where is \"%s\"?", path ); - throw "Config missing"; + throw std::runtime_error("Config missing"); } std::string line1; @@ -117,7 +117,7 @@ int parseProfiles() { closedir( dir ); } else { - throw "Directory with CAConfigs not found"; + throw std::runtime_error("Directory with CAConfigs not found"); } profiles.emplace( profileName, prof ); diff --git a/src/crypto/CRL.cpp b/src/crypto/CRL.cpp index 902cb6d..41fbe8b 100644 --- a/src/crypto/CRL.cpp +++ b/src/crypto/CRL.cpp @@ -2,6 +2,7 @@ #include #include +#include CRL::CRL( std::string path ) { std::shared_ptr bio( BIO_new_file( path.c_str(), "r" ), BIO_free ); @@ -17,21 +18,21 @@ std::string CRL::revoke( std::string serial, std::string time ) { logger::note("parsing serial"); if( ! BN_hex2bn( &serBN, serial.c_str() ) ) { - throw "hex2bn malloc fail"; + throw std::runtime_error("hex2bn malloc fail"); } std::shared_ptr serBNP( serBN, BN_free ); std::shared_ptr ser( BN_to_ASN1_INTEGER( serBN, NULL ), ASN1_INTEGER_free ); if( !ser ) { - throw "BN Malloc fail"; + throw std::runtime_error("BN Malloc fail"); } logger::note("building current time"); std::shared_ptr tmptm( ASN1_TIME_new(), ASN1_TIME_free ); if( !tmptm ) { - throw "ASN1-Time Malloc fail"; + throw std::runtime_error("ASN1-Time Malloc fail"); } X509_gmtime_adj( tmptm.get(), 0 ); @@ -63,21 +64,21 @@ void CRL::sign( std::shared_ptr ca ) { std::shared_ptr tmptm( ASN1_TIME_new(), ASN1_TIME_free ); if( !tmptm ) { - throw "ASN1-Time Malloc fail"; + throw std::runtime_error("ASN1-Time Malloc fail"); } X509_gmtime_adj( tmptm.get(), 0 ); logger::note("setting issuer"); if( !X509_CRL_set_issuer_name( crl.get(), X509_get_subject_name( ca->ca.get() ) ) ) { - throw "Setting issuer failed"; + throw std::runtime_error("Setting issuer failed"); } logger::note("setting update"); X509_CRL_set_lastUpdate( crl.get(), tmptm.get() ); if( !X509_time_adj_ex( tmptm.get(), 1, 10, NULL ) ) { - throw "Updating time failed"; + throw std::runtime_error("Updating time failed"); } logger::note("setting next update"); diff --git a/src/crypto/X509.cpp b/src/crypto/X509.cpp index caa2a9d..7a4314d 100644 --- a/src/crypto/X509.cpp +++ b/src/crypto/X509.cpp @@ -11,7 +11,7 @@ X509Req::X509Req( X509_REQ* csr ) : req( csr, X509_REQ_free ) { EVP_PKEY* pkt = X509_REQ_get_pubkey( req.get() ); if( !pkt ) { - throw "Error extracting public key"; + throw std::runtime_error("Error extracting public key"); } pk = std::shared_ptr( pkt, EVP_PKEY_free ); @@ -19,21 +19,21 @@ X509Req::X509Req( X509_REQ* csr ) : req( csr, X509_REQ_free ) { X509Req::X509Req( std::string spkac ) { if( spkac.compare( 0, 6, "SPKAC=" ) != 0 ) { - throw "Error: not a SPKAC"; + throw std::runtime_error("Error: not a SPKAC"); } spkac = spkac.substr( 6 ); NETSCAPE_SPKI* spki_p = NETSCAPE_SPKI_b64_decode( spkac.c_str(), spkac.size() ); if( !spki_p ) { - throw "Error: decode failed"; + throw std::runtime_error("Error: decode failed"); } spki = std::shared_ptr( spki_p, NETSCAPE_SPKI_free ); EVP_PKEY* pkt_p = NETSCAPE_SPKI_get_pubkey( spki.get() ); if( !pkt_p ) { - throw "Error: reading SPKAC Pubkey failed"; + throw std::runtime_error("Error: reading SPKAC Pubkey failed"); } pk = std::shared_ptr( pkt_p, EVP_PKEY_free ); @@ -56,7 +56,7 @@ std::shared_ptr X509Req::parseCSR( std::string content ) { X509_REQ* req = PEM_read_bio_X509_REQ( in.get(), NULL, NULL, NULL ); if( !req ) { - throw "Error parsing CSR"; + throw std::runtime_error("Error parsing CSR"); } return std::shared_ptr( new X509Req( req )); // TODO ask @@ -94,19 +94,19 @@ X509Cert::X509Cert() { X509* c = X509_new(); if( !c ) { - throw "malloc failed"; + throw std::runtime_error("malloc failed"); } target = std::shared_ptr( c, X509_free ); if( !X509_set_version( c, 2 ) ) { - throw "Setting X509-version to 3 failed"; + throw std::runtime_error("Setting X509-version to 3 failed"); } X509_NAME* subjectP = X509_NAME_new(); if( !subjectP ) { - throw "malloc failure in construct."; + throw std::runtime_error("malloc failure in construct."); } subject = std::shared_ptr( subjectP, X509_NAME_free ); @@ -114,13 +114,13 @@ X509Cert::X509Cert() { void X509Cert::addRDN( int nid, std::string data ) { if( ! X509_NAME_add_entry_by_NID( subject.get(), nid, MBSTRING_UTF8, ( unsigned char* )const_cast( data.data() ), data.size(), -1, 0 ) ) { - throw "malloc failure in RDN"; + throw std::runtime_error("malloc failure in RDN"); } } void X509Cert::setIssuerNameFrom( std::shared_ptr caCert ) { if( !X509_set_issuer_name( target.get(), X509_get_subject_name( caCert.get() ) ) ) { - throw "Error setting Issuer name"; + throw std::runtime_error("Error setting Issuer name"); } } @@ -128,7 +128,7 @@ void X509Cert::setPubkeyFrom( std::shared_ptr req ) { std::shared_ptr pktmp = req->getPkey(); if( !X509_set_pubkey( target.get(), pktmp.get() ) ) { - throw "Setting public key failed."; + throw std::runtime_error("Setting public key failed."); } } @@ -171,7 +171,7 @@ static X509_EXTENSION* do_ext_i2d( int ext_nid, int crit, ASN1_VALUE* ext_struc return ext; merr: - throw "memerr"; + throw std::runtime_error("memerr"); } void X509Cert::setExtensions( std::shared_ptr caCert, std::vector>& sans, Profile& prof, std::string crlURL, std::string crtURL ) { @@ -200,7 +200,7 @@ void X509Cert::setExtensions( std::shared_ptr caCert, std::vectortype = name->type == "DNS" ? GEN_DNS : name->type == "email" ? GEN_EMAIL : 0; // GEN_EMAIL; @@ -209,7 +209,7 @@ void X509Cert::setExtensions( std::shared_ptr caCert, std::vectord.ia5 = M_ASN1_IA5STRING_new() ) || !ASN1_STRING_set( gen->d.ia5, name->content.data(), name->content.size() ) ) { GENERAL_NAME_free( gen ); - throw "initing iasting5 failed"; + throw std::runtime_error("initing iasting5 failed"); } sk_GENERAL_NAME_push( gens.get(), gen ); @@ -223,7 +223,7 @@ void X509Cert::setExtensions( std::shared_ptr caCert, std::vector X509Cert::sign( std::shared_ptr caKey, std::string signAlg ) { if( !X509_set_subject_name( target.get(), subject.get() ) ) { - throw "error setting subject"; + throw std::runtime_error("error setting subject"); } const EVP_MD* md; @@ -237,11 +237,11 @@ std::shared_ptr X509Cert::sign( std::shared_ptr caK } else if( signAlg == "sha1" ) { md = EVP_sha1(); } else { - throw "Unknown md-type"; + throw std::runtime_error("Unknown md-type"); } if( !X509_sign( target.get(), caKey.get(), md ) ) { - throw "Signing failed."; + throw std::runtime_error("Signing failed."); } //X509_print_fp( stdout, target.get() ); @@ -249,7 +249,7 @@ std::shared_ptr X509Cert::sign( std::shared_ptr caK std::shared_ptr mem = std::shared_ptr( BIO_new( BIO_s_mem() ), BIO_free ); if( !mem ) { - throw "Failed to allocate memory for the signed certificate."; + throw std::runtime_error("Failed to allocate memory for the signed certificate."); } PEM_write_bio_X509( mem.get(), target.get() ); @@ -263,7 +263,7 @@ std::shared_ptr X509Cert::sign( std::shared_ptr caK std::shared_ptr ser( ASN1_INTEGER_to_BN( target->cert_info->serialNumber, NULL ), BN_free ); if( !ser ) { - throw "Failed to retrieve certificate serial of signed certificate."; + throw std::runtime_error("Failed to retrieve certificate serial of signed certificate."); } std::shared_ptr serStr( diff --git a/src/crypto/remoteSigner.cpp b/src/crypto/remoteSigner.cpp index a890711..1c32ba0 100644 --- a/src/crypto/remoteSigner.cpp +++ b/src/crypto/remoteSigner.cpp @@ -92,8 +92,8 @@ std::shared_ptr RemoteSigner::sign( std::shared_ptr(); } } @@ -107,7 +107,7 @@ std::shared_ptr RemoteSigner::sign( std::shared_ptr RemoteSigner::sign( std::shared_ptr pem( PEM_read_bio_X509( bios.get(), NULL, 0, NULL ) ); if( !pem ) { - throw "Pem was not readable"; + throw std::runtime_error("Pem was not readable"); } std::shared_ptr ser( ASN1_INTEGER_to_BN( pem->cert_info->serialNumber, NULL ), BN_free ); @@ -169,7 +169,7 @@ std::pair, std::string> RemoteSigner::revoke( std::shared_p std::string date; if( static_cast( head.command ) != RecordHeader::SignerResult::REVOKED ) { - throw "Protocol violation"; + throw std::runtime_error("Protocol violation"); } const unsigned char* buffer2 = reinterpret_cast( payload.data() ); @@ -196,7 +196,7 @@ std::pair, std::string> RemoteSigner::revoke( std::shared_p payload = parseCommandChunked( head, conn ); if( static_cast( head.command ) != RecordHeader::SignerResult::FULL_CRL ) { - throw "Protocol violation"; + throw std::runtime_error("Protocol violation"); } std::string name_bak = ca->path + std::string( "/ca.crl.bak" ); diff --git a/src/crypto/simpleOpensslSigner.cpp b/src/crypto/simpleOpensslSigner.cpp index f0d415e..cad772c 100644 --- a/src/crypto/simpleOpensslSigner.cpp +++ b/src/crypto/simpleOpensslSigner.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -36,11 +37,11 @@ std::pair, std::string> SimpleOpensslSigner::nextSerial( bn = BN_new(); if( !bn || !BN_hex2bn( &bn, "1" )) { - throw "Initing serial failed"; + throw std::runtime_error("Initing serial failed"); } } else { if( !BN_hex2bn( &bn, res.c_str() ) ) { - throw "Parsing serial failed."; + throw std::runtime_error("Parsing serial failed."); } } @@ -56,7 +57,7 @@ std::pair, std::string> SimpleOpensslSigner::nextSerial( data.get()[len + 3] = profile & 0xFF; // profile id if( !RAND_bytes( data.get() + len + 4, 16 ) || !BN_add_word( serial.get(), 1 ) ) { - throw "Big number math failed while fetching random data for serial number."; + throw std::runtime_error("Big number math failed while fetching random data for serial number."); } std::shared_ptr serStr = std::shared_ptr( @@ -83,7 +84,7 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptrcsr_content ); } else { logger::errorf( "ERROR: Unknown type (\"%s\") of certification in request.", cert->csr_type ); - throw "Error, unknown REQ rype " + ( cert->csr_type ); //! \fixme: Pointer instead of string, please use proper exception classes + throw std::runtime_error("Error, unknown REQ rype " + cert->csr_type ); //! \fixme: Pointer instead of string, please use proper exception classe)s } int i = req->verify(); if( i < 0 ) { - throw "Request contains a Signature with problems ... "; + throw std::runtime_error("Request contains a Signature with problems ... "); } else if( i == 0 ) { - throw "Request contains a Signature that does not match ..."; + throw std::runtime_error("Request contains a Signature that does not match ..."); } else { logger::note( "FINE: Request contains valid self-signature." ); } @@ -142,7 +143,7 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptrvalue ); } else { logger::error( "ERROR: Trying to add illegal RDN/AVA type: ", a->name ); - throw "Unhandled/Illegal AVA type"; + throw std::runtime_error("Unhandled/Illegal AVA type"); } } @@ -244,7 +245,7 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptr generateSSLContext( bool server ) { } ); if( !SSL_CTX_set_cipher_list( ctx.get(), "HIGH:+CAMELLIA256:!eNull:!aNULL:!ADH:!MD5:-RSA+AES+SHA1:!RC4:!DES:!3DES:!SEED:!EXP:!AES128:!CAMELLIA128" ) ) { - throw "Cannot set cipher list. Your source is broken."; + throw std::runtime_error("Cannot set cipher list. Your source is broken."); } SSL_CTX_set_verify( ctx.get(), SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_callback ); @@ -105,7 +105,7 @@ std::shared_ptr generateSSLContext( bool server ) { SSL_CTX_use_PrivateKey_file( ctx.get(), server ? "keys/signer_server.key" : "keys/signer_client.key", SSL_FILETYPE_PEM ); if( 1 != SSL_CTX_load_verify_locations( ctx.get(), "keys/ca.crt", 0 ) ) { - throw "Cannot load CA store for certificate validation."; + throw std::runtime_error("Cannot load CA store for certificate validation."); } if( server ) { @@ -131,7 +131,7 @@ std::shared_ptr generateSSLContext( bool server ) { cb.cb.cb_2 = gencb; if( !DH_generate_parameters_ex( dh_param.get(), 2048, 5, &cb ) ) { - throw "DH generation failed"; + throw std::runtime_error("DH generation failed"); } std::cout << std::endl; @@ -144,7 +144,7 @@ std::shared_ptr generateSSLContext( bool server ) { } if( !SSL_CTX_set_tmp_dh( ctx.get(), dh_param.get() ) ) { - throw "Cannot set tmp dh."; + throw std::runtime_error("Cannot set tmp dh."); } } @@ -155,7 +155,7 @@ void setupSerial( std::shared_ptr f ) { struct termios attr; if( tcgetattr( fileno( f.get() ), &attr ) ) { - throw "failed to get attrs"; + throw std::runtime_error("failed to get attrs"); } attr.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON ); @@ -168,7 +168,7 @@ void setupSerial( std::shared_ptr f ) { cfsetospeed( &attr, B115200 ); if( tcsetattr( fileno( f.get() ), TCSANOW, &attr ) ) { - throw "failed to get attrs"; + throw std::runtime_error("failed to get attrs"); } } @@ -214,7 +214,7 @@ std::string timeToString( std::shared_ptr time ) { logger::notef("openssl formatted me a date: %s", strdate); if( strdate[strdate.size() - 1] != 'Z' ) { - throw "Got invalid date?"; + throw std::runtime_error("Got invalid date?"); } return strdate.substr( 0, strdate.size() - 1 ); diff --git a/src/db/mysql.cpp b/src/db/mysql.cpp index ecb6fe0..e8f1d4d 100644 --- a/src/db/mysql.cpp +++ b/src/db/mysql.cpp @@ -32,7 +32,7 @@ std::shared_ptr MySQLJobProvider::lib_ref( MySQLJobProvider::MySQLJobProvider( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) { if( !lib_ref || *lib_ref ) { - throw "MySQL library not initialized!"; + throw std::runtime_error("MySQL library not initialized!"); } connect( server, user, password, database ); @@ -86,7 +86,7 @@ std::pair< int, std::shared_ptr > MySQLJobProvider::query( const std: int err = mysql_real_query( this->conn.get(), query.c_str(), query.size() ); if( err ) { - throw std::string( "MySQL error: " ) + mysql_error( this->conn.get() ); + throw std::runtime_error(std::string( "MySQL error: " ) + mysql_error( this->conn.get() )); } auto c = conn; @@ -143,7 +143,7 @@ std::shared_ptr MySQLJobProvider::fetchJob() { std::string MySQLJobProvider::escape_string( const std::string& target ) { if( !conn ) { - throw "Not connected!"; + throw std::runtime_error("Not connected!"); } std::string result; @@ -159,25 +159,25 @@ std::string MySQLJobProvider::escape_string( const std::string& target ) { void MySQLJobProvider::finishJob( std::shared_ptr job ) { if( !conn ) { - throw "Not connected!"; + throw std::runtime_error("Not connected!"); } std::string q = "UPDATE jobs SET state='done' WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1"; if( query( q ).first ) { - throw "No database entry found."; + throw std::runtime_error("No database entry found."); } } void MySQLJobProvider::failJob( std::shared_ptr job ) { if( !conn ) { - throw "Not connected!"; + throw std::runtime_error("Not connected!"); } std::string q = "UPDATE jobs SET warning = warning + 1 WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1"; if( query( q ).first ) { - throw "No database entry found."; + throw std::runtime_error("No database entry found."); } } @@ -273,7 +273,7 @@ std::shared_ptr MySQLJobProvider::fetchTBSCert( std::shared_ptr< void MySQLJobProvider::writeBack( std::shared_ptr job, std::shared_ptr res ) { if( !conn ) { - throw "Error while writing back"; + throw std::runtime_error("Error while writing back"); } std::string id = "SELECT id FROM cacerts WHERE keyname='" + this->escape_string( res->ca_name ) + "'"; @@ -283,7 +283,7 @@ void MySQLJobProvider::writeBack( std::shared_ptr job, std::shared_ptr job, std::shared_ptr job, std::shared_ptr MySQLJobProvider::getRevocationInfo( std::sh std::tie( err, resu ) = query( q ); if( err ) { - throw "Error while looking ca cert id"; + throw std::runtime_error("Error while looking ca cert id"); } MYSQL_ROW row = mysql_fetch_row( resu.get() ); unsigned long* l = mysql_fetch_lengths( resu.get() ); if( !row || !l ) { - throw "Error while inserting new ca cert"; + throw std::runtime_error("Error while inserting new ca cert"); } return std::pair( std::string( row[0], row[0] + l[0] ), std::string( row[1], row[1] + l[1] ) ); @@ -327,6 +327,6 @@ std::pair MySQLJobProvider::getRevocationInfo( std::sh void MySQLJobProvider::writeBackRevocation( std::shared_ptr job, std::string date ) { if( query( "UPDATE certs SET revoked = '" + this->escape_string( date ) + "' WHERE id = '" + this->escape_string( job->target ) + "'" ).first ) { - throw "Error while writing back revocation"; + throw std::runtime_error("Error while writing back revocation"); } } diff --git a/src/db/psql.cpp b/src/db/psql.cpp index c2a282e..7187b9e 100644 --- a/src/db/psql.cpp +++ b/src/db/psql.cpp @@ -5,6 +5,7 @@ #include #include +#include PostgresJobProvider::PostgresJobProvider( const std::string& server, const std::string& user, const std::string& password, const std::string& database ): c("dbname="+database+" host="+server+" user="+user+" password=" + password + " client_encoding=UTF-8 application_name=cassiopeia-client"){ @@ -43,7 +44,7 @@ void PostgresJobProvider::finishJob( std::shared_ptr job ) { pqxx::result r = txn.exec(q); if( r.affected_rows() != 1 ) { - throw "No database entry found."; + throw std::runtime_error("No database entry found."); } txn.commit(); } @@ -55,7 +56,7 @@ void PostgresJobProvider::failJob( std::shared_ptr job ) { pqxx::result r = txn.exec(q); if( r.affected_rows() != 1 ) { - throw "No database entry found."; + throw std::runtime_error("No database entry found."); } txn.commit(); } @@ -67,7 +68,7 @@ std::shared_ptr PostgresJobProvider::fetchTBSCert( std::shared_p pqxx::result r = txn.exec(q); if( r.size() != 1 ) { - throw "Error, no or multiple certs found"; + throw std::runtime_error("Error, no or multiple certs found"); } auto ro = r[0]; @@ -124,7 +125,7 @@ void PostgresJobProvider::writeBack( std::shared_ptr job, std::shared_ptr(); } @@ -135,7 +136,7 @@ void PostgresJobProvider::writeBack( std::shared_ptr job, std::shared_ptr PostgresJobProvider::getRevocationInfo( std: pqxx::result r = txn.exec( q ); if( r.size() != 1) { - throw "Only one row expected but multiple found."; + throw std::runtime_error("Only one row expected but multiple found."); } @@ -159,7 +160,7 @@ void PostgresJobProvider::writeBackRevocation( std::shared_ptr job, std::st logger::errorf( "executing" ); pqxx::result r = txn.exec( "UPDATE certs SET revoked = " + txn.quote( pgTime( date ) ) + " WHERE id = " + txn.quote( job->target ) ); if( r.affected_rows() != 1 ){ - throw "Only one row should be updated."; + throw std::runtime_error("Only one row should be updated."); } logger::errorf( "committing" ); txn.commit(); diff --git a/src/io/opensslBIO.cpp b/src/io/opensslBIO.cpp index 056c291..02fd472 100644 --- a/src/io/opensslBIO.cpp +++ b/src/io/opensslBIO.cpp @@ -1,4 +1,5 @@ #include "opensslBIO.h" +#include OpensslBIOWrapper::OpensslBIOWrapper( std::shared_ptr _b ) : b( _b ), buffer( 2*0xFFFF+20, 0 ), pos(0) { } @@ -29,33 +30,31 @@ int OpensslBIOWrapper::gets( char* str, int size ) { const char* OpensslBIOWrapper::getName() { return "OpenSSLWrapper"; } -#include std::string OpensslBIOWrapper::readLine(){ int target = 0; while(1){ - logger::warn("doing data"); + logger::debug("doing data"); while(target < pos){ if(buffer[target] == '\n'){ target++; std::string res(buffer.data(), 0, target); std::copy(buffer.data() + target, buffer.data() + pos, buffer.data() ); pos -= target; - logger::warn("emit"); + logger::debug("emit"); return res; } target++; } std::stringstream ss; ss << "target: " << target << ", pos:" << pos; - logger::warn(ss.str()); + logger::debug(ss.str()); int dlen = read(buffer.data() + pos, buffer.size() - pos); if ( dlen <= 0 ){ - logger::warn(" error! "); - throw EOFException(); + throw eof_exception(); } std::stringstream ss2; ss2 << "done: " << dlen; - logger::warn(ss2.str()); + logger::debug(ss2.str()); pos += dlen; } } diff --git a/src/io/opensslBIO.h b/src/io/opensslBIO.h index 092fa9c..994b7bc 100644 --- a/src/io/opensslBIO.h +++ b/src/io/opensslBIO.h @@ -26,5 +26,5 @@ public: std::string readLine(); }; -class EOFException : public std::exception{ +class eof_exception : public std::exception{ }; diff --git a/src/io/record.cpp b/src/io/record.cpp index c3a2270..4cc5b89 100644 --- a/src/io/record.cpp +++ b/src/io/record.cpp @@ -96,10 +96,10 @@ std::string parseCommand( RecordHeader& head, const std::string& input) { std::stringstream ss; ss << "Expected: " << expectedTotalLength << ", Got: " << input.size(); logger::error( ss.str() ); - throw "Error, invalid length"; + throw std::length_error("Error, invalid length"); } if( checksum != -1 || error || dlen < RECORD_HEADER_SIZE ) { - throw "Error, invalid checksum"; + throw std::runtime_error("Error, invalid checksum"); } data.pop_back(); diff --git a/src/io/record.h b/src/io/record.h index c06e6ad..48dd8af 100644 --- a/src/io/record.h +++ b/src/io/record.h @@ -4,7 +4,7 @@ #include #include - +#include #include "bios.h" #include "io/opensslBIO.h" @@ -90,7 +90,7 @@ public: void unpackFromString( const std::string& str ) { if( str.size() != RECORD_HEADER_SIZE ) { - throw "Invalid string length"; + throw std::runtime_error("Invalid string length"); } auto it = str.cbegin(); diff --git a/src/io/recordHandler.cpp b/src/io/recordHandler.cpp index 551b2c4..f8c2185 100644 --- a/src/io/recordHandler.cpp +++ b/src/io/recordHandler.cpp @@ -75,8 +75,8 @@ public: RecordHeader head; std::string all = parseCommandChunked( head, io ); execute( static_cast( head.command ), all ); - } catch( const char* msg ) { - logger::error( "ERROR: ", msg ); + } catch( const std::exception& msg ) { + logger::error( "ERROR: ", msg.what() ); parent->reset(); return; } @@ -207,7 +207,7 @@ public: break; default: - throw "Unimplemented"; + throw std::runtime_error("Unimplemented"); } } }; @@ -228,7 +228,7 @@ void DefaultRecordHandler::handle() { } try { currentSession->work(); - } catch( EOFException e ){ + } catch( eof_exception e ){ reset(); } } diff --git a/src/io/slipBio.cpp b/src/io/slipBio.cpp index a9100b3..29bc41e 100644 --- a/src/io/slipBio.cpp +++ b/src/io/slipBio.cpp @@ -1,7 +1,7 @@ #include "slipBio.h" #include - +#include #include #include "log/logger.hpp" @@ -111,7 +111,7 @@ int SlipBIO::write( const char* buf, int num ) { #endif if( dlen < 0 ) { - throw "Error, target write failed"; + throw std::runtime_error("Error, target write failed"); } else if( dlen == 0 ) { // sleep logger::note( "waiting for write ability" ); diff --git a/src/util.cpp b/src/util.cpp index 40fcf25..3197b68 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -190,7 +190,7 @@ std::unique_ptr openLogfile( const std::string &name ) { auto res = std::make_unique( tname ); if( ! res->good() ) { - throw std::string( "Failed to open file for logging: " ) + name; + throw std::runtime_error( std::string("Failed to open file for logging: " ) + name ); } return res;