I need to test application responses to possible errors to ensure the error handling and information to user is correct at application level. Is there a library to force errors to test how robust an application is to these?
Ideally I want c++ source code or a tutorial for doing this.
I have read these:-
Atlas: Handle Errors (for java)
Error Handling (for .NET)
git example: exception.cpp
The core application is most likely to be accessing an Atlas database.
At this point I would probably be targeting the most common errors, but it would be much better if there was a comprehensive test suite I could just link in.
These files seem to containe the potential error codes (extracts are just for general reference at this point as I don’t anticipate testing them all).
mongocxx\exception\error_code.hpp
bsoncxx\exception\error_code.hpp
libmongoc-1.0\mongoc\mongoc-error.h
libbson-1.0\bson\bson-error.h
///
/// Enum representing the various error types that can occur during driver usage.
///
enum class error_code : std::int32_t {
/// More than one mongocxx::instance has been created.
k_cannot_recreate_instance = 1,
/// A default-constructed or moved-from mongocxx::client object has been used.
k_invalid_client_object,
/// A default-constructed or moved-from mongocxx::collection object has been used.
k_invalid_collection_object,
/// A default-constructed or moved-from mongocxx::database object has been used.
k_invalid_database_object,
/// An invalid or out-of-bounds parameter was provided.
k_invalid_parameter,
/// An SSL operation was used without SSL support being built.
k_ssl_not_supported,
/// An unknown read concern level was set.
k_unknown_read_concern,
/// An unknown write concern level was set.
k_unknown_write_concern,
/// The server returned a malformed response.
k_server_response_malformed,
/// An invalid MongoDB URI was provided.
k_invalid_uri,
/// A default-constructed or moved-from mongocxx::gridfs::bucket object has been used.
k_invalid_gridfs_bucket_object,
/// A default-constructed or moved-from mongocxx::gridfs::uploader object has been used.
k_invalid_gridfs_uploader_object,
/// A default-constructed or moved-from mongocxx::gridfs::downloader object has been used.
k_invalid_gridfs_downloader_object,
/// A mongocxx::gridfs::uploader object was not open for writing, or a
/// mongocxx::gridfs::downloader object was not open for reading.
k_gridfs_stream_not_open,
/// A mongocxx::gridfs::uploader object has exceeded the maximum number of allowable GridFS
/// chunks when attempting to upload the requested file.
k_gridfs_upload_requires_too_many_chunks,
/// The requested GridFS file was not found.
k_gridfs_file_not_found,
/// A GridFS file being operated on was discovered to be corrupted.
k_gridfs_file_corrupted,
/// The mongocxx::instance has been destroyed.
k_instance_destroyed,
/// mongocxx::client.create_session failed to create a mongocxx::client_session.
k_cannot_create_session,
/// A failure attempting to pass a mongocxx::client_session to a method.
k_invalid_session,
/// A moved-from mongocxx::options::transaction object has been used.
k_invalid_transaction_options_object,
// A resource (server API handle, etc.) could not be created:
k_create_resource_fail,
// Add new constant string message to error_code.cpp as well!
};
namespace bsoncxx {
BSONCXX_INLINE_NAMESPACE_BEGIN
///
/// Enum representing the various error types that can occur while operating on BSON values.
///
enum class error_code : std::int32_t {
/// A new key was appended while building a subarray.
k_cannot_append_key_in_sub_array = 1,
/// A subarray was closed while building a subdocument.
k_cannot_close_array_in_sub_document,
/// A subdocument was closed while building a subarray.
k_cannot_close_document_in_sub_array,
/// An array operation was performed while building a document.
k_cannot_perform_array_operation_on_document,
/// A document operation was performed while building an array.
k_cannot_perform_document_operation_on_array,
#define BSONCXX_ENUM(name, value) k_need_element_type_k_##name,
#include <bsoncxx/enums/type.hpp>
#undef BSONCXX_ENUM
/// No key was provided when one was needed.
k_need_key,
/// An array was closed while no array was open.
k_no_array_to_close,
/// A document was closed while no document was open.
k_no_document_to_close,
// Attempted to view or extract a document when a key was still awaiting a matching value.
k_unmatched_key_in_builder,
/// An empty element was accessed.
k_unset_element,
/// A JSON document failed to parse.
k_json_parse_failure,
/// An Object ID string failed to parse.
k_invalid_oid,
// This type is unused and deprecated.
k_failed_converting_bson_to_json,
/// A Decimal128 string failed to parse.
k_invalid_decimal128,
/// BSON data could not be processed, but no specific reason was available.
k_internal_error,
/// Failed to begin appending an array to a BSON document or array.
k_cannot_begin_appending_array,
/// Failed to begin appending a BSON document to a BSON document or array.
k_cannot_begin_appending_document,
/// Failed to complete appending an array to a BSON document or array.
k_cannot_end_appending_array,
/// Failed to complete appending a BSON document to a BSON document or array.
k_cannot_end_appending_document,
/// Invalid binary subtype.
k_invalid_binary_subtype,
/// Invalid type.
k_invalid_bson_type_id,
/// A value failed to append.
#define BSONCXX_ENUM(name, value) k_cannot_append_##name,
#include <bsoncxx/enums/type.hpp>
#undef BSONCXX_ENUM
k_cannot_append_utf8 = k_cannot_append_string,
k_need_element_type_k_utf8 = k_need_element_type_k_string,
// Add new constant string message to error_code.cpp as well!
};
///
/// Get the error_category for exceptions originating from the bsoncxx library.
///
/// @return The bsoncxx error_category
///
BSONCXX_API const std::error_category& BSONCXX_CALL error_category();
///
/// Translate a bsoncxx::error_code into a std::error_code.
///
/// @param error An error from bsoncxx
/// @return An error_code
///
BSONCXX_INLINE std::error_code make_error_code(error_code error) {
return {static_cast<int>(error), error_category()};
}
BSONCXX_INLINE_NAMESPACE_END
} // namespace bsoncxx
BSON_BEGIN_DECLS
typedef enum {
MONGOC_ERROR_CLIENT = 1,
MONGOC_ERROR_STREAM,
MONGOC_ERROR_PROTOCOL,
MONGOC_ERROR_CURSOR,
MONGOC_ERROR_QUERY,
MONGOC_ERROR_INSERT,
MONGOC_ERROR_SASL,
MONGOC_ERROR_BSON,
MONGOC_ERROR_MATCHER,
MONGOC_ERROR_NAMESPACE,
MONGOC_ERROR_COMMAND,
MONGOC_ERROR_COLLECTION,
MONGOC_ERROR_GRIDFS,
MONGOC_ERROR_SCRAM,
MONGOC_ERROR_SERVER_SELECTION,
MONGOC_ERROR_WRITE_CONCERN,
MONGOC_ERROR_SERVER, /* Error API Version 2 only */
MONGOC_ERROR_TRANSACTION,
MONGOC_ERROR_CLIENT_SIDE_ENCRYPTION, /* An error coming from libmongocrypt */
MONGOC_ERROR_POOL
} mongoc_error_domain_t;
typedef enum {
MONGOC_ERROR_STREAM_INVALID_TYPE = 1,
MONGOC_ERROR_STREAM_INVALID_STATE,
MONGOC_ERROR_STREAM_NAME_RESOLUTION,
MONGOC_ERROR_STREAM_SOCKET,
MONGOC_ERROR_STREAM_CONNECT,
MONGOC_ERROR_STREAM_NOT_ESTABLISHED,
MONGOC_ERROR_CLIENT_NOT_READY,
MONGOC_ERROR_CLIENT_TOO_BIG,
MONGOC_ERROR_CLIENT_TOO_SMALL,
MONGOC_ERROR_CLIENT_GETNONCE,
MONGOC_ERROR_CLIENT_AUTHENTICATE,
MONGOC_ERROR_CLIENT_NO_ACCEPTABLE_PEER,
MONGOC_ERROR_CLIENT_IN_EXHAUST,
MONGOC_ERROR_PROTOCOL_INVALID_REPLY,
MONGOC_ERROR_PROTOCOL_BAD_WIRE_VERSION,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
MONGOC_ERROR_QUERY_FAILURE,
MONGOC_ERROR_BSON_INVALID,
MONGOC_ERROR_MATCHER_INVALID,
MONGOC_ERROR_NAMESPACE_INVALID,
MONGOC_ERROR_NAMESPACE_INVALID_FILTER_TYPE,
MONGOC_ERROR_COMMAND_INVALID_ARG,
MONGOC_ERROR_COLLECTION_INSERT_FAILED,
MONGOC_ERROR_COLLECTION_UPDATE_FAILED,
MONGOC_ERROR_COLLECTION_DELETE_FAILED,
MONGOC_ERROR_COLLECTION_DOES_NOT_EXIST = 26,
MONGOC_ERROR_GRIDFS_INVALID_FILENAME,
MONGOC_ERROR_SCRAM_NOT_DONE,
MONGOC_ERROR_SCRAM_PROTOCOL_ERROR,
MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND = 59,
MONGOC_ERROR_QUERY_NOT_TAILABLE = 13051,
MONGOC_ERROR_SERVER_SELECTION_BAD_WIRE_VERSION,
MONGOC_ERROR_SERVER_SELECTION_FAILURE,
MONGOC_ERROR_SERVER_SELECTION_INVALID_ID,
MONGOC_ERROR_GRIDFS_CHUNK_MISSING,
MONGOC_ERROR_GRIDFS_PROTOCOL_ERROR,
/* Dup with query failure. */
MONGOC_ERROR_PROTOCOL_ERROR = 17,
MONGOC_ERROR_WRITE_CONCERN_ERROR = 64,
MONGOC_ERROR_DUPLICATE_KEY = 11000,
MONGOC_ERROR_MAX_TIME_MS_EXPIRED = 50,
MONGOC_ERROR_CHANGE_STREAM_NO_RESUME_TOKEN,
MONGOC_ERROR_CLIENT_SESSION_FAILURE,
MONGOC_ERROR_TRANSACTION_INVALID_STATE,
MONGOC_ERROR_GRIDFS_CORRUPT,
MONGOC_ERROR_GRIDFS_BUCKET_FILE_NOT_FOUND,
MONGOC_ERROR_GRIDFS_BUCKET_STREAM,
/* An error related to initializing client side encryption. */
MONGOC_ERROR_CLIENT_INVALID_ENCRYPTION_STATE,
MONGOC_ERROR_CLIENT_INVALID_ENCRYPTION_ARG,
/* An error related to server version api */
MONGOC_ERROR_CLIENT_API_ALREADY_SET,
MONGOC_ERROR_CLIENT_API_FROM_POOL,
MONGOC_ERROR_POOL_API_ALREADY_SET,
MONGOC_ERROR_POOL_API_TOO_LATE,
MONGOC_ERROR_CLIENT_INVALID_LOAD_BALANCER,
} mongoc_error_code_t;
MONGOC_EXPORT (bool)
mongoc_error_has_label (const bson_t *reply, const char *label);
BSON_END_DECLS