better error handling

This commit is contained in:
2022-07-07 21:59:16 +02:00
parent ec1b41bb7f
commit ccfde2c08d
2 changed files with 48 additions and 19 deletions

View File

@ -61,7 +61,8 @@ void SslWebsocketClient::onResolved(const std::error_code &error, asio::ip::tcp:
{ {
if (error) if (error)
{ {
ESP_LOGW(TAG, "Resolving failed: %s", error.message().c_str()); ESP_LOGW(TAG, "Resolving failed: %i %s", error.value(), error.message().c_str());
m_error = Error { .mesage = fmt::format("Resolving failed: {}", error.value(), error.message()) };
return; return;
} }
@ -84,7 +85,8 @@ void SslWebsocketClient::onConnected(const std::error_code &error)
{ {
if (error) if (error)
{ {
ESP_LOGW(TAG, "Connect failed: %s", error.message().c_str()); ESP_LOGW(TAG, "Connect failed: %i %s", error.value(), error.message().c_str());
m_error = Error { .mesage = fmt::format("Connect failed: {}", error.value(), error.message()) };
return; return;
} }
@ -108,7 +110,8 @@ void SslWebsocketClient::onHandshaked(const std::error_code &error)
{ {
if (error) if (error)
{ {
ESP_LOGW(TAG, "Handshake failed: %s", error.message().c_str()); ESP_LOGW(TAG, "SSL-Handshake failed: %i %s", error.value(), error.message().c_str());
m_error = Error { .mesage = fmt::format("SSL-Handshake failed: {}", error.value(), error.message()) };
return; return;
} }
@ -141,8 +144,10 @@ void SslWebsocketClient::onSentRequest(const std::error_code &error, std::size_t
{ {
if (error) if (error)
{ {
ESP_LOGW(TAG, "Write failed: %s", error.message().c_str()); ESP_LOGW(TAG, "Sending http request failed: %i %s", error.value(), error.message().c_str());
m_error = Error { .mesage = fmt::format("Sending http request failed: {}", error.value(), error.message()) };
m_sending = std::nullopt; m_sending = std::nullopt;
m_socket.shutdown();
return; return;
} }
@ -168,7 +173,9 @@ void SslWebsocketClient::onReceivedResponse(const std::error_code &error, std::s
{ {
if (error) if (error)
{ {
ESP_LOGI(TAG, "Read failed: %s", error.message().c_str()); ESP_LOGI(TAG, "Receiving http response failed: %i %s", error.value(), error.message().c_str());
m_error = Error { .mesage = fmt::format("Receiving http response failed: {}", error.value(), error.message()) };
m_socket.shutdown();
return; return;
} }
@ -236,27 +243,29 @@ bool SslWebsocketClient::parseResponseLine(std::string_view line)
if (const auto index = line.find(' '); index == std::string::npos) if (const auto index = line.find(' '); index == std::string::npos)
{ {
ESP_LOGW(TAG, "invalid response line (1): \"%.*s\"", line.size(), line.data()); ESP_LOGW(TAG, "invalid response line (1): \"%.*s\"", line.size(), line.data());
//m_socket.close(); m_error = Error { .mesage = fmt::format("invalid response line (1): \"{}\"", line) };
m_socket.shutdown();
return false; return false;
} }
else else
{ {
const std::string_view protocol { line.data(), index }; const std::string_view protocol { line.data(), index };
ESP_LOGV(TAG, "response protocol: %zd \"%.*s\"", protocol.size(), protocol.size(), protocol.data()); // ESP_LOGV(TAG, "response protocol: %zd \"%.*s\"", protocol.size(), protocol.size(), protocol.data());
if (const auto index2 = line.find(' ', index + 1); index2 == std::string::npos) if (const auto index2 = line.find(' ', index + 1); index2 == std::string::npos)
{ {
ESP_LOGW(TAG, "invalid request line (2): \"%.*s\"", line.size(), line.data()); ESP_LOGW(TAG, "invalid request line (2): \"%.*s\"", line.size(), line.data());
//m_socket.close(); m_error = Error { .mesage = fmt::format("invalid response line (2): \"{}\"", line) };
m_socket.shutdown();
return false; return false;
} }
else else
{ {
const std::string_view status { line.data() + index + 1, line.data() + index2 }; const std::string_view status { line.data() + index + 1, line.data() + index2 };
ESP_LOGV(TAG, "response status: %zd \"%.*s\"", status.size(), status.size(), status.data()); // ESP_LOGV(TAG, "response status: %zd \"%.*s\"", status.size(), status.size(), status.data());
const std::string_view message { line.cbegin() + index2 + 1, line.cend() }; const std::string_view message { line.cbegin() + index2 + 1, line.cend() };
ESP_LOGV(TAG, "response message: %zd \"%.*s\"", message.size(), message.size(), message.data()); // ESP_LOGV(TAG, "response message: %zd \"%.*s\"", message.size(), message.size(), message.data());
// ESP_LOGV(TAG, "state changed to ResponseHeaders"); // ESP_LOGV(TAG, "state changed to ResponseHeaders");
m_state = State::ResponseHeaders; m_state = State::ResponseHeaders;
@ -275,8 +284,9 @@ bool SslWebsocketClient::parseResponseHeader(std::string_view line)
constexpr std::string_view sep{": "}; constexpr std::string_view sep{": "};
if (const auto index = line.find(sep.data(), 0, sep.size()); index == std::string_view::npos) if (const auto index = line.find(sep.data(), 0, sep.size()); index == std::string_view::npos)
{ {
ESP_LOGW(TAG, "invalid request header: %zd \"%.*s\"", line.size(), line.size(), line.data()); ESP_LOGW(TAG, "invalid response header: %zd \"%.*s\"", line.size(), line.size(), line.data());
//m_socket.close(); m_error = Error { .mesage = fmt::format("invalid response header: \"{}\"", line) };
m_socket.shutdown();
return false; return false;
} }
else else
@ -292,7 +302,8 @@ bool SslWebsocketClient::parseResponseHeader(std::string_view line)
{ {
ESP_LOGW(TAG, "invalid Content-Length %.*s %.*s", value.size(), value.data(), ESP_LOGW(TAG, "invalid Content-Length %.*s %.*s", value.size(), value.data(),
parsed.error().size(), parsed.error().data()); parsed.error().size(), parsed.error().data());
//m_socket.close(); m_error = Error { .mesage = fmt::format("iinvalid Content-Length: \"{}\": {}", value, parsed.error()) };
m_socket.shutdown();
return false; return false;
} }
else else
@ -336,7 +347,7 @@ bool SslWebsocketClient::parseResponseHeader(std::string_view line)
else else
{ {
requestFinished: requestFinished:
ESP_LOGI(TAG, "finished"); // ESP_LOGV(TAG, "finished");
handleConnected(); handleConnected();
@ -364,7 +375,9 @@ void SslWebsocketClient::onReceiveWebsocket(const std::error_code &error, std::s
{ {
if (error) if (error)
{ {
ESP_LOGI(TAG, "error: %i %s", error.value(), error.message().c_str()); ESP_LOGI(TAG, "Receiving websocket response failed: %i %s", error.value(), error.message().c_str());
m_error = Error { .mesage = fmt::format("Receiving websocket response failed: {}", error.value(), error.message()) };
m_socket.shutdown();
return; return;
} }
@ -535,11 +548,13 @@ void SslWebsocketClient::sendMessage(bool fin, uint8_t reserved, uint8_t opcode,
} }
} }
void SslWebsocketClient::onMessageSent(std::error_code ec, std::size_t length) void SslWebsocketClient::onMessageSent(std::error_code error, std::size_t length)
{ {
if (ec) if (error)
{ {
ESP_LOGW(TAG, "error: %i %s", ec.value(), ec.message().c_str()); ESP_LOGI(TAG, "Sending websocket message failed: %i %s", error.value(), error.message().c_str());
m_error = Error { .mesage = fmt::format("Sending websocket message failed: {}", error.value(), error.message()) };
m_socket.shutdown();
m_sending = std::nullopt; m_sending = std::nullopt;
m_sendingQueue = {}; m_sendingQueue = {};
return; return;

View File

@ -8,6 +8,9 @@
#include <asio.hpp> #include <asio.hpp>
#include <asio/ssl.hpp> #include <asio/ssl.hpp>
// 3rdparty lib includes
#include <espchrono.h>
class SslWebsocketClient class SslWebsocketClient
{ {
public: public:
@ -17,8 +20,17 @@ public:
void start(); void start();
virtual void handleConnected() = 0; virtual void handleConnected() = 0;
virtual void handleDisconnected() = 0;
virtual void handleMessage(bool fin, uint8_t reserved, uint8_t opcode, bool mask, std::string_view payload) = 0; virtual void handleMessage(bool fin, uint8_t reserved, uint8_t opcode, bool mask, std::string_view payload) = 0;
struct Error {
espchrono::millis_clock::time_point timestamp = espchrono::millis_clock::now();
std::string mesage;
};
const std::optional<Error> &error() const { return m_error; }
void clearError() { m_error = std::nullopt; }
private: private:
void resolve(); void resolve();
void onResolved(const std::error_code &error, asio::ip::tcp::resolver::iterator iterator); void onResolved(const std::error_code &error, asio::ip::tcp::resolver::iterator iterator);
@ -40,7 +52,7 @@ public:
void sendMessage(bool fin, uint8_t reserved, uint8_t opcode, bool mask, std::string_view payload); void sendMessage(bool fin, uint8_t reserved, uint8_t opcode, bool mask, std::string_view payload);
private: private:
void onMessageSent(std::error_code ec, std::size_t length); void onMessageSent(std::error_code error, std::size_t length);
std::string m_host; std::string m_host;
std::string m_port; std::string m_port;
@ -61,4 +73,6 @@ private:
std::optional<std::string> m_sending; std::optional<std::string> m_sending;
std::queue<std::string> m_sendingQueue; std::queue<std::string> m_sendingQueue;
std::optional<Error> m_error;
}; };