Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
HRESULTErrorException.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#if qStroika_Platform_Windows
7#include <Windows.h>
8#include <wininet.h> // for INTERNET_ERROR_BASE
9#else
10#error "WINDOWS REQUIRED FOR THIS MODULE"
11#endif
12
14#include "Stroika/Foundation/Common/Common.h"
16#include "Stroika/Foundation/Containers/Common.h"
18#include "Stroika/Foundation/Execution/Platform/Windows/Exception.h"
20
21#include "HRESULTErrorException.h"
22
23using namespace Stroika::Foundation;
25using namespace Stroika::Foundation::Execution;
26using namespace Stroika::Foundation::Execution::Platform;
28
29namespace {
30 class HRESULT_error_category_ : public error_category {
31 public:
32 virtual const char* name () const noexcept override
33 {
34 return "HRESULT error"; // used to return return "getaddrinfo"; - but the name DNS is more widely recognized, and even though this could be from another source, this name is more clear
35 }
36 virtual error_condition default_error_condition ([[maybe_unused]] int ev) const noexcept override
37 {
38 if (HRESULT_CODE (ev) or ev == 0) {
39 // a wrapped Win32 code: what it means unwrapped (@see Win32_error_category)
40 return Win32_error_category ().default_error_condition (HRESULT_CODE (ev));
41 }
42 else {
43 return {ev, HRESULT_error_category ()}; // special error condition (the immortal object - NOT a temporary)
44 }
45 }
46 virtual bool equivalent (int ev, const error_condition& cond) const noexcept override
47 {
48 if (HRESULT_CODE (ev) or ev == 0) {
49 return Win32_error_category ().equivalent (HRESULT_CODE (ev), cond); // all it means unwrapped - not just its default
50 }
51 return error_category::equivalent (ev, cond);
52 }
53 virtual string message (int hr) const override
54 {
55 switch (hr) {
56 case E_FAIL:
57 return "E_FAIL";
58 case E_ACCESSDENIED:
59 return "E_ACCESSDENIED";
60 case E_INVALIDARG:
61 return "E_INVALIDARG";
62 case E_NOINTERFACE:
63 return "E_NOINTERFACE";
64 case E_POINTER:
65 return "E_POINTER";
66 case E_HANDLE:
67 return "E_HANDLE";
68 case E_ABORT:
69 return "E_ABORT";
70 case DISP_E_TYPEMISMATCH:
71 return "DISP_E_TYPEMISMATCH";
72 case DISP_E_EXCEPTION:
73 return "DISP_E_EXCEPTION";
74 case INET_E_RESOURCE_NOT_FOUND:
75 return "INET_E_RESOURCE_NOT_FOUND";
76 case REGDB_E_CLASSNOTREG:
77 return "REGDB_E_CLASSNOTREG";
78 }
79 if (HRESULT_FACILITY (hr) == FACILITY_WIN32) {
80 return Win32_error_category ().message (HRESULT_CODE (hr));
81 }
82 if (HRESULT_FACILITY (hr) == FACILITY_INTERNET) {
83 unsigned int wCode = HRESULT_CODE (hr);
84 if (wCode < INTERNET_ERROR_BASE) {
85 wCode += INTERNET_ERROR_BASE; // because the HRESULT_CODE doesn't (at least sometimes) include the INTERNET_ERROR_BASE
86 // included in the constants below...
87 }
88 return Win32_error_category ().message (HRESULT_CODE (wCode));
89 }
90 char buf[1024];
91 (void)::snprintf (buf, std::size (buf), "HRESULT error code: 0x%x", hr);
92 return buf;
93 }
94 };
95}
96
97/*
98 ********************************************************************************
99 ************* Platform::Windows::HRESULT_error_category ************************
100 ********************************************************************************
101 */
103{
104 return Common::Immortalize<HRESULT_error_category_> ();
105}