summaryrefslogtreecommitdiff
path: root/zen/sys_error.h
diff options
context:
space:
mode:
Diffstat (limited to 'zen/sys_error.h')
-rw-r--r--zen/sys_error.h41
1 files changed, 21 insertions, 20 deletions
diff --git a/zen/sys_error.h b/zen/sys_error.h
index 9f7667db..7fb12d31 100644
--- a/zen/sys_error.h
+++ b/zen/sys_error.h
@@ -28,14 +28,12 @@ namespace zen
typedef DWORD ErrorCode;
#elif defined ZEN_LINUX || defined ZEN_MAC
typedef int ErrorCode;
-#else
- #error define a platform!
#endif
ErrorCode getLastError();
-std::wstring formatSystemError(const std::wstring& functionName, ErrorCode lastError);
-std::wstring formatSystemError(const std::wstring& functionName, ErrorCode lastError, const std::wstring& lastErrorMsg);
+std::wstring formatSystemError(const std::wstring& functionName, ErrorCode ec);
+std::wstring formatSystemError(const std::wstring& functionName, ErrorCode ec, const std::wstring& errorMsg);
//A low-level exception class giving (non-translated) detail information only - same conceptional level like "GetLastError()"!
class SysError
@@ -67,16 +65,14 @@ ErrorCode getLastError()
}
-std::wstring formatSystemError(const std::wstring& functionName, long long lastError) = delete; //not implemented! intentional overload ambiguity to catch usage errors with HRESULT!
-
+std::wstring formatSystemErrorRaw(long long) = delete; //intentional overload ambiguity to catch usage errors
inline
-std::wstring formatSystemError(const std::wstring& functionName, ErrorCode lastError)
+std::wstring formatSystemErrorRaw(ErrorCode ec) //return empty string on error
{
const ErrorCode currentError = getLastError(); //not necessarily == lastError
- std::wstring lastErrorMsg;
-
+ std::wstring errorMsg;
#ifdef ZEN_WIN
ZEN_ON_SCOPE_EXIT(::SetLastError(currentError)); //this function must not change active system error variable!
@@ -84,37 +80,42 @@ std::wstring formatSystemError(const std::wstring& functionName, ErrorCode lastE
if (::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_MAX_WIDTH_MASK |
FORMAT_MESSAGE_IGNORE_INSERTS | //important: without this flag ::FormatMessage() will fail if message contains placeholders
- FORMAT_MESSAGE_ALLOCATE_BUFFER, nullptr, lastError, 0, reinterpret_cast<LPWSTR>(&buffer), 0, nullptr) != 0)
+ FORMAT_MESSAGE_ALLOCATE_BUFFER, nullptr, ec, 0, reinterpret_cast<LPWSTR>(&buffer), 0, nullptr) != 0)
if (buffer) //"don't trust nobody"
{
ZEN_ON_SCOPE_EXIT(::LocalFree(buffer));
- lastErrorMsg = buffer;
+ errorMsg = buffer;
}
#elif defined ZEN_LINUX || defined ZEN_MAC
ZEN_ON_SCOPE_EXIT(errno = currentError);
- lastErrorMsg = utfCvrtTo<std::wstring>(::strerror(lastError));
+ errorMsg = utfCvrtTo<std::wstring>(::strerror(ec));
#endif
+ trim(errorMsg); //Windows messages seem to end with a blank...
- return formatSystemError(functionName, lastError, lastErrorMsg);
+ return errorMsg;
}
+std::wstring formatSystemError(const std::wstring& functionName, long long lastError) = delete; //intentional overload ambiguity to catch usage errors with HRESULT!
+
inline
-std::wstring formatSystemError(const std::wstring& functionName, ErrorCode lastError, const std::wstring& lastErrorMsg)
+std::wstring formatSystemError(const std::wstring& functionName, ErrorCode ec) { return formatSystemError(functionName, ec, formatSystemErrorRaw(ec)); }
+
+
+inline
+std::wstring formatSystemError(const std::wstring& functionName, ErrorCode ec, const std::wstring& errorMsg)
{
- std::wstring output = replaceCpy(_("Error Code %x:"), L"%x", numberTo<std::wstring>(lastError));
+ std::wstring output = replaceCpy(_("Error Code %x:"), L"%x", numberTo<std::wstring>(ec));
- if (!lastErrorMsg.empty())
+ if (!errorMsg.empty())
{
output += L" ";
- output += lastErrorMsg;
+ output += errorMsg;
}
- if (!endsWith(output, L" ")) //Windows messages seem to end with a blank...
- output += L" ";
- output += L"(" + functionName + L")";
+ output += L" (" + functionName + L")";
return output;
}
bgstack15