magpie
Loading...
Searching...
No Matches
ErrorHandler.hpp
1#pragma once
2
3#include "magpie/logger/Logger.hpp"
4#include "magpie/transfer/Response.hpp"
5#include "magpie/transfer/StatusCode.hpp"
6#include <string>
7#include <functional>
8
9namespace magpie::utility {
10
11inline void defaultErrorResponse(Response* res) {
12 if (res == nullptr) {
13 return;
14 }
15
16 *res = Response(
17 Status::InternalServerError,
18 "Unexpected error. Try again later."
19 );
20}
21
34inline void runWithErrorLogging(
35 const std::function<void()>& errorHandled,
36 Response* res = nullptr
37) {
38
39 try {
40 errorHandled();
41 } catch (const std::exception& e) {
42 logger::error(
43 "Uncaught exception: {}", e.what()
44 );
45 defaultErrorResponse(res);
46 } catch (const std::string& e) {
47 logger::error(
48 "Uncaught {{str}}exception: {}", e
49 );
50 defaultErrorResponse(res);
51 } catch (...) {
52 // TODO: how does catch2 do this kind of logging? I doubt they have a full cascade of every single
53 // type that may be caught in anything ever
54 // Might be template fuckery though
55 logger::critical(
56 "Uncaught exception of unknown type (cannot log)"
57 );
58 defaultErrorResponse(res);
59 }
60}
61
62}