Refactor error reporting API.

This commit is contained in:
Victor Zverovich
2014-06-30 14:26:29 -07:00
parent d29e505568
commit 53201033f2
8 changed files with 203 additions and 132 deletions

View File

@@ -1369,7 +1369,7 @@ TEST(FormatterTest, FormatExamples) {
EXPECT_SYSTEM_ERROR({
FILE *f = fopen(filename, "r");
if (!f)
fmt::ThrowSystemError(errno, "Cannot open file '{}'") << filename;
throw fmt::SystemError(errno, "Cannot open file '{}'", filename);
}, error_code, "Cannot open file 'nonexistent'");
}

View File

@@ -107,7 +107,7 @@ void ThrowException() {
}
void ThrowSystemError() {
fmt::ThrowSystemError(EDOM, "test");
throw fmt::SystemError(EDOM, "test");
}
// Tests that when EXPECT_THROW_MSG fails, it evaluates its message argument
@@ -236,12 +236,11 @@ TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) {
// EXPECT_SYSTEM_ERROR macro.
TEST(ExpectSystemErrorTest, DoesNotGenerateUnreachableCodeWarning) {
int n = 0;
EXPECT_SYSTEM_ERROR(throw fmt::SystemError(
FormatSystemErrorMessage(EDOM, "test"), EDOM), EDOM, "test");
EXPECT_SYSTEM_ERROR(throw fmt::SystemError(EDOM, "test"), EDOM, "test");
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(n++, EDOM, ""), "");
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(throw 1, EDOM, ""), "");
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(
throw fmt::SystemError("aaa", EDOM), EDOM, "bbb"), "");
throw fmt::SystemError(EDOM, "aaa"), EDOM, "bbb"), "");
}
TEST(AssertionSyntaxTest, ExceptionAssertionBehavesLikeSingleStatement) {

View File

@@ -38,7 +38,7 @@ void OutputRedirect::Flush() {
int result = 0;
FMT_RETRY(result, fflush(file_));
if (result != 0)
fmt::ThrowSystemError(errno, "cannot flush stream");
throw fmt::SystemError(errno, "cannot flush stream");
}
void OutputRedirect::Restore() {

View File

@@ -163,9 +163,9 @@ TEST(UtilTest, StrError) {
}
TEST(UtilTest, SystemError) {
fmt::SystemError e(StringRef("test"), 42);
EXPECT_STREQ("test", e.what());
EXPECT_EQ(42, e.error_code());
fmt::SystemError e(EDOM, "test");
EXPECT_EQ(fmt::format("test: {}", GetSystemErrorMessage(EDOM)), e.what());
EXPECT_EQ(EDOM, e.error_code());
}
typedef void (*FormatErrorMessage)(
@@ -173,7 +173,7 @@ typedef void (*FormatErrorMessage)(
template <typename Sink>
void CheckErrorSink(int error_code, FormatErrorMessage format) {
fmt::SystemError error("", 0);
fmt::SystemError error(0, "");
Sink sink(error_code);
fmt::Writer w;
w << "test";
@@ -188,12 +188,11 @@ void CheckErrorSink(int error_code, FormatErrorMessage format) {
EXPECT_EQ(error_code, error.error_code());
}
template <typename Sink>
void CheckThrowError(int error_code, FormatErrorMessage format,
fmt::Formatter<Sink> (*throw_error)(int error_code, StringRef format)) {
fmt::SystemError error("", 0);
template <typename Error>
void CheckThrowError(int error_code, FormatErrorMessage format) {
fmt::SystemError error(0, "");
try {
throw_error(error_code, "test {}") << "error";
throw Error(error_code, "test {}", "error");
} catch (const fmt::SystemError &e) {
error = e;
}
@@ -216,8 +215,7 @@ TEST(UtilTest, SystemErrorSink) {
}
TEST(UtilTest, ThrowSystemError) {
CheckThrowError(EDOM,
fmt::internal::FormatSystemErrorMessage, fmt::ThrowSystemError);
CheckThrowError<fmt::SystemError>(EDOM, fmt::internal::FormatSystemErrorMessage);
}
TEST(UtilTest, ReportSystemError) {