I am making a c++ wxwidgets application that has network connectivity, which connects to another process which is the server outside of my own program. Since I can't control the time it takes to the server for starting up, I want to when an exception arise because the server is not ready yet, my program should read the exception string to check if the status code is 0, so instead of showing the usual message, I want to add a message of wait a few memoents because of the server is starting or similar message:
This is my current code:
try
{
// work with the server
}
catch (const std::exception& e)
{
wxLogError("HTTP GET Error: %s", e.what());
wxMessageBox(e.what(), "Error", wxOK | wxICON_ERROR);
}
And this code pop-up two message boxes, one from the wxLogError and the other from the wxMessageBox, should I just keep one, instead? if wxLogError always pop-up an error, maybe using it instead of the message box.
The two message boxes that appear have this format: (First line is title, second line is message body)
1º Message box
Ollamawx Error
HTTP GET Error: HTTP GET request failed with status: 0
2º Message box
Error
HTTP GET request failed with status: 0
Assistant
Yes, showing two message boxes for the same error can be redundant and may confuse the user. You should streamline the way you handle and display error messages. Using wxLogError or wxMessageBox alone would be sufficient in most cases. Here’s an approach where you read the exception string to check for a status code of 0 and adjust the error message accordingly.
First, let's inspect whether the exception's message contains "status: 0". If it does, we'll provide a more informative message. You can use wxLogError or wxMessageBox according to the level of user interaction you prefer:
Using wxLogError Only:
cpp
try
{
// work with the server
}
catch (const std::exception& e)
{
std::string errorMsg = e.what();
if (errorMsg.find("status: 0") != std::string::npos) {
wxLogError("HTTP GET Error: Server is starting up, please wait a few moments.");
} else {
wxLogError("HTTP GET Error: %s", e.what());
}
}
Using wxMessageBox Only:
cpp
try
{
// work with the server
}
catch (const std::exception& e)
{
std::string errorMsg = e.what();
if (errorMsg.find("status: 0") != std::string::npos) {
wxMessageBox("Server is starting up, please wait a few moments.", "Error", wxOK | wxICON_ERROR);
} else {
wxMessageBox(e.what(), "Error", wxOK | wxICON_ERROR);
}
}
Mixing Both Approaches:
cpp
try
{
// work with the server
}
catch (const std::exception& e)
{
std::string errorMsg = e.what();
if (errorMsg.find("status: 0") != std::string::npos) {
wxLogError("Server is starting up, please wait a few moments.");
} else {
wxLogError("HTTP GET Error: %s", e.what());
}
wxMessageBox(errorMsg, "Error", wxOK | wxICON_ERROR);
}
Among the three options above, if you decide to use both wxLogError and wxMessageBox, ensure that the wxMessageBox only displays unique or consolidated information to avoid redundancy.
Recommended Approach:
For a typical user interface scenario where both logging and user alerts are necessary, using wxMessageBox is often more direct for the user. Logging can be useful for developers and debugging. In that case, you may prefer the first or second approach based on your need.
Here's a revised example using wxMessageBox which is user-friendly:
cpp
try
{
// work with the server
}
catch (const std::exception& e)
{
std::string errorMsg = e.what();
if (errorMsg.find("status: 0") != std::string::npos) {
wxMessageBox("Server is starting up, please wait a few moments.", "Error", wxOK | wxICON_ERROR);
} else {
wxMessageBox(errorMsg, "Error", wxOK | wxICON_ERROR);
}
}
This way, you provide a clear and user-friendly message, especially when the server isn't ready yet, while keeping the handling streamlined and efficient.