Integrate with C++11 system_error (see PR #27)
EvanBalster opened this issue · comments
(Lodging an issue for this; I might be inclined to develop a pull request.)
C++11 introduces an error_code class that can accommodate both POSIX standard error codes and alternative collections of error codes such as NNG's. An error_code includes an integer value (0 for no error) and an error category which can be either the C++/POSIX one or some custom category. nngpp would benefit from implementing a custom error category.
https://en.cppreference.com/w/cpp/error/error_category
Implementations of std::error_category have a few responsibilities: Categorizing error codes, providing string representations, and mapping platform-dependent codes to platform-agnostic ones (including standard C++/POSIX error codes). These functions serve to make codebases more consistent and integrable; for example, libasio uses std::error_code extensively.
I recommend making nng::error convertible to std::error_code
and/or adding a method for doing so to nng::exception
.
The POSIX error codes supported by C++11 are listed here: https://en.cppreference.com/w/cpp/error/errc
The following codes have no direct mapping to a C++/POSIX equivalent.
NNG CODE | possible C++ mapping |
---|---|
ECLOSED | |
ESTATE | |
EUNREACHABLE, EADDRINVAL | address_not_available |
ENOFILES | |
EREADONLY / EWRITEONLY | |
ECRYPTO / EPEERAUTH | |
ENOARG / EAMBIGUOUS / EBADTYPE | |
ECONNSHUT | connection_aborted |
EINTERNAL | |
ESYSERR | |
ETRANERR |
These codes would need to be either lossily mapped to C++ codes or implemented as an NNG-specific error_condition
. It would be somewhat advantageous to assign them values that don't conflict with the standard C++ ones.
I'm working on a pull request which adds support for <system_error>
to error.h
without modifying or removing any current functionality.
These are my notes as I attempt to complete the mapping to generic_error_category based on the POSIX standard and a partial mapping in NNG's nn.c
.
NNG CODE | POSIX | C++ errc | Basis of mapping |
---|---|---|---|
ECLOSED | EBADF | bad_file_descriptor | nn.c |
ESTATE | ENOTTY | inappropriate_io_control_operation | POSIX (nn.c uses EFSM) |
EUNREACHABLE | EHOSTUNREACH | host_unreachable | nn.c |
EADDRINVAL | EADDRNOTAVAIL | address_not_available | POSIX (nn.c uses EINVAL) |
ENOFILES | EMFILE | too_many_files_open | nn.c |
EREADONLY / EWRITEONLY | †EACCES | permission_denied | nn.c |
ECRYPTO / EPEERAUTH | †EACCES | permission_denied | nn.c |
ENOARG | †EINVAL | invalid_argument | "option requires argument" |
EAMBIGUOUS | †EINVAL | invalid_argument | nn.c |
EBADTYPE | †EINVAL | invalid_argument | nn.c |
ECONNSHUT | †ECONNABORTED | connection_aborted | closest available |
EINTERNAL | — | — | NNG-specific |
ESYSERR (flag) | — | — | platform-specific |
ETRANERR (flag) | — | — | transport-specific |
† "Lossy" mappings; multiple NNG error codes would map to one POSIX error in default_error_condition
.