Exception In boost-beast

1k views Asked by At

I use Boost::Beast and i got somme exception message for exemple : resolve: No such host is known. I want in my code to print i message more significant that can a simple user get a idea what this message means . how i can do that?

that is the exemple that i chose to work with in the first but thy don't work for me

 if (ec && ec != beast::errc::not_connected) {
      beast::system_error{ ec };
      std::cout << "beast::errc::not_connected" << ec << " with explanatory message " << ec.message() << std::endl;
    }
    if (ec && ec != beast::errc::no_buffer_space) {
      beast::system_error{ ec };
      std::cout << "beast::errc::no_buffer_space" << ec << " with explanatory message " << ec.message() << std::endl;

    }
    if (ec && ec != beast::errc::timed_out) {
      beast::system_error{ ec };
      std::cout << "beast::errc::timed_out" << ec << " with explanatory message " << ec.message() << std::endl;

    }
    if (ec && ec != beast::errc::no_buffer_space) {
      beast::system_error{ ec };
      std::cout << "beast::errc::no_buffer_space" << ec << " with explanatory message " << ec.message() << std::endl;

    }
    if (ec && ec != beast::errc::stream_timeout) {
      beast::system_error{ ec };
      std::cout << "beast::errc::stream_timeout" << ec << " with explanatory message " << ec.message() << std::endl;

    }
1

There are 1 answers

1
sehe On

As pointed out by others, ec && ec != CODE needed to be ec && ec == CODE.

However, that conjunction is redundant: ec && ec == CODE is equivalent to ec == CODE (unless !ec.failed()).

I don't think any of the conditions are required:

    if (ec) {
        std::cout << ec.message() << "\n";
    }

That just prints the friendly message for any error:

Live On Coliru

#include <boost/beast.hpp>
#include <iostream>
namespace beast = boost::beast;

int main() {
    for (auto code : {
             beast::errc::not_connected,
             beast::errc::no_buffer_space,
             beast::errc::timed_out,
             beast::errc::no_buffer_space,
             beast::errc::stream_timeout,
         })
    {
        boost::system::error_code ec = make_error_code(code);
        if (ec) {
            std::cout << ec.message() << "\n";
        }
    }
}

Prints

Transport endpoint is not connected
No buffer space available
Connection timed out
No buffer space available
Timer expired

To the gist of the question

There will not be friendlier error descriptions for "your users" as they will always depend on your program's logic and domain. So it's on the programmer to translate any non-recoverable errors into user-friendly conditions.

E.g. there cannot ever be a useful "end-user" description for no_buffer_space since ... you know buffers interest programmers only. You might translate that into something like "System load exceeded" or "Simulation too large" or "Attachment is too large".