diff options
author | Andy Schwerin <schwerin@10gen.com> | 2012-10-29 13:11:11 -0400 |
---|---|---|
committer | Andy Schwerin <schwerin@10gen.com> | 2012-11-01 23:06:51 -0400 |
commit | 79c550530065678d1611567cf262a7b325c2db47 (patch) | |
tree | d7f61dc27a788b4461811494f6fcf7efbcb136ea /src/mongo/base | |
parent | bd5d48f9c57df96e2a11933057e9a90392a89e40 (diff) | |
download | mongo-79c550530065678d1611567cf262a7b325c2db47.tar.gz |
Add an isOK method to Status, and methods to get an ErrorCodes::Error from a string or an int.
Diffstat (limited to 'src/mongo/base')
-rw-r--r-- | src/mongo/base/generate_error_codes.py | 44 | ||||
-rw-r--r-- | src/mongo/base/status.h | 1 | ||||
-rw-r--r-- | src/mongo/base/status_test.cpp | 14 |
3 files changed, 57 insertions, 2 deletions
diff --git a/src/mongo/base/generate_error_codes.py b/src/mongo/base/generate_error_codes.py index 4ca74b04b05..c2acd47ef21 100644 --- a/src/mongo/base/generate_error_codes.py +++ b/src/mongo/base/generate_error_codes.py @@ -120,10 +120,19 @@ def generate_header(filename, error_codes, error_classes): error_code_class_predicate_declarations=predicate_declarations)) def generate_source(filename, error_codes, error_classes): - symbol_to_string_cases = ';\n '.join('case %s: return "%s"' % (ec[0], ec[0]) for ec in error_codes) - predicate_definitions = '\n '.join(generate_error_class_predicate_definition(*ec) for ec in error_classes) + symbol_to_string_cases = ';\n '.join( + 'case %s: return "%s"' % (ec[0], ec[0]) for ec in error_codes) + string_to_symbol_cases = ';\n '.join( + 'if (!strncmp("%s", name.data(), name.size())) return %s' % (ec[0], ec[0]) + for ec in error_codes) + int_to_symbol_cases = ';\n '.join( + 'case %s: return %s' % (ec[0], ec[0]) for ec in error_codes) + predicate_definitions = '\n '.join( + generate_error_class_predicate_definition(*ec) for ec in error_classes) open(filename, 'w').write(source_template % dict( symbol_to_string_cases=symbol_to_string_cases, + string_to_symbol_cases=string_to_symbol_cases, + int_to_symbol_cases=int_to_symbol_cases, error_code_class_predicate_definitions=predicate_definitions)) def generate_error_class_predicate_definition(class_name, code_names): @@ -135,6 +144,8 @@ header_template = '''// AUTO-GENERATED FILE DO NOT EDIT #pragma once +#include "mongo/base/string_data.h" + namespace mongo { /** @@ -153,6 +164,20 @@ namespace mongo { static const char* errorString(Error err); + /** + * Parse 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. + */ + static Error fromInt(int code); + %(error_code_class_predicate_declarations)s; }; @@ -164,6 +189,8 @@ source_template = '''// AUTO-GENERATED FILE DO NOT EDIT #include "mongo/base/error_codes.h" +#include <cstring> + namespace mongo { const char *ErrorCodes::errorString(Error err) { switch (err) { @@ -172,6 +199,19 @@ namespace mongo { } } + ErrorCodes::Error ErrorCodes::fromString(const StringData& name) { + %(string_to_symbol_cases)s; + return UnknownError; + } + + ErrorCodes::Error ErrorCodes::fromInt(int code) { + switch (code) { + %(int_to_symbol_cases)s; + default: + return UnknownError; + } + } + %(error_code_class_predicate_definitions)s } // namespace mongo ''' diff --git a/src/mongo/base/status.h b/src/mongo/base/status.h index ed3266566cd..d71852c6c41 100644 --- a/src/mongo/base/status.h +++ b/src/mongo/base/status.h @@ -82,6 +82,7 @@ namespace mongo { // accessors // + bool isOK() const { return code() == ErrorCodes::OK; } ErrorCodes::Error code() const { return _error->code; } const char* codeString() const { return ErrorCodes::errorString(_error->code); } const std::string& reason() const { return _error->reason; } diff --git a/src/mongo/base/status_test.cpp b/src/mongo/base/status_test.cpp index 5b0979b4d0f..00f40a42962 100644 --- a/src/mongo/base/status_test.cpp +++ b/src/mongo/base/status_test.cpp @@ -63,4 +63,18 @@ namespace { ASSERT_EQUALS(Status::OK().refCount(), 0); } + 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::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 |