Skip to content

Commit

Permalink
Added getError at result that returns response header and body
Browse files Browse the repository at this point in the history
  • Loading branch information
durner committed Jul 5, 2023
1 parent ad3eb18 commit 1c14fd3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
6 changes: 5 additions & 1 deletion include/network/message_result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class MessageResult {
uint64_t size;
/// The offset of the result; the start after the message header
uint64_t offset;
/// The error response
const MessageResult* originError;
/// The failure code
uint16_t failureCode;
/// The state
Expand Down Expand Up @@ -96,8 +98,10 @@ class MessageResult {
[[nodiscard]] uint64_t getOffset() const;
/// Get the state
[[nodiscard]] MessageState getState() const;
/// Get the original message
/// Get the failure code
[[nodiscard]] uint16_t getFailureCode() const;
/// Get the error response (incl. header)
[[nodiscard]] std::string_view getError() const;
/// Is the data owned by this object
[[nodiscard]] bool owned() const;
/// Was the request successful
Expand Down
13 changes: 10 additions & 3 deletions include/network/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Transaction {
std::atomic<int> outstanding;
/// The state
std::atomic<State> state;
/// The error message id
std::atomic<int> errorMessageId;

/// The constructor
explicit MultipartUpload(int parts) : messages(parts + 1), eTags(parts), outstanding(parts), state(State::Default) {}
Expand Down Expand Up @@ -179,23 +181,28 @@ class Transaction {
for (auto i = 1ull; i <= parts; i++) {
auto finishMultipart = [&callback, &initalRequestResult, position, remotePath, traceId, i, parts, this](network::MessageResult& result) {
if (!result.success()) [[unlikely]] {
_multipartUploads[position].errorMessageId = i - 1;
_multipartUploads[position].state = MultipartUpload::State::Aborted;
} else {
_multipartUploads[position].eTags[i - 1] = _provider->getETag(std::string_view(reinterpret_cast<const char*>(result.getData()), result.getOffset()));
}
if (_multipartUploads[position].outstanding.fetch_sub(1) == 1) {
if (_multipartUploads[position].state != MultipartUpload::State::Aborted) [[likely]] {
auto finished = [&callback, &initalRequestResult, this](network::MessageResult& result) {
if (!result.success())
auto finished = [&callback, &initalRequestResult, position, this](network::MessageResult& result) {
if (!result.success()) {
_multipartUploads[position].errorMessageId = _multipartUploads[position].messages.size() - 1;
initalRequestResult.state = network::MessageState::Cancelled;
initalRequestResult.originError = &result;
}
_completedMultiparts++;
std::forward<Callback>(callback)(initalRequestResult);
};
auto originalMsg = makeCallbackMessage(std::move(finished), _provider->completeMultiPartRequest(remotePath, _multipartUploads[position].uploadId, _multipartUploads[position].eTags), _provider->getAddress(), _provider->getPort(), nullptr, 0, traceId);
_multipartUploads[position].messages[parts] = std::move(originalMsg);
} else {
auto finished = [&callback, &initalRequestResult, this](network::MessageResult& /*result*/) {
auto finished = [&callback, &initalRequestResult, position, this](network::MessageResult& /*result*/) {
initalRequestResult.state = network::MessageState::Cancelled;
initalRequestResult.originError = &_multipartUploads[position].messages[_multipartUploads[position].errorMessageId]->result;
_completedMultiparts++;
std::forward<Callback>(callback)(initalRequestResult);
};
Expand Down
15 changes: 12 additions & 3 deletions src/network/message_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ namespace network {
//---------------------------------------------------------------------------
using namespace std;
//---------------------------------------------------------------------------
MessageResult::MessageResult() : size(), offset(), failureCode(), state(MessageState::Init)
MessageResult::MessageResult() : size(), offset(), originError(), failureCode(), state(MessageState::Init)
// The default constructor
{
dataVector = make_unique<utils::DataVector<uint8_t>>();
}
//---------------------------------------------------------------------------
MessageResult::MessageResult(uint8_t* data, uint64_t size) : size(), offset(), failureCode(), state(MessageState::Init)
MessageResult::MessageResult(uint8_t* data, uint64_t size) : size(), offset(), originError(), failureCode(), state(MessageState::Init)
// The constructor with buffer input
{
if (data)
Expand Down Expand Up @@ -83,11 +83,20 @@ MessageState MessageResult::getState() const
}
//---------------------------------------------------------------------------
uint16_t MessageResult::getFailureCode() const
// Get the original message
// Get the failure code
{
return failureCode;
}
//---------------------------------------------------------------------------
std::string_view MessageResult::getError() const
// Get the error header
{
if (!originError)
return originError->getError();
else
return string_view(reinterpret_cast<char*>(dataVector->data()), offset + size);
}
//---------------------------------------------------------------------------
bool MessageResult::owned() const
// Is the data onwed by this
{
Expand Down

0 comments on commit 1c14fd3

Please sign in to comment.