Skip to content

Commit

Permalink
Implement global exception handling and improve error logging
Browse files Browse the repository at this point in the history
- Added a terminate handler to log unhandled exceptions and abort the program gracefully.
- Introduced a Windows-specific exception handler to log detailed information about unhandled exceptions on Windows systems.
- Updated the main function to set the terminate handler and Windows exception filter, enhancing the robustness of the application.

These changes improve error handling and logging capabilities, ensuring better diagnostics and stability during runtime.
  • Loading branch information
Joachim Rosskopf committed Jan 17, 2025
1 parent d347cf5 commit e7f9d00
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include <argparse/argparse.hpp>
#include <exception>
#include <iostream>
#include <cstdlib>

#ifdef _WIN32
#include <windows.h>
#endif

#include "api_server.hpp"
#include "auth_middleware.hpp"
Expand Down Expand Up @@ -42,8 +48,38 @@ void initializeDatabase(std::shared_ptr<ConfigManager> config_manager) {
}
}

void terminateHandler() {
CROW_LOG_ERROR << "Unhandled exception caught! flapi is giving up :-(";

auto ex = std::current_exception();
try {
std::rethrow_exception (ex);
} catch (const std::exception& e) {
CROW_LOG_ERROR << "exception caught: " << e.what();
}
std::abort();
}

#ifdef _WIN32

LONG WINAPI windowsExceptionHandler(EXCEPTION_POINTERS* exceptionInfo) {
CROW_LOG_ERROR << "Unhandled Windows exception caught!";
CROW_LOG_ERROR << "Exception code: " << std::hex << exceptionInfo->ExceptionRecord->ExceptionCode;
CROW_LOG_ERROR << "Address: " << exceptionInfo->ExceptionRecord->ExceptionAddress;

// Return control to Windows
return EXCEPTION_EXECUTE_HANDLER;
}

#endif

int main(int argc, char* argv[])
{
std::set_terminate(terminateHandler);
#ifdef _WIN32
SetUnhandledExceptionFilter(windowsExceptionHandler);
#endif

static argparse::ArgumentParser program("flapi");

program.add_argument("-c", "--config")
Expand Down

0 comments on commit e7f9d00

Please sign in to comment.