function::user_ulong_error - Linux
Overview
function::user_ulong_error generates a user-defined error code with an unsigned long field.
Syntax
function::user_ulong_error(error_code_or_condition, domain, number, msg, ulong_code)
Options/Flags
- error_code_or_condition: The error code (e.g.,
std::errc::invalid_argument
) or asystem_error
condition. - domain: The error domain.
- number: The error number within the specified error domain.
- msg: The human-readable error message.
- ulong_code: The unsigned long error code to be associated with the generated error.
Examples
Simple Example:
auto ec = function::user_ulong_error(std::errc::invalid_argument, std::generic_category(), 42, "Invalid arguments", 123456789);
Complex Example with Macros:
#include <concepts/concepts.hpp>
#include <functional/error.hpp>
namespace fs = std::filesystem;
namespace custom {
constexpr auto custom_domain = std::error_domain("custom.domain");
namespace error {
constexpr auto invalid_path = MAKE_USER_ULONG_ERROR_CODE(custom_domain, 1, "Invalid path");
} // namespace error
} // namespace custom
int main() {
const fs::path invalid_path("/non/existing/path");
try {
fs::remove(invalid_path);
} catch (const std::exception& e) {
if (auto ec = concepts::unexpected_error(e)) {
if (ec == custom::error::invalid_path) {
std::cerr << "Invalid path: " << ec.message() << std::endl;
return ec.value();
}
}
std::rethrow_exception(e);
}
}
Common Issues
- Ensure that the error domain is unique to avoid conflicts with other domains.
- Use descriptive error messages to aid in debugging.
- Check for the existence of the unsigned long error code field in the error handling code.
Integration
- Combine with
concepts::unexpected_error()
to extract error codes from exceptions. - Integrate with
custom::error_code_traits
for advanced error handling and customization.