pgbackrest.git / summary / log / commit / refs
commit 8f81620b9f5a8de4f2d2ebb0a5ecc2fe49d26435
Author: David Steele <david@pgmasters.net>
Date: Tue Jan 16 18:29:27 2018 +0000
Improve error module.
Add functions to convert error codes to C errors and handle system errors.
doc/xml/release.xml | 4 +++
src/common/error.c | 36 ++++++++++++++++++++
src/common/error.h | 10 ++++++
src/common/errorType.c | 51 +++++++++++++++++++++++++++-
src/common/errorType.h | 3 +-
src/postgres/pageChecksum.c | 2 +-
test/lib/pgBackRestTest/Common/DefineTest.pm | 2 +-
test/src/module/common/errorTest.c | 46 +++++++++++++++++++++++++
8 files changed, 150 insertions(+), 4 deletions(-)
diff --git a/doc/xml/release.xml b/doc/xml/release.xml
index 34b163bf5..1c82f79d6 100644
--- a/doc/xml/release.xml
+++ b/doc/xml/release.xml
@@ -81,6 +81,10 @@
<p>Improve <code>MemContext</code> module. Add temporary context blocks and refactor allocation arrays to include allocation size.</p>
</release-item>
+ <release-item>
+ <p>Improve <code>error</code> module. Add functions to convert error codes to C errors and handle system errors.</p>
+ </release-item>
+
<release-item>
<p>Replace <code>cfgCommandTotal()</code>/<code>cfgOptionTotal()</code> functions with constants. The constants are applicable in more cases and allow the compiler to optimize certain loops more efficiently.</p>
</release-item>
diff --git a/src/common/error.c b/src/common/error.c
index c84b006f0..fb279758a 100644
--- a/src/common/error.c
+++ b/src/common/error.c
@@ -1,6 +1,7 @@
/***********************************************************************************************************************************
Error Handler
***********************************************************************************************************************************/
+#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
@@ -259,6 +260,7 @@ void errorInternalThrow(const ErrorType *errorType, const char *fileName, int fi
va_list argument;
va_start(argument, format);
vsnprintf(messageBufferTemp, ERROR_MESSAGE_BUFFER_SIZE - 1, format, argument);
+ va_end(argument);
// Assign message to the error
strcpy(messageBuffer, messageBufferTemp);
@@ -267,3 +269,37 @@ void errorInternalThrow(const ErrorType *errorType, const char *fileName, int fi
// Propogate the error
errorInternalPropagate();
} // {uncoverable - errorInternalPropagate() does not return}
+
+/***********************************************************************************************************************************
+Throw an error
+***********************************************************************************************************************************/
+void errorInternalThrowSys(int result, const ErrorType *errorType, const char *fileName, int fileLine, const char *format, ...)
+{
+ // Only error if the result is not 0
+ if (result != 0)
+ {
+ // Capture the error number
+ int errNo = errno;
+
+ // Setup error data
+ errorContext.error.errorType = errorType;
+ errorContext.error.fileName = fileName;
+ errorContext.error.fileLine = fileLine;
+
+ // Create message
+ va_list argument;
+ va_start(argument, format);
+ int messageSize = vsnprintf(messageBufferTemp, ERROR_MESSAGE_BUFFER_SIZE - 1, format, argument);
+ va_end(argument);
+
+ // Append the system message
+ snprintf(messageBufferTemp + messageSize, ERROR_MESSAGE_BUFFER_SIZE - 1 - messageSize, ": [%d] %s", errNo, strerror(errNo));
+
+ // Assign message to the error
+ strcpy(messageBuffer, messageBufferTemp);
+ errorContext.error.message = (const char *)messageBuffer;
+
+ // Propogate the error
+ errorInternalPropagate();
+ }
+}
diff --git a/src/common/error.h b/src/common/error.h
index a8547f554..f23c24f7a 100644
--- a/src/common/error.h
+++ b/src/common/error.h
@@ -98,6 +98,15 @@ error information to stderr.
#define THROW(errorType, ...) \
errorInternalThrow(&errorType, __FILE__, __LINE__, __VA_ARGS__)
+#define THROW_CODE(errorCode, ...) \
+ errorInternalThrow(errorTypeFromCode(errorCode), __FILE__, __LINE__, __VA_ARGS__)
+
+/***********************************************************************************************************************************
+Throw an error when a system call fails
+***********************************************************************************************************************************/
+#define THROW_ON_SYS_ERROR(result, errorType, ...) \
+ errorInternalThrowSys(result, &errorType, __FILE__, __LINE__, __VA_ARGS__)
+
/***********************************************************************************************************************************
Rethrow the current error
***********************************************************************************************************************************/
@@ -117,5 +126,6 @@ bool errorInternalStateFinal();
bool errorInternalProcess(bool catch);
void errorInternalPropagate();
void errorInternalThrow(const ErrorType *errorType, const char *fileName, int fileLine, const char *format, ...);
+void errorInternalThrowSys(int result, const ErrorType *errorType, const char *fileName, int fileLine, const char *format, ...);
#endif
diff --git a/src/common/errorType.c b/src/common/errorType.c
index 2ac1d4ed7..398ddd4a2 100644
--- a/src/common/errorType.c
+++ b/src/common/errorType.c
@@ -1,7 +1,7 @@
/***********************************************************************************************************************************
Application-Defined Errors
***********************************************************************************************************************************/
-#include "common/errorType.h"
+#include "common/error.h"
/***********************************************************************************************************************************
Error code range -- chosen to not overlap with defined return values
@@ -44,6 +44,30 @@ ERROR_DEFINE(ERROR_CODE_MIN + 70, CipherError, FormatError);
ERROR_DEFINE(ERROR_CODE_MAX, RuntimeError, RuntimeError);
+/***********************************************************************************************************************************
+Place errors in an array so they can be found by code
+***********************************************************************************************************************************/
+static const ErrorType *errorTypeList[] =
+{
+ &AssertError,
+
+ &FormatError,
+ &CommandRequiredError,
+ &OptionInvalidError,
+ &OptionInvalidValueError,
+ &OptionRequiredError,
+ &FileOpenError,
+ &FileReadError,
+ &CommandInvalidError,
+ &FileWriteError,
+ &MemoryError,
+ &CipherError,
+
+ &RuntimeError,
+
+ NULL,
+};
+
/***********************************************************************************************************************************
Error type code
***********************************************************************************************************************************/
@@ -53,6 +77,31 @@ errorTypeCode(const ErrorType *errorType)
return errorType->code;
}
+/***********************************************************************************************************************************
+Get error type using a code
+***********************************************************************************************************************************/
+const ErrorType *
+errorTypeFromCode(int code)
+{
+ // Search for error type by code
+ int errorTypeIdx = 0;
+ const ErrorType *result = errorTypeList[errorTypeIdx];
+
+ while (result != NULL)
+ {
+ if (result->code == code)
+ break;
+
+ result = errorTypeList[++errorTypeIdx];
+ }
+
+ // Error if type was not found
+ if (result == NULL)
+ THROW(AssertError, "could not find error type for code '%d'", code);
+
+ return result;
+}
+
/***********************************************************************************************************************************
Error type name
***********************************************************************************************************************************/
diff --git a/src/common/errorType.h b/src/common/errorType.h
index e568366cd..b28acf485 100644
--- a/src/common/errorType.h
+++ b/src/common/errorType.h
@@ -32,8 +32,9 @@ ERROR_DECLARE(RuntimeError);
// Functions
int errorTypeCode(const ErrorType *errorType);
+const ErrorType *errorTypeFromCode(int code);
const char *errorTypeName(const ErrorType *errorType);
-const ErrorType * errorTypeParent(const ErrorType *errorType);
+const ErrorType *errorTypeParent(const ErrorType *errorType);
bool errorTypeExtends(const ErrorType *child, const ErrorType *parent);
#endif
diff --git a/src/postgres/pageChecksum.c b/src/postgres/pageChecksum.c
index 3d3018cfd..607cc1b43 100644
--- a/src/postgres/pageChecksum.c
+++ b/src/postgres/pageChecksum.c
@@ -209,7 +209,7 @@ pageChecksumBufferTest(
{
// If the buffer does not represent an even number of pages then error
if (pageBufferSize % pageSize != 0 || pageBufferSize / pageSize == 0)
- THROW(AssertError, "buffer size %lu, page size %lu are not divisible", pageBufferSize, pageSize);
+ THROW(AssertError, "buffer size %d, page size %d are not divisible", pageBufferSize, pageSize);
// Loop through all pages in the buffer
for (int pageIdx = 0; pageIdx < pageBufferSize / pageSize; pageIdx++)
diff --git a/test/lib/pgBackRestTest/Common/DefineTest.pm b/test/lib/pgBackRestTest/Common/DefineTest.pm
index eace0d5a5..ffd54c074 100644
--- a/test/lib/pgBackRestTest/Common/DefineTest.pm
+++ b/test/lib/pgBackRestTest/Common/DefineTest.pm
@@ -110,7 +110,7 @@ my $oTestDef =
},
{
&TESTDEF_NAME => 'error',
- &TESTDEF_TOTAL => 4,
+ &TESTDEF_TOTAL => 6,
&TESTDEF_C => true,
&TESTDEF_CDEF => '-DNO_ERROR',
diff --git a/test/src/module/common/errorTest.c b/test/src/module/common/errorTest.c
index 375f52159..a3e134049 100644
--- a/test/src/module/common/errorTest.c
+++ b/test/src/module/common/errorTest.c
@@ -173,4 +173,50 @@ void testRun()
assert(!testTryRecurseCatch);
assert(testTryRecurseFinally);
}
+
+ // -----------------------------------------------------------------------------------------------------------------------------
+ if (testBegin("THROW_CODE()"))
+ {
+ TRY_BEGIN()
+ {
+ THROW_CODE(25, "message");
+ }
+ CATCH_ANY()
+ {
+ assert(errorCode() == 25);
+ assert(strcmp(errorMessage(), "message") == 0);
+ }
+ TRY_END();
+
+ // -------------------------------------------------------------------------------------------------------------------------
+ TRY_BEGIN()
+ {
+ THROW_CODE(777, "message");
+ }
+ CATCH_ANY()
+ {
+ assert(errorCode() == AssertError.code);
+ assert(strcmp(errorMessage(), "could not find error type for code '777'") == 0);
+ }
+ TRY_END();
+ }
+
+ // -----------------------------------------------------------------------------------------------------------------------------
+ if (testBegin("THROW_ON_SYS_ERROR()"))
+ {
+ THROW_ON_SYS_ERROR(0, AssertError, "message");
+
+ TRY_BEGIN()
+ {
+ errno = E2BIG;
+ THROW_ON_SYS_ERROR(1, AssertError, "message");
+ }
+ CATCH_ANY()
+ {
+ printf("%s\n", errorMessage());
+ assert(errorCode() == AssertError.code);
+ assert(strcmp(errorMessage(), "message: [7] Argument list too long") == 0);
+ }
+ TRY_END();
+ }
}
[parent: bffc6c49b31a]