Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
HRESULTErrorException.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#if qStroika_Foundation_Common_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
13#include "Exception.h"
15#include "Stroika/Foundation/Common/Common.h"
17#include "Stroika/Foundation/Containers/Common.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) || ev == 0) {
39 // system error condition
40 return system_category ().default_error_condition (HRESULT_CODE (ev));
41 }
42 else {
43 return {ev, HRESULT_error_category_ ()}; // special error condition
44 }
45 }
46 virtual string message (int hr) const override
47 {
48 switch (hr) {
49 case E_FAIL:
50 return "E_FAIL";
51 case E_ACCESSDENIED:
52 return "E_ACCESSDENIED";
53 case E_INVALIDARG:
54 return "E_INVALIDARG";
55 case E_NOINTERFACE:
56 return "E_NOINTERFACE";
57 case E_POINTER:
58 return "E_POINTER";
59 case E_HANDLE:
60 return "E_HANDLE";
61 case E_ABORT:
62 return "E_ABORT";
63 case DISP_E_TYPEMISMATCH:
64 return "DISP_E_TYPEMISMATCH";
65 case DISP_E_EXCEPTION:
66 return "DISP_E_EXCEPTION";
67 case INET_E_RESOURCE_NOT_FOUND:
68 return "INET_E_RESOURCE_NOT_FOUND";
69 case REGDB_E_CLASSNOTREG:
70 return "REGDB_E_CLASSNOTREG";
71 }
72 if (HRESULT_FACILITY (hr) == FACILITY_WIN32) {
73 return system_category ().message (HRESULT_CODE (hr));
74 }
75 if (HRESULT_FACILITY (hr) == FACILITY_INTERNET) {
76 unsigned int wCode = HRESULT_CODE (hr);
77 if (wCode < INTERNET_ERROR_BASE) {
78 wCode += INTERNET_ERROR_BASE; // because the HRESULT_CODE doesn't (at least sometimes) include the INTERNET_ERROR_BASE
79 // included in the constants below...
80 }
81 return system_category ().message (HRESULT_CODE (wCode));
82 }
83 char buf[1024];
84 (void)::snprintf (buf, Memory::NEltsOf (buf), "HRESULT error code: 0x%x", hr);
85 return buf;
86 }
87 };
88}
89
90/*
91 ********************************************************************************
92 ************* Platform::Windows::HRESULT_error_category ************************
93 ********************************************************************************
94 */
96{
97 return Common::Immortalize<HRESULT_error_category_> ();
98}