#ifndef ERR_H #define ERR_H #include #include #include "win32.h" enum ErrType : unsigned char { GENERIC, WINDOWS, WININET, LIBXML2 }; struct Err { std::wstring what; Err(ErrType t, const wchar_t* fmt = L"%s"); inline Err(ErrType t, std::wstring fmt) : Err(t, fmt.c_str()) {} }; /* Return a wide string describing exception. */ std::wstring What(std::exception_ptr e = std::current_exception()); /* Exit gracefully on uncaught exception. */ void OnTerminate(); /* Exception-catching version of F that warns on exception. */ template auto Except = (decltype(F))[](auto... xs) { try { return F(xs...); } catch (...) { EBMessageBox(What(), L"Error"); if constexpr (!std::is_same_v) return decltype(F(xs...)){}; } }; /* Exception-catching version of F that exits on exception. */ template auto Noexcept = (decltype(F))[](auto... xs) { try { return F(xs...); } catch (...) { EBMessageBox(What(), L"Fatal Error", MB_ICONERROR); exit(1); if constexpr (!std::is_same_v) return decltype(F(xs...)){}; } }; #endif