From eefcc5dcf6fa71968aa51925df736729f1f0c507 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 2 Oct 2021 14:41:27 +0300 Subject: [PATCH] Add error_code::what --- include/boost/system/detail/error_code.hpp | 17 ++++ test/CMakeLists.txt | 1 + test/Jamfile.v2 | 1 + test/ec_what_test.cpp | 91 ++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 test/ec_what_test.cpp diff --git a/include/boost/system/detail/error_code.hpp b/include/boost/system/detail/error_code.hpp index fb79150..494167c 100644 --- a/include/boost/system/detail/error_code.hpp +++ b/include/boost/system/detail/error_code.hpp @@ -602,6 +602,23 @@ public: { return os << ec.to_string(); } + + std::string what() const + { + std::string r = message(); + + r += " ["; + r += to_string(); + + if( has_location() ) + { + r += " at "; + r += location().to_string(); + } + + r += "]"; + return r; + } }; inline std::size_t hash_value( error_code const & ec ) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6f31850..902feff 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -107,6 +107,7 @@ boost_test(TYPE run SOURCES system_error_test2.cpp) boost_test(TYPE run SOURCES std_interop_test10.cpp) boost_test(TYPE run SOURCES ec_location_test2.cpp) +boost_test(TYPE run SOURCES ec_what_test.cpp) # result diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 33dec94..75f6684 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -129,6 +129,7 @@ run system_error_test2.cpp ; run std_interop_test10.cpp ; run ec_location_test2.cpp ; +run ec_what_test.cpp ; # result diff --git a/test/ec_what_test.cpp b/test/ec_what_test.cpp new file mode 100644 index 0000000..35c1e03 --- /dev/null +++ b/test/ec_what_test.cpp @@ -0,0 +1,91 @@ +// Copyright 2020, 2021 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include + +namespace sys = boost::system; + +int main() +{ + { + sys::error_code ec; + + BOOST_TEST_EQ( ec.value(), 0 ); + BOOST_TEST( ec.category() == sys::system_category() ); + + BOOST_TEST_EQ( ec.what(), ec.message() + " [system:0]" ); + } + + { + sys::error_code ec( 5, sys::generic_category() ); + + BOOST_TEST_EQ( ec.value(), 5 ); + BOOST_TEST( ec.category() == sys::generic_category() ); + + BOOST_TEST_EQ( ec.what(), ec.message() + " [generic:5]" ); + } + + { + sys::error_code ec( 5, sys::system_category() ); + + BOOST_TEST_EQ( ec.value(), 5 ); + BOOST_TEST( ec.category() == sys::system_category() ); + + BOOST_TEST_EQ( ec.what(), ec.message() + " [system:5]" ); + } + + { + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + sys::error_code ec( 5, sys::generic_category(), &loc ); + + BOOST_TEST_EQ( ec.value(), 5 ); + BOOST_TEST( ec.category() == sys::generic_category() ); + BOOST_TEST_EQ( &ec.location(), &loc ); + + BOOST_TEST_EQ( ec.what(), ec.message() + " [generic:5 at " + loc.to_string() + "]" ); + } + + { + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + sys::error_code ec( 5, sys::system_category(), &loc ); + + BOOST_TEST_EQ( ec.value(), 5 ); + BOOST_TEST( ec.category() == sys::system_category() ); + BOOST_TEST_EQ( &ec.location(), &loc ); + + BOOST_TEST_EQ( ec.what(), ec.message() + " [system:5 at " + loc.to_string() + "]" ); + } + +#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR) + + { + std::error_code ec; + sys::error_code ec2( ec ); + + BOOST_TEST_EQ( ec2.what(), ec2.message() + " [std:system:0]" ); + } + + { + std::error_code ec( 5, std::generic_category() ); + sys::error_code ec2( ec ); + + BOOST_TEST_EQ( ec2.what(), ec2.message() + " [std:generic:5]" ); + } + + { + std::error_code ec( 5, std::system_category() ); + sys::error_code ec2( ec ); + + BOOST_TEST_EQ( ec2.what(), ec2.message() + " [std:system:5]" ); + } + +#endif + + return boost::report_errors(); +}