Permalink
Browse files

SERVER-12770 Change stringification and casting of ErrorCodes::Error …

…to improve round-tripping.
  • Loading branch information...
1 parent 56e74d2 commit c5ce70cdef9102b1f71cbf2beb94da7ebb9f1c5c @andy10gen andy10gen committed Feb 18, 2014
Showing with 21 additions and 23 deletions.
  1. +18 −13 src/mongo/base/generate_error_codes.py
  2. +1 −1 src/mongo/base/status-inl.h
  3. +1 −1 src/mongo/base/status.h
  4. +1 −8 src/mongo/base/status_test.cpp
@@ -158,6 +158,8 @@ def generate_error_class_predicate_definition(class_name, code_names):
#pragma once
+#include <string>
+
#include "mongo/base/string_data.h"
#include "mongo/client/export_macros.h"
@@ -177,19 +179,19 @@ class MONGO_CLIENT_API ErrorCodes {
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 @@ class MONGO_CLIENT_API ErrorCodes {
#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 @@ class MONGO_CLIENT_API ErrorCodes {
}
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
'''
@@ -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());
}
View
@@ -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;
@@ -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

0 comments on commit c5ce70c

Please sign in to comment.