1
0
Fork 0

Exceptions:

- Assert that the associated errorcode has format options so if you call a THROW-macro with parameters these don't get lost unheard.
  - Add new macro which offers format strings, so you can throw standard errors with custom messages.
This commit is contained in:
Willi Goesgens 2015-01-21 15:03:21 +01:00
parent bdb0eb8ff0
commit 854f733c49
2 changed files with 51 additions and 0 deletions

View File

@ -117,6 +117,9 @@ char const* Exception::what () const throw () {
return _errorMessage.c_str();
}
////////////////////////////////////////////////////////////////////////////////
/// @brief construct an error message from a template string
////////////////////////////////////////////////////////////////////////////////
@ -126,6 +129,11 @@ std::string Exception::FillExceptionString (int code,
char const* format = TRI_errno_string(code);
TRI_ASSERT(format != nullptr);
#ifdef TRI_ENABLE_MAINTAINER_MODE
// Obviously the formatstring of the error code has to support parameters.
TRI_ASSERT(strchr(format, '%') != nullptr);
#endif
char buffer[1024];
va_list ap;
va_start(ap, code);
@ -136,6 +144,32 @@ std::string Exception::FillExceptionString (int code,
return std::string(buffer);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief construct an error message from a template string
////////////////////////////////////////////////////////////////////////////////
std::string Exception::FillFormatExceptionString (char const* format,
...) {
TRI_ASSERT(format != nullptr);
#ifdef TRI_ENABLE_MAINTAINER_MODE
// Format #1 should come from the macro...
TRI_ASSERT(strchr(format, '%') != nullptr);
// Obviously the user has to give us a format string.
TRI_ASSERT(strchr(strchr(format, '%'), '%') != nullptr);
#endif
char buffer[1024];
va_list ap;
va_start(ap, format);
vsnprintf(buffer, sizeof(buffer) - 1, format, ap);
va_end(ap);
buffer[sizeof(buffer) - 1] = '\0'; // Windows
return std::string(buffer);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief controls whether a backtrace is created for each exception
////////////////////////////////////////////////////////////////////////////////

View File

@ -54,6 +54,21 @@
#define THROW_ARANGO_EXCEPTION_PARAMS(code, ...) \
throw triagens::arango::Exception(code, triagens::arango::Exception::FillExceptionString(code, __VA_ARGS__), __FILE__, __LINE__)
////////////////////////////////////////////////////////////////////////////////
/// @brief throws an arango exception with an error code and arbitrary
/// arguments (to be inserted in printf-style manner)
////////////////////////////////////////////////////////////////////////////////
#define THROW_ARANGO_EXCEPTION_FORMAT(code, format, ...) \
throw triagens::arango::Exception(code, \
triagens::arango::Exception::FillFormatExceptionString( \
"%s: " format,\
TRI_errno_string(code), \
__VA_ARGS__),\
__FILE__, __LINE__)
////////////////////////////////////////////////////////////////////////////////
/// @brief throws an arango exception with an error code and an already-built
/// error message
@ -105,6 +120,8 @@ namespace triagens {
}
static std::string FillExceptionString (int, ...);
static std::string FillFormatExceptionString (char const * format,
...);
////////////////////////////////////////////////////////////////////////////////
/// @brief controls whether a backtrace is created for each exception