Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
IO/FileSystem/Exception.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
8
9#include "Exception.h"
10
11using namespace Stroika::Foundation;
13using namespace Stroika::Foundation::IO;
15
16// Comment this in to turn on aggressive noisy DbgTrace in this module
17// #define USE_NOISY_TRACE_IN_THIS_MODULE_ 1
18
19/*
20 ********************************************************************************
21 ******************************* FileSystem::Exception **************************
22 ********************************************************************************
23 */
24Characters::String FileSystem::Exception::mkMsg_ (error_code errCode, const path& p1, const path& p2)
25{
26 StringBuilder sb = Execution::Private_::SystemErrorExceptionPrivate_::mkMsg_ (errCode);
27 if (not p1.empty ()) {
28 sb << " {"sv << String{p1.wstring ()}.LimitLength (25, StringShorteningPreference::ePreferKeepRight) << "}"sv;
29 }
30 if (not p2.empty ()) {
31 sb << " {"sv << String{p2.wstring ()}.LimitLength (25, StringShorteningPreference::ePreferKeepRight) << "}"sv;
32 }
33 return sb;
34}
35
36Characters::String FileSystem::Exception::mkMsg_ (error_code errCode, const Characters::String& message, const path& p1, const path& p2)
37{
38 StringBuilder sb = Execution::Private_::SystemErrorExceptionPrivate_::mkCombinedMsg_ (errCode, message);
39 if (not p1.empty ()) {
40 sb << " {"sv << String{p1.wstring ()}.LimitLength (25, StringShorteningPreference::ePreferKeepRight) << "}"sv;
41 }
42 if (not p2.empty ()) {
43 sb << " {"sv << String{p2.wstring ()}.LimitLength (25, StringShorteningPreference::ePreferKeepRight) << "}"sv;
44 }
45 return sb;
46}
47
48void Exception::ThrowPOSIXErrNo (errno_t errNo, const path& p1, const path& p2)
49{
50#if USE_NOISY_TRACE_IN_THIS_MODULE_
51 Debug::TraceContextBumper ctx{"IO::FileSystem::Exception::ThrowPOSIXErrNo", "sysErr={}, p1={}, p2={}"_f, errNo, p1, p2};
52#endif
53 Require (errNo != 0);
54#if qStroika_Foundation_Common_Platform_POSIX
55 error_code ec{errNo, system_category ()};
56#else
57 error_code ec{errNo, generic_category ()};
58#endif
59 Throw (Exception{ec, p1, p2});
60}
61
62void Exception::ThrowSystemErrNo (int sysErr, const path& p1, const path& p2)
63{
64#if USE_NOISY_TRACE_IN_THIS_MODULE_
65 Debug::TraceContextBumper ctx{"IO::FileSystem::Exception::ThrowSystemErrNo", "sysErr={}, p1={}, p2={}"_f, sysErr, p1, p2};
66#endif
67 Require (sysErr != 0);
68 error_code ec{sysErr, system_category ()};
69 Throw (Exception{ec, p1, p2});
70}
71
72void Exception::ThrowSystemErrNo (const path& p1, const path& p2)
73{
74#if qStroika_Foundation_Common_Platform_POSIX
75 ThrowSystemErrNo (errno, p1, p2);
76#elif qStroika_Foundation_Common_Platform_Windows
77 ThrowSystemErrNo (::GetLastError (), p1, p2);
78#endif
79}
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual String LimitLength(size_t maxLen, StringShorteningPreference keepPref=StringShorteningPreference::ePreferKeepLeft) const
return the first maxLen (or fewer if string shorter) characters of this string (adding ellipsis if tr...
Definition String.inl:741
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...