aboutsummaryrefslogtreecommitdiff
path: root/src/base/platform/win/Exception.cpp
blob: adc59e3c3751cc4938f1c479f8a07a371d48a173 (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
#include "cru/base/platform/win/Exception.h"

#include <format>
#include <optional>

namespace cru::platform::win {

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

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

HResultError::HResultError(HRESULT h_result,
                           std::string_view additional_message)
    : Exception(HResultMakeMessage(h_result, additional_message)),
      h_result_(h_result) {}

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

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

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