Skip to content

Commit

Permalink
berrno: better error messages
Browse files Browse the repository at this point in the history
Now the error number is always printed and trailing whitespace is
trimmed.
  • Loading branch information
sebsura authored and BareosBot committed Jan 22, 2024
1 parent 78929b0 commit 041cc16
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
29 changes: 20 additions & 9 deletions core/src/lib/berrno.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2004-2011 Free Software Foundation Europe e.V.
Copyright (C) 2016-2023 Bareos GmbH & Co. KG
Copyright (C) 2016-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -94,16 +94,27 @@ const char* BErrNo::bstrerror()
void BErrNo::FormatWin32Message()
{
#ifdef HAVE_WIN32
LPVOID msg;
char* msg;
int windows_error_code = GetLastError();
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, windows_error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&msg, 0,
NULL)) {
PmStrcpy(buf_, (const char*)msg);

if (auto len = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, windows_error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&msg, 0, NULL);
len != 0) {
// the formated message often ends in .\r\n
// we want to trim this since our own error messages already append
// '.\n' to the end.
while (len > 0 && isspace(msg[len - 1])) { len -= 1; }

if (len > 0 && msg[len - 1] == '.') { len -= 1; }

msg[len] = '\0';

Mmsg(buf_, "%s (0x%08X)", msg, windows_error_code);
} else {
Mmsg(buf_, "Windows error 0x%08X", windows_error_code);
Mmsg(buf_, "Unknown windows error (0x%08X)", windows_error_code);
}
LocalFree(msg);
#endif
Expand Down
6 changes: 3 additions & 3 deletions core/src/tests/berrno_test.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2021-2023 Bareos GmbH & Co. KG
Copyright (C) 2021-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -47,10 +47,10 @@ const char* bind_error_message = "Bad file number";
#elif defined HAVE_WIN32
const char* open_error_message
= "No such file or directory (errno=2 | win_error=0x00000002)";
const char* win_open_error_message = "File not found.\r\n";
const char* win_open_error_message = "File not found (0x00000002)";
const char* socket_error_message
= "No such file or directory (errno=2 | win_error=0x0000276D)";
const char* win_socket_error_message = "Windows error 0x0000276D";
const char* win_socket_error_message = "Unknown windows error (0x0000276D)";
const char* bind_error_message
= "No such file or directory (errno=2 | win_error=0x000027";
#else
Expand Down

0 comments on commit 041cc16

Please sign in to comment.