Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
FileSystemWatcherErrorFormatter.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "../Common/ResultErrorFormatter.h"
6#include "FileSystemWatcherError.h"
7
8namespace SC
9{
12
13namespace detail
14{
15inline bool appendFileSystemWatcherErrorDetail(ResultErrorFormatter& formatter, FileSystemWatcherErrorDetail detail)
16{
17 switch (detail)
18 {
19 case FileSystemWatcherErrorDetail::None: return true;
20 case FileSystemWatcherErrorDetail::AssignWatchPath: formatter.append("assign watch path"); break;
21 case FileSystemWatcherErrorDetail::BuildSubfolderPath: formatter.append("build subfolder path"); break;
22 case FileSystemWatcherErrorDetail::BuildNotificationPath: formatter.append("build notification path"); break;
23 case FileSystemWatcherErrorDetail::BuildFullPath: formatter.append("build full path"); break;
24 case FileSystemWatcherErrorDetail::WatchPathCapacity: formatter.append("watch path capacity"); break;
25 case FileSystemWatcherErrorDetail::WorkerAlreadyStarted: formatter.append("worker already started"); break;
26 case FileSystemWatcherErrorDetail::PosixPthreadCreate: formatter.append("POSIX create thread"); break;
27 case FileSystemWatcherErrorDetail::PosixPthreadJoin: formatter.append("POSIX join thread"); break;
28 case FileSystemWatcherErrorDetail::WindowsCreateThread: formatter.append("Windows create thread"); break;
29 case FileSystemWatcherErrorDetail::WindowsWaitForSingleObject: formatter.append("Windows wait for thread"); break;
30 case FileSystemWatcherErrorDetail::WindowsCreateWatchEvent: formatter.append("Windows create watch event"); break;
31 case FileSystemWatcherErrorDetail::WindowsSignalWatchEvent: formatter.append("Windows signal watch event"); break;
32 case FileSystemWatcherErrorDetail::WindowsWaitForWatchEvents:
33 formatter.append("Windows wait for watch events");
34 break;
35 case FileSystemWatcherErrorDetail::WindowsCancelDirectoryChanges:
36 formatter.append("Windows cancel directory changes");
37 break;
38 case FileSystemWatcherErrorDetail::PosixCreateShutdownPipe: formatter.append("POSIX create shutdown pipe"); break;
39 case FileSystemWatcherErrorDetail::InotifyInitialize: formatter.append("inotify initialize"); break;
40 case FileSystemWatcherErrorDetail::PosixWriteShutdownPipe: formatter.append("POSIX write shutdown pipe"); break;
41 case FileSystemWatcherErrorDetail::InotifyDescriptor: formatter.append("inotify descriptor"); break;
42 case FileSystemWatcherErrorDetail::InotifyRemoveWatch: formatter.append("inotify remove watch"); break;
43 case FileSystemWatcherErrorDetail::InotifyAddRootWatch: formatter.append("inotify add root watch"); break;
44 case FileSystemWatcherErrorDetail::InotifyAddSubdirectoryWatch:
45 formatter.append("inotify add subdirectory watch");
46 break;
47 case FileSystemWatcherErrorDetail::InotifyOpenRootDirectory: formatter.append("inotify open root directory"); break;
48 case FileSystemWatcherErrorDetail::InotifyOpenDirectoryStream:
49 formatter.append("inotify open directory stream");
50 break;
51 case FileSystemWatcherErrorDetail::InotifyReadDirectoryEntries:
52 formatter.append("inotify read directory entries");
53 break;
54 case FileSystemWatcherErrorDetail::InotifyInspectDirectoryEntry:
55 formatter.append("inotify inspect directory entry");
56 break;
57 case FileSystemWatcherErrorDetail::InotifyOpenSubdirectory: formatter.append("inotify open subdirectory"); break;
58 case FileSystemWatcherErrorDetail::InotifyReadEvents: formatter.append("inotify read events"); break;
59 case FileSystemWatcherErrorDetail::InotifyVerifyDirectoryIdentity:
60 formatter.append("inotify verify directory identity");
61 break;
62 case FileSystemWatcherErrorDetail::InotifyWatchInvalidated: formatter.append("inotify watch invalidated"); break;
63 case FileSystemWatcherErrorDetail::InotifyWaitForEvents: formatter.append("inotify wait for events"); break;
64 case FileSystemWatcherErrorDetail::InotifyWatchHandleCapacity:
65 formatter.append("inotify watch handle capacity");
66 break;
67 case FileSystemWatcherErrorDetail::InotifyPendingDirectoryCapacity:
68 formatter.append("inotify pending directory capacity");
69 break;
70 case FileSystemWatcherErrorDetail::InotifyRelativePathStorage:
71 formatter.append("inotify relative path storage");
72 break;
73 case FileSystemWatcherErrorDetail::AppleRunLoop: formatter.append("Apple run loop"); break;
74 case FileSystemWatcherErrorDetail::AppleCreateRunLoopSource:
75 formatter.append("Apple create run loop source");
76 break;
77 case FileSystemWatcherErrorDetail::AppleAllocateWatchPaths: formatter.append("Apple allocate watch paths"); break;
78 case FileSystemWatcherErrorDetail::AppleCreateWatchPathString:
79 formatter.append("Apple create watch path string");
80 break;
81 case FileSystemWatcherErrorDetail::AppleCreateWatchPathsArray:
82 formatter.append("Apple create watch paths array");
83 break;
84 case FileSystemWatcherErrorDetail::AppleCreateEventStream: formatter.append("Apple create event stream"); break;
85 case FileSystemWatcherErrorDetail::AppleStartEventStream: formatter.append("Apple start event stream"); break;
86 case FileSystemWatcherErrorDetail::WindowsOpenDirectory: formatter.append("Windows open directory"); break;
87 case FileSystemWatcherErrorDetail::WindowsSubmitDirectoryChanges:
88 formatter.append("Windows submit directory changes");
89 break;
90 case FileSystemWatcherErrorDetail::WindowsCompleteDirectoryChanges:
91 formatter.append("Windows complete directory changes");
92 break;
93 case FileSystemWatcherErrorDetail::AppleOpenRootDirectory: formatter.append("Apple open root directory"); break;
94 case FileSystemWatcherErrorDetail::AppleResolveWatchRoot: formatter.append("Apple resolve watch root"); break;
95 case FileSystemWatcherErrorDetail::AppleWatchInvalidated: formatter.append("Apple watch invalidated"); break;
96 case FileSystemWatcherErrorDetail::WindowsQueryRootDirectory:
97 formatter.append("Windows query root directory");
98 break;
99 default: return false;
100 }
101 return true;
102}
103
104inline ResultErrorFormat formatFileSystemWatcherErrorWithDetails(FileSystemWatcherError error,
106 uint32_t nativeError, Span<char> output)
107{
108 ResultErrorFormatter formatter(output);
109 switch (error)
110 {
111 case FileSystemWatcherError::NotInitialized: formatter.append("Watcher is not initialized"); break;
112 case FileSystemWatcherError::AlreadyWatching: formatter.append("Folder watcher is already watching"); break;
113 case FileSystemWatcherError::NotWatching: formatter.append("Folder watcher is not watching"); break;
114 case FileSystemWatcherError::UnsupportedPathEncoding: formatter.append("Path encoding is not supported"); break;
115 case FileSystemWatcherError::PathPreparationFailed: formatter.append("Failed to prepare path"); break;
116 case FileSystemWatcherError::WatchLimitExceeded: formatter.append("Watch capacity exceeded"); break;
117 case FileSystemWatcherError::BufferTooSmall: formatter.append("Buffer capacity is insufficient"); break;
118 case FileSystemWatcherError::InitializationFailed: formatter.append("Failed to initialize watcher"); break;
119 case FileSystemWatcherError::WatchSetupFailed: formatter.append("Failed to configure watching"); break;
120 case FileSystemWatcherError::StopWatchingFailed: formatter.append("Failed to stop watching"); break;
121 case FileSystemWatcherError::CloseFailed: formatter.append("Failed to close watcher"); break;
122 case FileSystemWatcherError::MonitoringFailed: formatter.append("Filesystem monitoring stopped"); break;
123 default: return ResultErrorFormatter::failure(ResultErrorFormatStatus::UnknownError, output);
124 }
125
126 if (detail != FileSystemWatcherErrorDetail::None)
127 {
128 formatter.append(" (detail: ");
129 if (not appendFileSystemWatcherErrorDetail(formatter, detail))
130 return ResultErrorFormatter::failure(ResultErrorFormatStatus::UnknownError, output);
131 if (nativeError != 0)
132 {
133 formatter.append(", native error: ");
134 formatter.append(static_cast<uint64_t>(nativeError));
135 }
136 formatter.append(")");
137 }
138 else if (nativeError != 0)
139 {
140 formatter.append(" (native error: ");
141 formatter.append(static_cast<uint64_t>(nativeError));
142 formatter.append(")");
143 }
144 return formatter.finish();
145}
146} // namespace detail
147
148inline ResultErrorFormat formatFileSystemWatcherError(FileSystemWatcherError error, Span<char> output)
149{
150 return detail::formatFileSystemWatcherErrorWithDetails(error, FileSystemWatcherErrorDetail::None, 0, output);
151}
152
153inline ResultErrorFormat formatFileSystemWatcherError(Result result, Span<char> output)
154{
155 if (result)
156 return ResultErrorFormatter::failure(ResultErrorFormatStatus::NotAnError, output);
157 if (result.category() != FileSystemWatcherResultCategory)
158 return ResultErrorFormatter::failure(ResultErrorFormatStatus::ForeignCategory, output);
159 return formatFileSystemWatcherError(static_cast<FileSystemWatcherError>(result.errorValue()), output);
160}
161
162inline ResultErrorFormat formatFileSystemWatcherError(ResultFileSystemWatcher result, Span<char> output)
163{
164 if (result)
165 return ResultErrorFormatter::failure(ResultErrorFormatStatus::NotAnError, output);
166 if (result.result.category() != FileSystemWatcherResultCategory)
167 return ResultErrorFormatter::failure(ResultErrorFormatStatus::ForeignCategory, output);
168 return detail::formatFileSystemWatcherErrorWithDetails(
169 static_cast<FileSystemWatcherError>(result.result.errorValue()), result.detail, result.nativeError, output);
170}
171
173} // namespace SC
FileSystemWatcherError
Stable portable failures returned by the FileSystemWatcher library.
Definition FileSystemWatcherError.h:14
FileSystemWatcherErrorDetail
Stable backend stages retained as context for a FileSystemWatcher failure.
Definition FileSystemWatcherError.h:31