Skip to content

Commit

Permalink
* Improved error message stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
iProgramMC committed Jun 13, 2024
1 parent df54c58 commit bf5d081
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/discord/DiscordInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,8 @@ void DiscordInstance::HandleRequest(NetRequest* pRequest)
break;
}

str = "The following resource " + pRequest->url + suffix;
str = "The following resource " + pRequest->url + suffix + "\n\nAdditionally, the server responded with the following:\n" +
pRequest->response;
bExitAfterError = false;
bShowMessageBox = true;
break;
Expand All @@ -572,7 +573,7 @@ void DiscordInstance::HandleRequest(NetRequest* pRequest)
case HTTP_BADREQUEST:
{
str = "A bug occurred and the client has sent an invalid request.\n"
"The resource in question is: " + pRequest->url + "\n\n";
"The resource in question is: " + pRequest->url;
bExitAfterError = false;
bShowMessageBox = true;
break;
Expand All @@ -581,7 +582,7 @@ void DiscordInstance::HandleRequest(NetRequest* pRequest)
case HTTP_TOOMANYREQS:
{
str = "You're issuing requests too fast! Try again later. Maybe grab a seltzer and calm down.\n"
"The resource in question is: " + pRequest->url + "\n\n";
"The resource in question is: " + pRequest->url;
bExitAfterError = false;
bShowMessageBox = true;
break;
Expand All @@ -592,8 +593,8 @@ void DiscordInstance::HandleRequest(NetRequest* pRequest)
break;

default:
str = "Unknown HTTP code " + std::to_string(pRequest->result) + ".\n"
"The resource in question is: " + pRequest->url + "\n\n" + pRequest->response;
str = "Unknown HTTP code " + std::to_string(pRequest->result) + " (" + pRequest->ErrorMessage() + ".\n"
"URL requested: " + pRequest->url + "\n\nResponse:" + pRequest->response;
bExitAfterError = false;
bShowMessageBox = true;
break;
Expand Down
5 changes: 5 additions & 0 deletions src/discord/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ NetRequest::NetRequest(
pFunc = _func;
}

std::string NetRequest::ErrorMessage() const
{
return GetHTTPClient()->ErrorMessage(result);
}

void HTTPClient::DefaultRequestHandler(NetRequest* pRequest)
{
GetFrontend()->OnRequestDone(pRequest);
Expand Down
3 changes: 3 additions & 0 deletions src/discord/HTTPClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ struct NetRequest
uint8_t* _bytes = nullptr,
size_t _size = 0
);

std::string ErrorMessage() const;
};

class HTTPClient
Expand All @@ -111,6 +113,7 @@ class HTTPClient
virtual void Kill() = 0;
virtual void StopAllRequests() = 0;
virtual void PrepareQuit() = 0;
virtual std::string ErrorMessage(int code) const = 0;

// Sends a request via this HTTP client. If interactive, is prioritized.
// Data from stream_bytes is copied if needed.
Expand Down
11 changes: 7 additions & 4 deletions src/windows/NetworkerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ bool NetworkerThread::ProcessResult(NetRequest& req, const httplib::Result& res)
else
{
req.result = res->status;
if (res->status == HTTP_OK)
req.response = res->body;
else
req.response = std::string(detail::status_message(res->status));
req.response = res->body;
}

// Call the handler function.
Expand All @@ -144,6 +141,12 @@ bool NetworkerThread::ProcessResult(NetRequest& req, const httplib::Result& res)
return false;
}

std::string NetworkerThreadManager::ErrorMessage(int code) const
{
if (code < 0) return "Client Error";
return std::string(httplib::detail::status_message(code));
}

void NetworkerThread::IdleWait()
{
Sleep(100);
Expand Down
2 changes: 2 additions & 0 deletions src/windows/NetworkerThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class NetworkerThreadManager : public HTTPClient
size_t stream_size = 0
) override;

std::string ErrorMessage(int errorCode) const;

private:
NetworkerThread* m_pNetworkThreads[C_AMT_NETWORKER_THREADS] = { nullptr };

Expand Down

0 comments on commit bf5d081

Please sign in to comment.