Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Compare boost::system::error_category

321 views
Skip to first unread message

Christopher Pisz

unread,
Mar 23, 2017, 3:09:07 PM3/23/17
to
The following comparison fails for an error whom outputs "asio.misc" for errorCode.category().name() and "end of file" for errorCode.message()

If it claims to be in category asio.misc, then why does the if condition of (errorCode.category() == boost::asio::error::misc_category ) evaluate to false?

Googling says that a boost::system::error_code can have the same value in more than one category, so I assume that in order to get the proper message and meaning we must compare boost::system::error_category as well as boost::system::error_code::value.

How do we properly compare the category if this fails to work?

Code in question:
//--------------------------------------------------------------------------------------------------
std::string ClientSocketASIO::ErrorCodeToString(const boost::system::error_code & errorCode)
{
std::ostringstream debugMsg;
debugMsg << " Error Category: " << errorCode.category().name() << ". "
<< " Error Message: " << errorCode.message() << ". ";

if( errorCode.category() == boost::asio::error::misc_category )
{
switch (errorCode.value())
{
case boost::asio::error::eof:
debugMsg << ". Server has disconnected.";
break;
case boost::asio::error::connection_refused:
debugMsg << ". Connection Refused";
break;
default:
debugMsg << ". Unknown Error.";
break;
}
}
else
{
debugMsg << ". Unknown Error category.";
}

return debugMsg.str();
}

Daniel

unread,
Mar 23, 2017, 10:53:59 PM3/23/17
to
On Thursday, March 23, 2017 at 3:09:07 PM UTC-4, Christopher Pisz wrote:
> The following comparison fails for an error whom outputs "asio.misc" for errorCode.category().name() and "end of file" for errorCode.message()
>
> If it claims to be in category asio.misc, then why does the if condition of (errorCode.category() == boost::asio::error::misc_category ) evaluate to false?
>

Apart from the category issues, if you're getting "end of file" for
errorCode.message(), I don't think you'll get any more information by
examining the error code. The misc_errors enum value corresponding to that
message is misc_errors::eof.

What version of boost are you using? At one time (v 1.4) there actually was a
boost::system::misc_category, now (v 6.2) there's this for backwards
compatibility:

static const boost::system::error_category& misc_category
= boost::asio::error::get_misc_category();

I believe all the error codes are now covered by
boost::system::error_category.

If you're using a recent version of boost, I would not have expected you
would see an error category of "asio.misc". For recent versions, I would
expect (errorCode.category() == boost::asio::error::get_system_category())

Daniel

Daniel

unread,
Mar 23, 2017, 11:47:40 PM3/23/17
to
On Thursday, March 23, 2017 at 10:53:59 PM UTC-4, Daniel wrote:
>
> I believe all the error codes are now covered by
> boost::system::error_category.
>
All the error codes required by boost asio, that is.

Daniel

Christopher Pisz

unread,
Mar 24, 2017, 10:43:26 AM3/24/17
to
I am using 1.62.0

According to what I am reading, anyone is free to create their own error codes and categories. Different boost libraries may use different categories. Just debugging through various network scenarios and see that the error_code::category is sometimes boost::asio::error::misc_category and sometimes boost::system::system_category.

I finally got onto another computer that wasn't blocking sites and at least one fella on the boost mailing list claims you should indeed compare category, but the comparison fails when boost is statically linked because one address is from one lib and the other is from the executable. That seems like one silly comparison operator...


Daniel

unread,
Mar 24, 2017, 11:11:26 AM3/24/17
to
On Friday, March 24, 2017 at 10:43:26 AM UTC-4, Christopher Pisz wrote:
>
> Different boost libraries may use different categories. Just debugging
> through various network scenarios and see that the error_code::category is > sometimes boost::asio::error::misc_category and sometimes
> boost::system::system_category.
>
Right, but I'm pretty sure that boost::asio::error::misc_category is
there for backwards compatibility only. Compare error.hpp in 1.62 and 1.43.
There used to be a misc_category that extended boost::system::error_category,
but now there is only boost::system::error_category.

> I finally got onto another computer that wasn't blocking sites and at least
> one fella on the boost mailing list claims you should indeed compare
> category, but the comparison fails when boost is statically linked because
> one address is from one lib and the other is from the executable.

You should be comparing with boost::asio::error::get_misc_category()
rather than the data member boost::asio::error::misc_category, does it
change anything if you do?

Also, note that if errorCode.message() returned a message, it was able to
resolve the category.

Daniel




Daniel

unread,
Mar 24, 2017, 12:46:11 PM3/24/17
to
On Friday, March 24, 2017 at 11:11:26 AM UTC-4, Daniel wrote:
> On Friday, March 24, 2017 at 10:43:26 AM UTC-4, Christopher Pisz wrote:
> >
> > I finally got onto another computer that wasn't blocking sites and at
> > least one fella on the boost mailing list claims you should indeed
> > compare category, but the comparison fails when boost is statically
> > linked because one address is from one lib and the other is from the
> > executable.
>
> You should be comparing with boost::asio::error::get_misc_category()
> rather than boost::asio::error::misc_category, does it
> change anything if you do?
>
Just to note, in 1.62, get_misc_category() is implemented in the boost
library, not the header files, so I would expect it to return a consistent
address.

Daniel


Christopher Pisz

unread,
Mar 24, 2017, 4:53:31 PM3/24/17
to
I compared &(errorCode.category()) and &boost::asio::error::misc_category and they are indeed different even though errorCode.category().name() outputs "asio.misc", so I don't know wtf is going on.

I also see that
if (errorCode == make_error_code(boost::asio::error::connection_refused))

Evaluates to true when I dynamically link and false when I statically link.

Daniel

unread,
Mar 24, 2017, 6:39:32 PM3/24/17
to
On Friday, March 24, 2017 at 4:53:31 PM UTC-4, Christopher Pisz wrote:
>
> I compared &(errorCode.category()) and &boost::asio::error::misc_category and they are indeed different even though errorCode.category().name() outputs "asio.misc", so I don't know wtf is going on.
>
Did you try comparing errorCode.category() and boost::asio::error::get_misc_category() (using the function call)

Christopher Pisz

unread,
Mar 27, 2017, 10:17:42 AM3/27/17
to
Yes. It failed.
Evidently, boost::system::error_category has all kinds of problems.

Ongoing discussion about it here on the boost mailing list
http://boost.2283326.n4.nabble.com/Compare-boost-system-error-category-td4692861.html#a4692910

I really don't follow what they are talking about. Comparing addresses seems silly to me, period.

Daniel

unread,
Mar 27, 2017, 10:57:21 AM3/27/17
to
On Monday, March 27, 2017 at 10:17:42 AM UTC-4, Christopher Pisz wrote:
>
> Comparing addresses seems silly to me, period.

I agree. Comparisons should be with names.

Unfortunately that approach made it into C++ 11 with std::error_code.

Daniel

0 new messages