Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve interoperability with std::system_error #131

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions include/boost/system/system_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,22 @@
#include <boost/system/detail/error_code.hpp>
#include <string>
#include <stdexcept>
#include <system_error>
#include <cassert>

namespace boost
{
namespace system
{

class BOOST_SYMBOL_VISIBLE system_error: public std::runtime_error
class BOOST_SYMBOL_VISIBLE system_error: public std::system_error
{
private:

error_code code_;

public:

explicit system_error( error_code const & ec ):
std::runtime_error( ec.what() ), code_( ec ) {}

system_error( error_code const & ec, std::string const & prefix ):
std::runtime_error( prefix + ": " + ec.what() ), code_( ec ) {}

system_error( error_code const & ec, char const * prefix ):
std::runtime_error( std::string( prefix ) + ": " + ec.what() ), code_( ec ) {}

system_error( int ev, error_category const & ecat ):
std::runtime_error( error_code( ev, ecat ).what() ), code_( ev, ecat ) {}

system_error( int ev, error_category const & ecat, std::string const & prefix ):
std::runtime_error( prefix + ": " + error_code( ev, ecat ).what() ), code_( ev, ecat ) {}

system_error( int ev, error_category const & ecat, char const * prefix ):
std::runtime_error( std::string( prefix ) + ": " + error_code( ev, ecat ).what() ), code_( ev, ecat ) {}
using std::system_error::system_error;

error_code code() const noexcept
{
return code_;
return std::system_error::code();
}
};

Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ run std_interop_test10.cpp ;
run ec_location_test2.cpp ;
run ec_what_test.cpp ;
run system_error_test3.cpp ;
run system_error_test4.cpp ;

run std_interop_test11.cpp ;

Expand Down
21 changes: 21 additions & 0 deletions test/system_error_test4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2021, 2022 Peter Dimov
// Distributed under the Boost Software License, Version 1.0
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/system/system_error.hpp>
#include <boost/core/lightweight_test.hpp>
#include <cerrno>

namespace sys = boost::system;

int main()
{
try {
sys::error_code ec( 5, sys::generic_category() );
throw sys::system_error( ec );
} catch (const std::system_error& err) {
// fine
}

return boost::report_errors();
}
Loading