Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
IO/FileSystem/Exception.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_IO_FileSystem_Exception_h_
5#define _Stroika_Foundation_IO_FileSystem_Exception_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <filesystem>
10
12#include "Stroika/Foundation/Execution/Exceptions.h"
13#include "Stroika/Foundation/IO/FileSystem/Common.h"
14
15/**
16 * \file
17 *
18 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
19 *
20 */
21
23
24 using Characters::String;
26
27 /**
28 * Simple wrapper on std::filesystem_error, but adding support for Stroika String, and other utility methods.
29 *
30 * \note see https://en.cppreference.com/w/cpp/error/errc for a mapping of errc conditions and ERRNO values.
31 *
32 * \par Example Usage
33 * \code
34 * try {
35 * FileSystem::ThrowPOSIXErrNo (make_error_code (errc::filename_too_long).value ());
36 * }
37 * catch (const std::filesystem_error& e) {
38 * EXPECT_TRUE (e.code ().value () == make_error_code (errc::filename_too_long).value ());
39 * EXPECT_TRUE (e.code ().category () == system_category () or e.code ().category () == generic_category ());
40 * }
41 * \endcode
42 *
43 * \par Example Usage (can catch as system_error as well since thats a base class of filesystem_error)
44 * \code
45 * try {
46 * FileSystem::ThrowPOSIXErrNo (make_error_code (errc::filename_too_long).value ());
47 * }
48 * catch (const std::system_error& e) {
49 * EXPECT_TRUE (e.code ().value () == make_error_code (errc::filename_too_long).value ());
50 * EXPECT_TRUE (e.code ().category () == system_category () or e.code ().category () == generic_category ());
51 * }
52 * \endcode
53 *
54 */
55 class Exception : public Execution::SystemErrorException<filesystem_error> {
56 private:
58
59 public:
60 /**
61 */
62 Exception (error_code errCode, const path& p1 = {}, const path& p2 = {});
63 Exception (error_code errCode, const Characters::String& message, const path& p1 = {}, const path& p2 = {});
64
65 public:
66 /**
67 * \brief treats errNo as a `POSIX errno` value, and throws a FileSystem::Exception (subclass of @std::filesystem_error) exception with it.
68 *
69 * \pre errNo != 0
70 * \pre if (p1.empty() then require (p2.empty ()); but neither is required
71 *
72 * See:
73 * @see Execution::ThrowPOSIXErrNo ();
74 * @see ThrowSystemErrNo ();
75 */
76 [[noreturn]] static void ThrowPOSIXErrNo (errno_t errNo, const path& p1 = {}, const path& p2 = {});
77
78 public:
79 /**
80 * Look at the argument value and if < 0,ThrowPOSIXErrNo (), and otherwise return it.
81 */
82 template <typename INT_TYPE>
83 static INT_TYPE ThrowPOSIXErrNoIfNegative (INT_TYPE returnCode, const path& p1 = {}, const path& p2 = {});
84
85 public:
86 /**
87 * \brief treats errNo as a platform-defined error number, and throws a FileSystem::Exception (subclass of @std::filesystem_error) exception with it.
88 *
89 * \pre sysErr != 0
90 * \pre if (p1.empty() then require (p2.empty ()); but neither is required
91 *
92 * See:
93 * @see SystemErrorException<>::ThrowSystemErrNo ();
94 * @see ThrowPOSIXErrNo ();
95 *
96 */
97 [[noreturn]] static void ThrowSystemErrNo (int sysErr, const path& p1 = {}, const path& p2 = {});
98 [[noreturn]] static void ThrowSystemErrNo (const path& p1, const path& p2 = {});
99
100#if qStroika_Foundation_Common_Platform_Windows
101 public:
102 /**
103 */
104 template <typename WINDOWS_API_RESULT>
105 static void ThrowIfZeroGetLastError (WINDOWS_API_RESULT test, const path& p1 = {}, const path& p2 = {});
106#endif
107
108 private:
109 static Characters::String mkMsg_ (error_code errCode, const path& p1, const path& p2);
110 static Characters::String mkMsg_ (error_code errCode, const Characters::String& message, const path& p1, const path& p2);
111 };
112
113}
114
115/*
116 ********************************************************************************
117 ***************************** Implementation Details ***************************
118 ********************************************************************************
119 */
120#include "Exception.inl"
121
122#endif /*_Stroika_Foundation_IO_FileSystem_Exception_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
static INT_TYPE ThrowPOSIXErrNoIfNegative(INT_TYPE returnCode, const path &p1={}, const path &p2={})
static void ThrowPOSIXErrNo(errno_t errNo, const path &p1={}, const path &p2={})
treats errNo as a POSIX errno value, and throws a FileSystem::Exception (subclass of @std::filesystem...
static void ThrowSystemErrNo(int sysErr, const path &p1={}, const path &p2={})
treats errNo as a platform-defined error number, and throws a FileSystem::Exception (subclass of @std...