Christopher Pisz
Christopher Pisz

Reputation: 4010

How to get the meaning of error codes from boost::filesystem::filesystem_error

I am considering switching from calling Windows to using boost::filesystem. However, the documentation really tells me next to nothing about how to get meaningful error information out of it.

For a quick example, I did the following

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());
}

e.code() gives me a value of 123 in the debugger. If I look up 123 in the windows header, it points me to the native error of ENOPROTOOPT and the boost error of no_protocol_option. That can't be right.

The message is somewhat useful and says "The filename, directory name, or volume label syntax is incorrect" However, I am not sure I should be relying on the message always being filled out or making sense. It could be alot better for this case and a switch statement + manual message seems appropriate.

What is the proper way to get meaningful error information out of boost::filesystem? Meaningful being both string messages and error codes that can be looked-up and compared against.

Edit: I've also found some older forum threads and articles that mention native_error(), however, the exception in version 1.62, in my debugger, does not appear to have any such method exposed.

Relevant Links I found: http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#Error-reporting

catching exception from boost::filesystem::is_directory

Upvotes: 1

Views: 5910

Answers (1)

Alexander
Alexander

Reputation: 1343

WinError.h says this:

//
// MessageId: ERROR_INVALID_NAME
//
// MessageText:
//
// The filename, directory name, or volume label syntax is incorrect.
//
#define ERROR_INVALID_NAME               123L    // dderror

Use errorCode.message(); as quotet, and you always get human-readable error description.

Upvotes: 2

Related Questions