DPM-Core/src/handlers.cpp

137 lines
5.0 KiB
C++
Raw Normal View History

#include "handlers.hpp"
// Helper function for validating required fields in a FlexDPMError
void validate_field(FlexDPMError context, const char* field_name, const void* field_value)
{
if (!field_value) {
std::cerr << "Error: Incomplete error context. Missing required field: " << field_name;
std::cerr << " (Error category: " << static_cast<int>(context.error) << ")" << std::endl;
// Hard exit when a required field is missing
exit(1);
}
}
// Main error handler that dispatches to specific handlers
int handle_error(FlexDPMError context) {
if (context.error == DPMErrorCategory::SUCCESS) {
return 0;
}
switch (context.error) {
case DPMErrorCategory::PATH_NOT_FOUND:
validate_field(context, "module_path", context.module_path);
return handle_path_not_found(context);
case DPMErrorCategory::PATH_NOT_DIRECTORY:
validate_field(context, "module_path", context.module_path);
return handle_path_not_directory(context);
case DPMErrorCategory::PATH_TOO_LONG:
validate_field(context, "module_path", context.module_path);
return handle_path_too_long(context);
case DPMErrorCategory::PERMISSION_DENIED:
validate_field(context, "module_path", context.module_path);
return handle_permission_denied(context);
case DPMErrorCategory::MODULE_NOT_FOUND:
validate_field(context, "module_name", context.module_name);
validate_field(context, "module_path", context.module_path);
return handle_module_not_found(context);
case DPMErrorCategory::MODULE_NOT_LOADED:
validate_field(context, "module_name", context.module_name);
return handle_module_not_loaded(context);
case DPMErrorCategory::MODULE_LOAD_FAILED:
validate_field(context, "module_name", context.module_name);
return handle_module_load_failed(context);
case DPMErrorCategory::INVALID_MODULE:
validate_field(context, "module_name", context.module_name);
return handle_invalid_module(context);
case DPMErrorCategory::SYMBOL_NOT_FOUND:
validate_field(context, "module_name", context.module_name);
return handle_symbol_not_found(context);
case DPMErrorCategory::SYMBOL_EXECUTION_FAILED:
validate_field(context, "module_name", context.module_name);
return handle_symbol_execution_failed(context);
case DPMErrorCategory::UNDEFINED_ERROR:
return handle_undefined_error(context);
default:
std::cerr << "Error: Unknown error code" << std::endl;
return 1;
}
}
// Now the individual handlers can be simplified since required fields are guaranteed
int handle_path_not_found( FlexDPMError context ) {
std::cerr << "Fatal error: The module directory '" << context.module_path << "' was not found. Exiting." << std::endl;
return 1;
}
int handle_path_not_directory( FlexDPMError context ) {
std::cerr << "Fatal error: The module path '" << context.module_path << "' is not a directory. Exiting." << std::endl;
return 1;
}
int handle_path_too_long( FlexDPMError context ) {
std::cerr << "Error: Module path is too long: '" << context.module_path << "'. Exiting." << std::endl;
return 1;
}
int handle_permission_denied( FlexDPMError context ) {
std::cerr << "Error: Permission denied accessing the modules path: '" << context.module_path << "'. Exiting." << std::endl;
return 1;
}
int handle_module_not_found( FlexDPMError context ) {
std::cerr << "Error: Module '"<< context.module_name << "' not found in '" << context.module_path << "'. Exiting." << std::endl;
return 1;
}
int handle_module_not_loaded( FlexDPMError context ) {
std::cerr << "Error: Attempted to execute module before loading it: " << context.module_name << std::endl;
return 1;
}
int handle_module_load_failed( FlexDPMError context ) {
std::cerr << "Error: Failed to load module: " << context.module_name << std::endl;
return 1;
}
int handle_invalid_module( FlexDPMError context ) {
std::cerr << "Error: Invalid module format: " << context.module_name << std::endl;
return 1;
}
int handle_symbol_not_found( FlexDPMError context ) {
std::cerr << "Error: Symbol not found in module: " << context.module_name;
if (context.message) {
std::cerr << " (" << context.message << ")";
}
std::cerr << std::endl;
return 1;
}
int handle_symbol_execution_failed(FlexDPMError context) {
std::cerr << "Error: Module execution failed: " << context.module_name << std::endl;
return 1;
}
int handle_undefined_error(FlexDPMError context) {
std::cerr << "Error: Undefined error occurred";
if (context.module_name) {
std::cerr << " with module: " << context.module_name;
}
if (context.message) {
std::cerr << " (" << context.message << ")";
}
std::cerr << std::endl;
return 1;
}