diff --git a/deps/ox/src/ox/std/errhandling.hpp b/deps/ox/src/ox/std/errhandling.hpp new file mode 100644 index 00000000..30dda7b3 --- /dev/null +++ b/deps/ox/src/ox/std/errhandling.hpp @@ -0,0 +1,67 @@ +/* +* Copyright 2015 - 2025 gary@drinkingtea.net + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include +#include +#include + +namespace ox { + +template +constexpr Error call(F const &f, Args&&... args) noexcept { + try { + f(ox::forward(args)...); + return {}; + } catch (Exception const &e) { + return e.toError(); + } catch (...) { + return Error{1, "unknown exception"}; + } +} + +template +constexpr Error call(T &receiver, Method methodPtr, Args&&... args) noexcept { + try { + (receiver.*methodPtr)(ox::forward(args)...); + return {}; + } catch (Exception const &e) { + return e.toError(); + } catch (...) { + return Error{1, "unknown exception"}; + } +} + +template +constexpr void logCatch(F const &f, Args&&... args) noexcept { + try { + f(ox::forward(args)...); + } catch (Exception const &e) { + oxErrf("Caught exception: {} ({}:{})\n", e.what(), e.src.file_name(), e.src.line()); + } catch (std::exception const &e) { + oxErrf("Caught exception: {}\n", e.what()); + } catch (...) { + oxErr("Caught unknown exception\n"); + } +} + +template +constexpr void logCatch(T &receiver, Method methodPtr, Args&&... args) noexcept { + try { + (receiver.*methodPtr)(ox::forward(args)...); + } catch (Exception const &e) { + oxErrf("Caught exception: {} ({}:{})\n", e.what(), e.src.file_name(), e.src.line()); + } catch (std::exception const &e) { + oxErrf("Caught exception: {}\n", e.what()); + } catch (...) { + oxErr("Caught unknown exception\n"); + } +} + +}