Skip to content

Commit

Permalink
Added better error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
durner committed Mar 6, 2024
1 parent 412f63b commit 0682125
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
4 changes: 2 additions & 2 deletions include/network/http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct HttpResponse {
Type type;

/// Get the request method
static constexpr auto getResponseCode(const Code& code) noexcept {
static constexpr std::string_view getResponseCode(const Code& code) noexcept {
switch (code) {
case Code::OK_200: return "200 OK";
case Code::CREATED_201: return "201 Created";
Expand All @@ -75,7 +75,7 @@ struct HttpResponse {
}
}
/// Get the request type
static constexpr auto getResponseType(const Type& type) noexcept {
static constexpr std::string_view getResponseType(const Type& type) noexcept {
switch (type) {
case Type::HTTP_1_0: return "HTTP/1.0";
case Type::HTTP_1_1: return "HTTP/1.1";
Expand Down
11 changes: 6 additions & 5 deletions include/network/message_result.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "network/http_helper.hpp"
#include <atomic>
#include <memory>
#include <string_view>
Expand Down Expand Up @@ -63,10 +64,8 @@ class MessageResult {
protected:
/// The data
std::unique_ptr<utils::DataVector<uint8_t>> dataVector;
/// The size of the result; the size of the message body
uint64_t size;
/// The offset of the result; the start after the message header
uint64_t offset;
/// The http response header info
std::unique_ptr<HttpHelper::Info> response;
/// The error response
const MessageResult* originError;
/// The failure code
Expand Down Expand Up @@ -101,7 +100,9 @@ class MessageResult {
/// Get the failure code
[[nodiscard]] uint16_t getFailureCode() const;
/// Get the error response (incl. header)
[[nodiscard]] std::string_view getError() const;
[[nodiscard]] std::string_view getErrorResponse() const;
/// Get the error response code
[[nodiscard]] std::string_view getResponseCode() const;
/// Is the data owned by this object
[[nodiscard]] bool owned() const;
/// Was the request successful
Expand Down
5 changes: 2 additions & 3 deletions src/network/http_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ MessageState HTTPMessage::execute(IOUringSocket& socket)
// check whether finished http
if (HttpHelper::finished(receive.data(), static_cast<uint64_t>(receiveBufferOffset), info)) {
socket.disconnect(request->fd, originalMessage->hostname, originalMessage->port, &tcpSettings, static_cast<uint64_t>(sendBufferOffset + receiveBufferOffset));
originalMessage->result.size = info->length;
originalMessage->result.offset = info->headerLength;
if (HttpResponse::checkSuccess(info->response.code)) {
originalMessage->result.response = move(info);
if (HttpResponse::checkSuccess(originalMessage->result.response->response.code)) {
state = MessageState::Finished;
} else {
originalMessage->result.failureCode |= static_cast<uint16_t>(MessageFailureCode::HTTP);
Expand Down
5 changes: 2 additions & 3 deletions src/network/https_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ MessageState HTTPSMessage::execute(IOUringSocket& socket)
receiveBufferOffset += result;
try {
if (HttpHelper::finished(receive.data(), static_cast<uint64_t>(receiveBufferOffset), info)) {
originalMessage->result.size = info->length;
originalMessage->result.offset = info->headerLength;
if (HttpResponse::checkSuccess(info->response.code)) {
originalMessage->result.response = move(info);
if (HttpResponse::checkSuccess(originalMessage->result.response->response.code)) {
state = MessageState::TLSShutdown;
} else {
originalMessage->result.failureCode |= static_cast<uint16_t>(MessageFailureCode::HTTP);
Expand Down
28 changes: 19 additions & 9 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(), originError(nullptr), failureCode(), state(MessageState::Init)
MessageResult::MessageResult() : response(), originError(nullptr), failureCode(), state(MessageState::Init)
// The default constructor
{
dataVector = make_unique<utils::DataVector<uint8_t>>();
}
//---------------------------------------------------------------------------
MessageResult::MessageResult(uint8_t* data, uint64_t size) : size(), offset(), originError(nullptr), failureCode(), state(MessageState::Init)
MessageResult::MessageResult(uint8_t* data, uint64_t size) : response(), originError(nullptr), failureCode(), state(MessageState::Init)
// The constructor with buffer input
{
if (data)
Expand All @@ -28,7 +28,7 @@ MessageResult::MessageResult(uint8_t* data, uint64_t size) : size(), offset(), o
dataVector = make_unique<utils::DataVector<uint8_t>>();
}
//---------------------------------------------------------------------------
MessageResult::MessageResult(utils::DataVector<uint8_t>* dataVector) : size(), offset(), originError(nullptr), failureCode(), state(MessageState::Init)
MessageResult::MessageResult(utils::DataVector<uint8_t>* dataVector) : response(), originError(nullptr), failureCode(), state(MessageState::Init)
// The constructor with buffer input
{
this->dataVector = unique_ptr<utils::DataVector<uint8_t>>(dataVector);
Expand All @@ -37,13 +37,13 @@ MessageResult::MessageResult(utils::DataVector<uint8_t>* dataVector) : size(), o
const string_view MessageResult::getResult() const
/// Get the result
{
return string_view(reinterpret_cast<const char*>(dataVector->cdata()) + offset, size);
return string_view(reinterpret_cast<const char*>(dataVector->cdata()) + response->headerLength, response->length);
}
//---------------------------------------------------------------------------
string_view MessageResult::getResult()
/// Get the result
{
return string_view(reinterpret_cast<char*>(dataVector->data()) + offset, size);
return string_view(reinterpret_cast<char*>(dataVector->data()) + response->headerLength, response->length);
}
//---------------------------------------------------------------------------
const uint8_t* MessageResult::getData() const
Expand All @@ -67,13 +67,13 @@ unique_ptr<uint8_t[]> MessageResult::moveData()
uint64_t MessageResult::getSize() const
// Get the size
{
return size;
return response->length;
}
//---------------------------------------------------------------------------
uint64_t MessageResult::getOffset() const
// Get the offset
{
return offset;
return response->headerLength;
}
//---------------------------------------------------------------------------
MessageState MessageResult::getState() const
Expand All @@ -91,11 +91,21 @@ uint16_t MessageResult::getFailureCode() const
return failureCode;
}
//---------------------------------------------------------------------------
std::string_view MessageResult::getError() const
std::string_view MessageResult::getResponseCode() const
// Get the failure code
{
if (originError)
return originError->getResponseCode();
else
return HttpResponse::getResponseCode(response->response.code);
}

//---------------------------------------------------------------------------
std::string_view MessageResult::getErrorResponse() const
// Get the error header
{
if (originError)
return originError->getError();
return originError->getErrorResponse();
else
return string_view(reinterpret_cast<char*>(dataVector->data()), dataVector->size());
}
Expand Down

0 comments on commit 0682125

Please sign in to comment.