diff options
| author | 2021-01-24 15:17:02 -0500 | |
|---|---|---|
| committer | 2021-02-28 17:25:31 -0500 | |
| commit | 2910aa77b2feea39fc0618598e275b138a346e71 (patch) | |
| tree | 2760d0c2914c672ae2cd2450851e4feda3b053e5 /src/common/misc.cpp | |
| parent | Merge pull request #6007 from bunnei/ldn-error (diff) | |
| download | yuzu-2910aa77b2feea39fc0618598e275b138a346e71.tar.gz yuzu-2910aa77b2feea39fc0618598e275b138a346e71.tar.xz yuzu-2910aa77b2feea39fc0618598e275b138a346e71.zip | |
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
Diffstat (limited to '')
| -rw-r--r-- | src/common/misc.cpp | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/src/common/misc.cpp b/src/common/misc.cpp index 1d5393597..495385b9e 100644 --- a/src/common/misc.cpp +++ b/src/common/misc.cpp | |||
| @@ -12,27 +12,41 @@ | |||
| 12 | 12 | ||
| 13 | #include "common/common_funcs.h" | 13 | #include "common/common_funcs.h" |
| 14 | 14 | ||
| 15 | // Generic function to get last error message. | 15 | std::string NativeErrorToString(int e) { |
| 16 | // Call directly after the command or use the error num. | ||
| 17 | // This function might change the error code. | ||
| 18 | std::string GetLastErrorMsg() { | ||
| 19 | static constexpr std::size_t buff_size = 255; | ||
| 20 | char err_str[buff_size]; | ||
| 21 | |||
| 22 | #ifdef _WIN32 | 16 | #ifdef _WIN32 |
| 23 | FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), | 17 | LPSTR err_str; |
| 24 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), err_str, buff_size, nullptr); | 18 | |
| 25 | return std::string(err_str, buff_size); | 19 | DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 26 | #elif defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)) | 20 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 21 | nullptr, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | ||
| 22 | reinterpret_cast<LPSTR>(&err_str), 1, nullptr); | ||
| 23 | if (!res) { | ||
| 24 | return "(FormatMessageA failed to format error)"; | ||
| 25 | } | ||
| 26 | std::string ret(err_str); | ||
| 27 | LocalFree(err_str); | ||
| 28 | return ret; | ||
| 29 | #else | ||
| 30 | char err_str[255]; | ||
| 31 | #if defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)) | ||
| 27 | // Thread safe (GNU-specific) | 32 | // Thread safe (GNU-specific) |
| 28 | const char* str = strerror_r(errno, err_str, buff_size); | 33 | const char* str = strerror_r(e, err_str, sizeof(err_str)); |
| 29 | return std::string(str); | 34 | return std::string(str); |
| 30 | #else | 35 | #else |
| 31 | // Thread safe (XSI-compliant) | 36 | // Thread safe (XSI-compliant) |
| 32 | const int success = strerror_r(errno, err_str, buff_size); | 37 | int second_err = strerror_r(e, err_str, sizeof(err_str)); |
| 33 | if (success != 0) { | 38 | if (second_err != 0) { |
| 34 | return {}; | 39 | return "(strerror_r failed to format error)"; |
| 35 | } | 40 | } |
| 36 | return std::string(err_str); | 41 | return std::string(err_str); |
| 42 | #endif // GLIBC etc. | ||
| 43 | #endif // _WIN32 | ||
| 44 | } | ||
| 45 | |||
| 46 | std::string GetLastErrorMsg() { | ||
| 47 | #ifdef _WIN32 | ||
| 48 | return NativeErrorToString(GetLastError()); | ||
| 49 | #else | ||
| 50 | return NativeErrorToString(errno); | ||
| 37 | #endif | 51 | #endif |
| 38 | } | 52 | } |