upd: [system] adjust the output format of the error message

This commit is contained in:
mutouyun 2022-08-14 13:45:21 +08:00
parent 6329d6c66b
commit 64a3ba24e7
5 changed files with 31 additions and 10 deletions

View File

@ -24,6 +24,11 @@ LIBIMP_EXPORT void error_code(result_code) noexcept;
/**
* @brief Gets a text description of the system error
*/
LIBIMP_EXPORT std::string error_str(result_code) noexcept;
/**
* @brief @brief A text description string with an error number attached
*/
LIBIMP_EXPORT std::string error_msg(result_code) noexcept;
} // namespace sys

View File

@ -36,7 +36,7 @@ void error_code(result_code code) noexcept {
* https://man7.org/linux/man-pages/man3/strerror_l.3.html
* https://manpages.ubuntu.com/manpages/xenial/en/man3/strerror.3.html
*/
std::string error_msg(result_code code) noexcept {
std::string error_str(result_code code) noexcept {
char msg_buf[256] {};
#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
LIBIMP_LOG_();

View File

@ -41,7 +41,7 @@ void error_code(result_code code) noexcept {
* @brief Gets a text description of the system error
* https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage
*/
std::string error_msg(result_code code) noexcept {
std::string error_str(result_code code) noexcept {
LIBIMP_LOG_();
try {
DWORD err = (DWORD)code.value();

View File

@ -3,4 +3,20 @@
#include "libimp/platform/win/system.h"
#else
#include "libimp/platform/posix/system.h"
#endif
#endif
#include "fmt/format.h"
LIBIMP_NAMESPACE_BEG_
namespace sys {
std::string error_msg(result_code code) noexcept {
try {
return ::fmt::format("[{}, \"{}\"]", code.value(), error_str(code));
} catch (...) {
return error_str(code);
}
}
} // namespace sys
LIBIMP_NAMESPACE_END_

View File

@ -14,7 +14,7 @@
#endif
TEST(system, error_code) {
std::cout << fmt::format("{}\n", imp::sys::error_code());
std::cout << fmt::format("{}\n", imp::sys::error_msg(imp::sys::error_code()));
imp::sys::error_code({false, 111});
auto err = imp::sys::error_code();
@ -25,7 +25,7 @@ TEST(system, error_code) {
EXPECT_TRUE(imp::sys::error_code());
}
TEST(system, error_msg) {
TEST(system, error_str) {
#if defined(LIBIMP_OS_WIN)
std::u16string u16_ok, u16_err;
LANGID lId = ::GetSystemDefaultLangID();
@ -44,16 +44,16 @@ TEST(system, error_msg) {
{
std::string s_txt;
imp::cvt_sstr(u16_ok, s_txt);
EXPECT_EQ(imp::sys::error_msg({}), s_txt);
EXPECT_EQ(imp::sys::error_str({}), s_txt);
}
{
std::string s_txt;
imp::cvt_sstr(u16_err, s_txt);
EXPECT_EQ(imp::sys::error_msg({false, ERROR_INVALID_HANDLE}), s_txt);
EXPECT_EQ(imp::sys::error_str({false, ERROR_INVALID_HANDLE}), s_txt);
}
#else
EXPECT_EQ(imp::sys::error_msg({false, 1234}), "Unknown error 1234");
EXPECT_EQ(imp::sys::error_msg({}), "Success");
EXPECT_EQ(imp::sys::error_msg({false, EINVAL}), "Invalid argument");
EXPECT_EQ(imp::sys::error_str({false, 1234}), "Unknown error 1234");
EXPECT_EQ(imp::sys::error_str({}), "Success");
EXPECT_EQ(imp::sys::error_str({false, EINVAL}), "Invalid argument");
#endif
}