aboutsummaryrefslogtreecommitdiff
path: root/src/exception.cpp
blob: dbc984538e40ec3dd948448b2f8c3b856ee0e20f (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
#include "exception.hpp"

#include "util/format.hpp"

namespace cru {
using util::Format;

inline std::string HResultMakeMessage(
    HRESULT h_result, std::optional<MultiByteStringView> message) {
  char buffer[10];
  sprintf_s(buffer, "%#08x", h_result);

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

HResultError::HResultError(
    HRESULT h_result, std::optional<MultiByteStringView> additional_message)
    : runtime_error(HResultMakeMessage(h_result, std::nullopt)),
      h_result_(h_result) {}

inline std::string Win32MakeMessage(
    DWORD error_code, std::optional<MultiByteStringView> message) {
  char buffer[10];
  sprintf_s(buffer, "%#04x", error_code);

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

Win32Error::Win32Error(DWORD error_code,
                       std::optional<MultiByteStringView> additional_message)
    : runtime_error(Win32MakeMessage(error_code, std::nullopt)),
      error_code_(error_code) {}
}  // namespace cru