aboutsummaryrefslogtreecommitdiff
path: root/src/win/Exception.cpp
blob: 9f9fb03d854587c6e0493ffd04602689ffad7fb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "cru/win/Exception.hpp"

#include <fmt/format.h>
#include <optional>

namespace cru::platform::win {

inline std::string HResultMakeMessage(HRESULT h_result,
                                      std::optional<std::string_view> message) {
  if (message.has_value())
    return fmt::format(FMT_STRING("HRESULT: {:#08x}. Message: {}"), h_result,
                       *message);
  else
    return fmt::format(FMT_STRING("HRESULT: {:#08x}."), h_result);
}

HResultError::HResultError(HRESULT h_result)
    : PlatformException(HResultMakeMessage(h_result, std::nullopt)),
      h_result_(h_result) {}

HResultError::HResultError(HRESULT h_result,
                           const std::string_view& additional_message)
    : PlatformException(HResultMakeMessage(h_result, additional_message)),
      h_result_(h_result) {}

inline std::string Win32MakeMessage(DWORD error_code,
                                    std::string_view message) {
  return fmt::format("Last error code: {:#04x}.\nMessage: {}\n", error_code,
                     message);
}

Win32Error::Win32Error(const std::string_view& message)
    : Win32Error(::GetLastError(), message) {}

Win32Error::Win32Error(DWORD error_code, const std::string_view& message)
    : PlatformException(Win32MakeMessage(error_code, message)),
      error_code_(error_code) {}
}  // namespace cru::platform::win