exception handling

This commit is contained in:
Scott E. Graves 2024-12-31 13:37:55 -06:00
parent 36908f7da9
commit 45ddd528b2

View File

@ -570,76 +570,87 @@ auto s3_provider::get_file_list(api_file_list &list, std::string &marker) const
-> api_error {
REPERTORY_USES_FUNCTION_NAME();
std::string response_data;
long response_code{};
if (not get_object_list(response_data, response_code, std::nullopt,
std::nullopt, marker)) {
return api_error::comm_error;
}
if (response_code != http_error_codes::ok) {
utils::error::raise_error(function_name, response_code,
"failed to get file list");
return api_error::comm_error;
}
pugi::xml_document doc;
auto result = doc.load_string(response_data.c_str());
if (result.status != pugi::xml_parse_status::status_ok) {
utils::error::raise_error(function_name, result.status,
"failed to parse xml document");
return api_error::comm_error;
}
auto grab_more =
doc.select_node("/ListBucketResult/IsTruncated").node().text().as_bool();
if (grab_more) {
marker = doc.select_node("/ListBucketResult/NextContinuationToken")
.node()
.text()
.as_string();
}
auto node_list = doc.select_nodes("/ListBucketResult/Contents");
for (const auto &node : node_list) {
auto object_name =
std::string{node.node().select_node("Key").node().text().as_string()};
auto api_path{object_name};
if (utils::string::ends_with(api_path, "/")) {
continue;
try {
std::string response_data;
long response_code{};
if (not get_object_list(response_data, response_code, std::nullopt,
std::nullopt, marker)) {
return api_error::comm_error;
}
auto is_encrypted = not get_s3_config().encryption_token.empty();
if (is_encrypted) {
auto res = decrypt_object_name(api_path);
if (response_code != http_error_codes::ok) {
utils::error::raise_error(function_name, response_code,
"failed to get file list");
return api_error::comm_error;
}
pugi::xml_document doc;
auto result = doc.load_string(response_data.c_str());
if (result.status != pugi::xml_parse_status::status_ok) {
utils::error::raise_error(function_name, result.status,
"failed to parse xml document");
return api_error::comm_error;
}
auto grab_more = doc.select_node("/ListBucketResult/IsTruncated")
.node()
.text()
.as_bool();
if (grab_more) {
marker = doc.select_node("/ListBucketResult/NextContinuationToken")
.node()
.text()
.as_string();
}
auto node_list = doc.select_nodes("/ListBucketResult/Contents");
for (const auto &node : node_list) {
auto object_name =
std::string{node.node().select_node("Key").node().text().as_string()};
auto api_path{object_name};
if (utils::string::ends_with(api_path, "/")) {
continue;
}
auto is_encrypted = not get_s3_config().encryption_token.empty();
if (is_encrypted) {
auto res = decrypt_object_name(api_path);
if (res != api_error::success) {
return res;
}
}
auto size = node.node().select_node("Size").node().text().as_ullong();
api_file file{};
file.api_path = utils::path::create_api_path(api_path);
file.api_parent = utils::path::get_parent_api_path(file.api_path);
file.accessed_date = file.changed_date = file.creation_date =
file.modified_date = convert_api_date(node.node()
.select_node("LastModified")
.node()
.text()
.as_string());
file.file_size =
is_encrypted
? utils::encryption::encrypting_reader::calculate_decrypted_size(
size)
: size;
file.key = is_encrypted ? utils::path::create_api_path(object_name) : "";
auto res = add_if_not_found(file, file.key);
if (res != api_error::success) {
return res;
}
list.push_back(std::move(file));
}
auto size = node.node().select_node("Size").node().text().as_ullong();
api_file file{};
file.api_path = utils::path::create_api_path(api_path);
file.api_parent = utils::path::get_parent_api_path(file.api_path);
file.accessed_date = file.changed_date = file.creation_date =
file.modified_date = convert_api_date(
node.node().select_node("LastModified").node().text().as_string());
file.file_size =
is_encrypted
? utils::encryption::encrypting_reader::calculate_decrypted_size(
size)
: size;
file.key = is_encrypted ? utils::path::create_api_path(object_name) : "";
auto res = add_if_not_found(file, file.key);
if (res != api_error::success) {
return res;
}
list.push_back(std::move(file));
return grab_more ? api_error::more_data : api_error::success;
} catch (const std::exception &e) {
utils::error::raise_error(function_name, e, "exception occurred");
}
return grab_more ? api_error::more_data : api_error::success;
return api_error::error;
}
auto s3_provider::get_last_modified(bool directory,
@ -753,13 +764,13 @@ auto s3_provider::is_directory(const std::string &api_path, bool &exists) const
-> api_error {
REPERTORY_USES_FUNCTION_NAME();
exists = false;
if (api_path == "/") {
exists = true;
return api_error::success;
}
try {
exists = false;
if (api_path == "/") {
exists = true;
return api_error::success;
}
bool is_encrypted{};
std::string object_name;
head_object_result result{};
@ -781,12 +792,12 @@ auto s3_provider::is_file(const std::string &api_path, bool &exists) const
-> api_error {
REPERTORY_USES_FUNCTION_NAME();
exists = false;
if (api_path == "/") {
return api_error::success;
}
try {
exists = false;
if (api_path == "/") {
return api_error::success;
}
bool is_encrypted{};
std::string object_name;
head_object_result result{};