I am trying to use the system_error facility to handle errors in a library of mine. I am going to briefly discuss the structure of the library in case you find it helpful: The namespace of the library is called commons and under this I have another namespace called dynlib. dynlib contains classes that are responsible for loading .so/.dll files:
namespace commons { namespace dynlib { class DynLibLoader { }; } } The errors that may occur in the DynLibLoader are LibraryFailedToLoad, LibraryFailedToUnload and SymbolNotFound. So my thoughts for handling the errors are the following: I will add a namespace error under the namespace dynlib. Then, under that namespace I will define one enum for std::error_codes and one enum for std::error_conditions. From my understanding the std::error_codes have to correspond to the value of errno (Linux) or GetLastError (Win32), and the std::error_conditions to values like LibraryFailedToLoad, SymbolNotFound etc. So, here are my questions:
- Is my understanding about
std::error_codeandstd::error_conditioncorrect? - How I am supposed to know all the possible values of
errnoandGetLastError()in order to define them under mystd::error_codesenum? What if Microsoft adds extra error values to the API in the future? Will I have to go back to the source code and define them under the enum I have for thestd::error_codes? - What if we are on another platform and there is no way to figure out the exact system error code when an error occurs?
- What if I want to have the same
std::error_codesfor the entire commons namespace and only define a differentstd::error_conditionfor each sub-namespace likedynlib. Is this a good practice? I would say yes because this will avoid duplicate code. But is there a catch behind this? - At the moment I am using a single
std::error_categoryfor each sub-namespace of commons. Is this a good practice? Do you think I should use thestd::error_categorydifferently?