Skip to content

Commit

Permalink
Cleanup error message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Dec 9, 2024
1 parent e3fb69e commit ab5cabc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion include/tao/pq/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace tao::pq

[[nodiscard]] static auto create( const std::string& connection_info, std::function< poll::callback > poll_cb = internal::poll ) -> std::shared_ptr< connection >;

[[nodiscard]] auto error_message() const -> std::string;
[[nodiscard]] auto error_message() const -> const char*;

[[nodiscard]] auto poll_callback() const noexcept -> const std::function< poll::callback >&;
void set_poll_callback( std::function< poll::callback > poll_cb ) noexcept;
Expand Down
27 changes: 13 additions & 14 deletions src/lib/pq/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ namespace tao::pq
{
[[nodiscard]] constexpr auto is_identifier( const std::string_view value ) noexcept -> bool
{
return !value.empty() && ( value.find_first_not_of( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" ) == std::string_view::npos ) && ( std::isdigit( static_cast< unsigned char >( value[ 0 ] ) ) == 0 );
return !value.empty() && ( std::isdigit( static_cast< unsigned char >( value[ 0 ] ) ) == 0 ) && ( value.find_first_not_of( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" ) == std::string_view::npos );
}

} // namespace
Expand All @@ -188,7 +188,7 @@ namespace tao::pq
{
const std::unique_ptr< char, decltype( &PQfreemem ) > buffer( PQescapeIdentifier( m_pgconn.get(), identifier.data(), identifier.size() ), &PQfreemem );
if( !buffer ) {
throw std::invalid_argument( PQerrorMessage( m_pgconn.get() ) ); // LCOV_EXCL_LINE
throw std::invalid_argument( error_message() ); // LCOV_EXCL_LINE
}
return buffer.get();
}
Expand Down Expand Up @@ -228,7 +228,7 @@ namespace tao::pq
PQsendQueryPrepared( m_pgconn.get(), statement, n_params, values, lengths, formats, 0 ) :
PQsendQueryParams( m_pgconn.get(), statement, n_params, types, values, lengths, formats, 0 );
if( result == 0 ) {
throw pq::connection_error( PQerrorMessage( m_pgconn.get() ) ); // LCOV_EXCL_LINE
throw pq::connection_error( error_message() ); // LCOV_EXCL_LINE
}
}

Expand Down Expand Up @@ -294,8 +294,7 @@ namespace tao::pq
break;

default:
throw std::runtime_error( "PQflush() failed: " + error_message() );
// LCOV_EXCL_STOP
throw std::runtime_error( std::format( "PQflush() failed: {}", error_message() ) ); // LCOV_EXCL_STOP
}
}
connection::wait( wait_for_write, end );
Expand Down Expand Up @@ -323,7 +322,7 @@ namespace tao::pq

// LCOV_EXCL_START
case -2:
throw std::runtime_error( "PQgetCopyData() failed: " + error_message() );
throw std::runtime_error( std::format( "PQgetCopyData() failed: {}", error_message() ) );

default:
TAO_PQ_INTERNAL_UNREACHABLE;
Expand Down Expand Up @@ -351,7 +350,7 @@ namespace tao::pq
break;

case -1:
throw std::runtime_error( "PQputCopyData() failed: " + error_message() );
throw std::runtime_error( std::format( "PQputCopyData() failed: {}", error_message() ) );

default:
TAO_PQ_INTERNAL_UNREACHABLE;
Expand All @@ -374,7 +373,7 @@ namespace tao::pq
break;

case -1:
throw std::runtime_error( "PQputCopyEnd() failed: " + connection::error_message() );
throw std::runtime_error( std::format( "PQputCopyEnd() failed: {}", connection::error_message() ) );

default:
TAO_PQ_INTERNAL_UNREACHABLE;
Expand Down Expand Up @@ -405,11 +404,11 @@ namespace tao::pq
if( !is_open() ) {
// note that we can not access the sqlstate after PQconnectdb(),
// see https://stackoverflow.com/q/23349086/2073257
throw pq::connection_error( PQerrorMessage( m_pgconn.get() ) );
throw pq::connection_error( error_message() );
}

if( PQsetnonblocking( m_pgconn.get(), 1 ) != 0 ) {
throw pq::connection_error( PQerrorMessage( m_pgconn.get() ) ); // LCOV_EXCL_LINE
throw pq::connection_error( error_message() ); // LCOV_EXCL_LINE
}
}

Expand All @@ -418,7 +417,7 @@ namespace tao::pq
return std::make_shared< connection >( private_key(), connection_info, std::move( poll_cb ) );
}

auto connection::error_message() const -> std::string
auto connection::error_message() const -> const char*
{
return PQerrorMessage( m_pgconn.get() );
}
Expand Down Expand Up @@ -507,7 +506,7 @@ namespace tao::pq
connection::check_prepared_name( name );
const auto end = timeout_end();
if( PQsendPrepare( m_pgconn.get(), name.c_str(), statement, 0, nullptr ) == 0 ) {
throw pq::connection_error( PQerrorMessage( m_pgconn.get() ) ); // LCOV_EXCL_LINE
throw pq::connection_error( error_message() ); // LCOV_EXCL_LINE
}
auto result = connection::get_result( end );
switch( PQresultStatus( result.get() ) ) {
Expand Down Expand Up @@ -582,7 +581,7 @@ namespace tao::pq
void connection::get_notifications()
{
if( PQconsumeInput( m_pgconn.get() ) == 0 ) {
throw pq::connection_error( PQerrorMessage( m_pgconn.get() ) );
throw pq::connection_error( error_message() );
}
handle_notifications();
}
Expand Down Expand Up @@ -610,7 +609,7 @@ namespace tao::pq
{
const std::unique_ptr< char, decltype( &PQfreemem ) > buffer( PQencryptPasswordConn( m_pgconn.get(), passwd, user, algorithm ), &PQfreemem );
if( !buffer ) {
throw std::invalid_argument( PQerrorMessage( m_pgconn.get() ) ); // LCOV_EXCL_LINE
throw std::invalid_argument( error_message() ); // LCOV_EXCL_LINE
}
return buffer.get();
}
Expand Down
23 changes: 12 additions & 11 deletions src/lib/pq/large_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <format>
#include <ios>
#include <memory>
#include <stdexcept>
Expand Down Expand Up @@ -38,31 +39,31 @@ namespace tao::pq
{
const oid id = static_cast< oid >( lo_create( transaction->connection()->underlying_raw_ptr(), static_cast< Oid >( desired_id ) ) );
if( id == oid::invalid ) {
throw std::runtime_error( "tao::pq::large_object::create() failed: " + transaction->connection()->error_message() );
throw std::runtime_error( std::format( "tao::pq::large_object::create() failed: {}", transaction->connection()->error_message() ) );
}
return id;
}

void large_object::remove( const std::shared_ptr< transaction >& transaction, const oid id )
{
if( lo_unlink( transaction->connection()->underlying_raw_ptr(), static_cast< Oid >( id ) ) == -1 ) {
throw std::runtime_error( "tao::pq::large_object::remove() failed: " + transaction->connection()->error_message() );
throw std::runtime_error( std::format( "tao::pq::large_object::remove() failed: {}", transaction->connection()->error_message() ) );
}
}

auto large_object::import_file( const std::shared_ptr< transaction >& transaction, const char* filename, const oid desired_id ) -> oid
{
const oid id = static_cast< oid >( lo_import_with_oid( transaction->connection()->underlying_raw_ptr(), filename, static_cast< Oid >( desired_id ) ) );
if( id == oid::invalid ) {
throw std::runtime_error( "tao::pq::large_object::import_file() failed: " + transaction->connection()->error_message() );
throw std::runtime_error( std::format( "tao::pq::large_object::import_file() failed: {}", transaction->connection()->error_message() ) );
}
return id;
}

void large_object::export_file( const std::shared_ptr< transaction >& transaction, const oid id, const char* filename )
{
if( lo_export( transaction->connection()->underlying_raw_ptr(), static_cast< Oid >( id ), filename ) == -1 ) {
throw std::runtime_error( "tao::pq::large_object::export_file() failed: " + transaction->connection()->error_message() );
throw std::runtime_error( std::format( "tao::pq::large_object::export_file() failed: {}", transaction->connection()->error_message() ) );
}
}

Expand All @@ -71,7 +72,7 @@ namespace tao::pq
m_fd( lo_open( transaction->connection()->underlying_raw_ptr(), static_cast< Oid >( id ), to_mode( m ) ) )
{
if( m_fd == -1 ) {
throw std::runtime_error( "tao::pq::large_object::open() failed: " + transaction->connection()->error_message() );
throw std::runtime_error( std::format( "tao::pq::large_object::open() failed: {}", transaction->connection()->error_message() ) );
}
}

Expand Down Expand Up @@ -104,7 +105,7 @@ namespace tao::pq
{
if( m_transaction ) {
if( lo_close( m_transaction->connection()->underlying_raw_ptr(), m_fd ) == -1 ) {
throw std::runtime_error( "tao::pq::large_object::close() failed: " + m_transaction->connection()->error_message() );
throw std::runtime_error( std::format( "tao::pq::large_object::close() failed: {}", m_transaction->connection()->error_message() ) );
}
m_transaction.reset();
}
Expand All @@ -115,7 +116,7 @@ namespace tao::pq
assert( m_transaction );
const auto result = lo_read( m_transaction->connection()->underlying_raw_ptr(), m_fd, data, size );
if( result == -1 ) {
throw std::runtime_error( "tao::pq::large_object::read() failed: " + m_transaction->connection()->error_message() );
throw std::runtime_error( std::format( "tao::pq::large_object::read() failed: {}", m_transaction->connection()->error_message() ) );
}
return result;
}
Expand All @@ -124,15 +125,15 @@ namespace tao::pq
{
assert( m_transaction );
if( lo_write( m_transaction->connection()->underlying_raw_ptr(), m_fd, data, size ) == -1 ) {
throw std::runtime_error( "tao::pq::large_object::write() failed: " + m_transaction->connection()->error_message() );
throw std::runtime_error( std::format( "tao::pq::large_object::write() failed: {}", m_transaction->connection()->error_message() ) );
}
}

void large_object::resize( const std::int64_t size )
{
assert( m_transaction );
if( lo_truncate64( m_transaction->connection()->underlying_raw_ptr(), m_fd, size ) == -1 ) {
throw std::runtime_error( "tao::pq::large_object::resize() failed: " + m_transaction->connection()->error_message() );
throw std::runtime_error( std::format( "tao::pq::large_object::resize() failed: {}", m_transaction->connection()->error_message() ) );
}
}

Expand All @@ -145,7 +146,7 @@ namespace tao::pq
assert( m_transaction );
const auto result = lo_lseek64( m_transaction->connection()->underlying_raw_ptr(), m_fd, offset, whence );
if( result == -1 ) {
throw std::runtime_error( "tao::pq::large_object::seek() failed: " + m_transaction->connection()->error_message() );
throw std::runtime_error( std::format( "tao::pq::large_object::seek() failed: {}", m_transaction->connection()->error_message() ) );
}
return result;
}
Expand All @@ -155,7 +156,7 @@ namespace tao::pq
assert( m_transaction );
const auto pos = lo_tell64( m_transaction->connection()->underlying_raw_ptr(), m_fd );
if( pos == -1 ) {
throw std::runtime_error( "tao::pq::large_object::tell() failed: " + m_transaction->connection()->error_message() );
throw std::runtime_error( std::format( "tao::pq::large_object::tell() failed: {}", m_transaction->connection()->error_message() ) );
}
return pos;
}
Expand Down

0 comments on commit ab5cabc

Please sign in to comment.