summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mongo/base/generate_error_codes.py31
-rw-r--r--src/mongo/base/status-inl.h2
-rw-r--r--src/mongo/base/status.h2
-rw-r--r--src/mongo/base/status_test.cpp9
4 files changed, 21 insertions, 23 deletions
diff --git a/src/mongo/base/generate_error_codes.py b/src/mongo/base/generate_error_codes.py
index 19330d1ff57..eee41afce2a 100644
--- a/src/mongo/base/generate_error_codes.py
+++ b/src/mongo/base/generate_error_codes.py
@@ -158,6 +158,8 @@ header_template = '''// AUTO-GENERATED FILE DO NOT EDIT
#pragma once
+#include <string>
+
#include "mongo/base/string_data.h"
#include "mongo/client/export_macros.h"
@@ -177,19 +179,19 @@ namespace mongo {
MaxError
};
- static const char* errorString(Error err);
+ static std::string errorString(Error err);
/**
- * Parse an Error from its "name". Returns UnknownError if "name" is unrecognized.
+ * Parses an Error from its "name". Returns UnknownError if "name" is unrecognized.
*
* NOTE: Also returns UnknownError for the string "UnknownError".
*/
static Error fromString(const StringData& name);
/**
- * Parse an Error from its "code". Returns UnknownError if "code" is unrecognized.
- *
- * NOTE: Also returns UnknownError for the integer code for UnknownError.
+ * Casts an integer "code" to an Error. Unrecognized codes are preserved, meaning
+ * that the result of a call to fromInt() may not be one of the values in the
+ * Error enumeration.
*/
static Error fromInt(int code);
@@ -218,13 +220,16 @@ source_template = '''// AUTO-GENERATED FILE DO NOT EDIT
#include "mongo/base/error_codes.h"
-#include <cstring>
+#include <boost/static_assert.hpp>
+
+#include "mongo/util/mongoutils/str.h"
namespace mongo {
- const char* ErrorCodes::errorString(Error err) {
+
+ std::string ErrorCodes::errorString(Error err) {
switch (err) {
%(symbol_to_string_cases)s;
- default: return "Unknown error code";
+ default: return mongoutils::str::stream() << "Location" << err;
}
}
@@ -234,14 +239,14 @@ namespace mongo {
}
ErrorCodes::Error ErrorCodes::fromInt(int code) {
- switch (code) {
- %(int_to_symbol_cases)s;
- default:
- return UnknownError;
- }
+ return static_cast<Error>(code);
}
%(error_code_class_predicate_definitions)s
+
+namespace {
+ BOOST_STATIC_ASSERT(sizeof(ErrorCodes::Error) == sizeof(int));
+} // namespace
} // namespace mongo
'''
diff --git a/src/mongo/base/status-inl.h b/src/mongo/base/status-inl.h
index 22679707b6c..add962e2369 100644
--- a/src/mongo/base/status-inl.h
+++ b/src/mongo/base/status-inl.h
@@ -45,7 +45,7 @@ namespace mongo {
return _error ? _error->code : ErrorCodes::OK;
}
- inline const char* Status::codeString() const {
+ inline std::string Status::codeString() const {
return ErrorCodes::errorString(code());
}
diff --git a/src/mongo/base/status.h b/src/mongo/base/status.h
index 275cb767e55..ad301f8b008 100644
--- a/src/mongo/base/status.h
+++ b/src/mongo/base/status.h
@@ -89,7 +89,7 @@ namespace mongo {
inline ErrorCodes::Error code() const;
- inline const char* codeString() const;
+ inline std::string codeString() const;
inline std::string reason() const;
diff --git a/src/mongo/base/status_test.cpp b/src/mongo/base/status_test.cpp
index f0cb473f22c..abda898c7f5 100644
--- a/src/mongo/base/status_test.cpp
+++ b/src/mongo/base/status_test.cpp
@@ -78,15 +78,8 @@ namespace {
TEST(Parsing, CodeToEnum) {
ASSERT_EQUALS(ErrorCodes::TypeMismatch, ErrorCodes::fromInt(ErrorCodes::TypeMismatch));
ASSERT_EQUALS(ErrorCodes::UnknownError, ErrorCodes::fromInt(ErrorCodes::UnknownError));
- ASSERT_EQUALS(ErrorCodes::UnknownError, ErrorCodes::fromInt(ErrorCodes::MaxError));
+ ASSERT_EQUALS(ErrorCodes::MaxError, ErrorCodes::fromInt(ErrorCodes::MaxError));
ASSERT_EQUALS(ErrorCodes::OK, ErrorCodes::fromInt(0));
}
- TEST(Parsing, StringToEnum) {
- ASSERT_EQUALS(ErrorCodes::TypeMismatch, ErrorCodes::fromString("TypeMismatch"));
- ASSERT_EQUALS(ErrorCodes::UnknownError, ErrorCodes::fromString("UnknownError"));
- ASSERT_EQUALS(ErrorCodes::UnknownError, ErrorCodes::fromString("Garbage"));
- ASSERT_EQUALS(ErrorCodes::OK, ErrorCodes::fromString("OK"));
- }
-
} // unnamed namespace