aboutsummaryrefslogtreecommitdiff
path: root/src/win/exception.cpp
blob: 62305329b6f48326207bb7f670e3d89c948b97c9 (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
39
40
41
42
43
44
45
46
47
48
49
50
#include "cru/win/exception.hpp"

#include "cru/common/format.hpp"

namespace cru::win {
using util::Format;

inline std::string HResultMakeMessage(HRESULT h_result,
                                      const std::string_view* message) {
  char buffer[10];
  sprintf_s(buffer, "%#08x", h_result);

  if (message)
    return Format(
        "An HResultError is thrown. HRESULT: {}.\nAdditional message: {}\n",
        buffer, *message);
  else
    return Format("An HResultError is thrown. HRESULT: {}.\n", buffer);
}

HResultError::HResultError(HRESULT h_result)
    : PlatformException(HResultMakeMessage(h_result, nullptr)),
      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,
                                    const std::string_view* message) {
  char buffer[10];
  sprintf_s(buffer, "%#04x", error_code);

  if (message)
    return Format("Last error code: {}.\nAdditional message: {}\n", buffer,
                  *message);
  else
    return Format("Last error code: {}.\n", buffer);
}

Win32Error::Win32Error(DWORD error_code)
    : PlatformException(Win32MakeMessage(error_code, nullptr)),
      error_code_(error_code) {}

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