Discussion:
boost::filesystem getting error info
(too old to reply)
Christopher J. Pisz
2016-11-09 03:46:03 UTC
Permalink
Was looking at boost::filesystem to replace some Windows specific stuff
I have.

It isn't very clear on what what wrong if you catch a filesystem exception.

For example, calling boost::filesystem::create_directories("?!#garbage?!#);

throws an exception where, afaict the only data available is the path
argument. It has a what(), but it is an empty string.

Am I missing some manner of enlightening information?
Öö Tiib
2016-11-09 10:10:45 UTC
Permalink
Post by Christopher J. Pisz
Was looking at boost::filesystem to replace some Windows specific stuff
I have.
It isn't very clear on what what wrong if you catch a filesystem exception.
For example, calling boost::filesystem::create_directories("?!#garbage?!#);
throws an exception where, afaict the only data available is the path
argument. It has a what(), but it is an empty string.
Am I missing some manner of enlightening information?
May be you miss that information from its documentation:
http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#filesystem_error-members

May be you miss way how to reach such information (I used Google
search).
If that information does not help you then please be more specific.
Öö Tiib
2016-11-09 10:16:11 UTC
Permalink
Post by Öö Tiib
Post by Christopher J. Pisz
Was looking at boost::filesystem to replace some Windows specific stuff
I have.
It isn't very clear on what what wrong if you catch a filesystem exception.
For example, calling boost::filesystem::create_directories("?!#garbage?!#);
throws an exception where, afaict the only data available is the path
argument. It has a what(), but it is an empty string.
Am I missing some manner of enlightening information?
http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#filesystem_error-members
On second thought it can also be that you don't know that information:
http://en.cppreference.com/w/cpp/error/system_error/code
Christopher J. Pisz
2016-11-09 15:05:55 UTC
Permalink
Post by Öö Tiib
Post by Öö Tiib
Post by Christopher J. Pisz
Was looking at boost::filesystem to replace some Windows specific stuff
I have.
It isn't very clear on what what wrong if you catch a filesystem exception.
For example, calling boost::filesystem::create_directories("?!#garbage?!#);
throws an exception where, afaict the only data available is the path
argument. It has a what(), but it is an empty string.
Am I missing some manner of enlightening information?
http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#filesystem_error-members
http://en.cppreference.com/w/cpp/error/system_error/code
In this example:

try
{
// Creates all directories in the path if they do not exist
boost::filesystem::create_directories("!?#Gibberish!?#");
}
catch(boost::filesystem::filesystem_error & e)
{
// Not very clear on how to get meaningful information from the
exception
// The codes are found in boost::system::errc::<your code here>
// Try and get the value and then find the Windows codes mapped to
the boost codes?
// The actual numeric value can be found in the header with the
Windows codes - errno.h under _CRT_NO_POSIX_ERROR_CODES?
//
// You'll have to compare against specific ones and make your own
meaningful error message?
const boost::system::error_code errorCode = e.code();

std::ostringstream msg;
msg << "boost::filesystem::create_directories failed with error
code: " << errorCode.message();


// Use our own exception type
throw Common::Exception(__FILE__, __LINE__, msg.str());
}


I see the members, but I get values 123. Where is a table to lookup what
123 means? If I dig through the headers, it takes me to errno.h on
Windows and points to ENOPROTOOPT, which makes no sense.

"The filename, directory name, or volume label syntax is incorrect" is a
meaningful string, but it could be alot better. I am going to want to
make a switch, handle specific codes, and write custom messages, but the
documentation is not telling me where to look up the the codes.
Öö Tiib
2016-11-09 20:01:08 UTC
Permalink
Post by Christopher J. Pisz
Post by Öö Tiib
Post by Öö Tiib
Post by Christopher J. Pisz
Was looking at boost::filesystem to replace some Windows specific stuff
I have.
It isn't very clear on what what wrong if you catch a filesystem exception.
For example, calling boost::filesystem::create_directories("?!#garbage?!#);
throws an exception where, afaict the only data available is the path
argument. It has a what(), but it is an empty string.
Am I missing some manner of enlightening information?
http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#filesystem_error-members
http://en.cppreference.com/w/cpp/error/system_error/code
try
{
// Creates all directories in the path if they do not exist
boost::filesystem::create_directories("!?#Gibberish!?#");
}
catch(boost::filesystem::filesystem_error & e)
{
// Not very clear on how to get meaningful information from the
exception
// The codes are found in boost::system::errc::<your code here>
// Try and get the value and then find the Windows codes mapped to
the boost codes?
// The actual numeric value can be found in the header with the
Windows codes - errno.h under _CRT_NO_POSIX_ERROR_CODES?
//
// You'll have to compare against specific ones and make your own
meaningful error message?
const boost::system::error_code errorCode = e.code();
std::ostringstream msg;
msg << "boost::filesystem::create_directories failed with error
code: " << errorCode.message();
// Use our own exception type
throw Common::Exception(__FILE__, __LINE__, msg.str());
}
That example does not compile as C++. Top level try?
Post by Christopher J. Pisz
I see the members, but I get values 123. Where is a table to lookup what
123 means? If I dig through the headers, it takes me to errno.h on
Windows and points to ENOPROTOOPT, which makes no sense.
"The filename, directory name, or volume label syntax is incorrect" is a
meaningful string, but it could be alot better. I am going to want to
make a switch, handle specific codes, and write custom messages, but the
documentation is not telling me where to look up the the codes.
I thought that error codes from boost are those:
http://www.boost.org/doc/libs/1_61_0/libs/system/doc/reference.html#Header-error_code

Value 123 looks overly large but it iss unsure how you got it. If you
can't post real code then perhaps use debugger or something?
Christopher J. Pisz
2016-11-09 20:45:09 UTC
Permalink
Post by Öö Tiib
That example does not compile as C++. Top level try?
#include <sstream>
#include <boost/filesystem.hpp>

//--------------------------------------------------------------------------------------------------
int main(int argc, char **argv)
{
try
{
// Creates all directories in the path if they do not exist
boost::filesystem::create_directories("!?#Gibberish!?#");
}
catch(boost::filesystem::filesystem_error & e)
{
// Not very clear on how to get meaningful information from
the exception
// The codes are found in boost::system::errc::<your code here>
// Try and get the value and then find the Windows codes
mapped to the boost codes?
// The actual numeric value can be found in the header with
the Windows codes - errno.h under _CRT_NO_POSIX_ERROR_CODES?
//
// You'll have to compare against specific ones and make your
own meaningful error message?
const boost::system::error_code errorCode = e.code();

std::ostringstream msg;
msg << "boost::filesystem::create_directories failed with
error code: " << errorCode.message();

// Use our own exception type
throw std::runtime_error(msg.str());
}

return 0;
}
Post by Öö Tiib
http://www.boost.org/doc/libs/1_61_0/libs/system/doc/reference.html#Header-error_code
That's what I thought looking through the header, but "no socket option"
makes no sense.

It appears to be these codes
https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx

but I want to make sure. IT doesn't mention it in the docs at all.
Öö Tiib
2016-11-10 07:01:48 UTC
Permalink
Post by Christopher J. Pisz
Post by Öö Tiib
That example does not compile as C++. Top level try?
#include <sstream>
#include <boost/filesystem.hpp>
//--------------------------------------------------------------------------------------------------
int main(int argc, char **argv)
{
try
{
// Creates all directories in the path if they do not exist
boost::filesystem::create_directories("!?#Gibberish!?#");
}
catch(boost::filesystem::filesystem_error & e)
{
// Not very clear on how to get meaningful information from
the exception
// The codes are found in boost::system::errc::<your code here>
// Try and get the value and then find the Windows codes
mapped to the boost codes?
// The actual numeric value can be found in the header with
the Windows codes - errno.h under _CRT_NO_POSIX_ERROR_CODES?
//
// You'll have to compare against specific ones and make your
own meaningful error message?
const boost::system::error_code errorCode = e.code();
std::ostringstream msg;
msg << "boost::filesystem::create_directories failed with
error code: " << errorCode.message();
// Use our own exception type
throw std::runtime_error(msg.str());
}
return 0;
}
Post by Öö Tiib
http://www.boost.org/doc/libs/1_61_0/libs/system/doc/reference.html#Header-error_code
That's what I thought looking through the header, but "no socket option"
makes no sense.
It appears to be these codes
https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx
but I want to make sure. IT doesn't mention it in the docs at all.
Yep, it seems weakly documented so I started to wonder why it works
in my projects as platform agnostic. :D Appears that it depends on error
category and different error codes of different categories may be
converted or may compare equal. My bots found such mock/hacking tool
from internet somewhere:

#include <iostream>
#include <boost/asio/error.hpp>
#include <boost/filesystem.hpp>
#include <boost/system/error_code.hpp>

int main()
{
// Two different error codes.
boost::system::error_code code1 = make_error_code(
boost::system::errc::no_such_file_or_directory);
boost::system::error_code code2 = make_error_code(
boost::asio::error::host_not_found_try_again);

// That have different error categories.
assert(code1.category() != code2.category());
assert(code1.default_error_condition().category() !=
code2.default_error_condition().category());

// Yet have the same value.
assert(code1.value() == code2.value());
assert(code1.default_error_condition().value() ==
code2.default_error_condition().value());

// Use the comparision operation to check both value
// and category.
assert(code1 != code2);
assert(code1.default_error_condition() !=
code2.default_error_condition());

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test with Boost.Filesytem
try
{
boost::filesystem::canonical("bogus_file");
}
catch(boost::filesystem::filesystem_error& error)
{
if (error.code() ==
make_error_code(boost::system::errc::no_such_file_or_directory))
{
std::cout << "No file or directory" << std::endl;
}
if (error.code() ==
make_error_code(boost::asio::error::host_not_found_try_again))
{
std::cout << "Host not found" << std::endl;
}
}
}

Loading...