88
99#include " source/common/common/assert.h"
1010#include " source/common/common/fmt.h"
11+ #include " source/common/common/thread.h"
1112#include " source/common/common/utility.h"
1213#include " source/common/filesystem/watcher_impl.h"
1314
@@ -116,20 +117,28 @@ absl::Status WatcherImpl::onKqueueEvent() {
116117
117118 absl::StatusOr<PathSplitResult> pathname_or_error =
118119 file_system_.splitPathFromFilename (file->file_ );
119- RETURN_IF_NOT_OK_REF (pathname_or_error.status ());
120+ if (!pathname_or_error.ok ()) {
121+ ENVOY_LOG (warn, " Failed to split path '{}': {}" , file->file_ ,
122+ pathname_or_error.status ().message ());
123+ continue ;
124+ }
120125 PathSplitResult& pathname = pathname_or_error.value ();
121126
122127 if (file->watching_dir_ ) {
123128 if (event.fflags & NOTE_DELETE) {
124- // directory was deleted
129+ // Directory was deleted.
125130 removeWatch (file);
126131 return absl::OkStatus ();
127132 }
128133
129134 if (event.fflags & NOTE_WRITE) {
130- // directory was written -- check if the file we're actually watching appeared
135+ // Directory was written -- check if the file we're actually watching appeared.
131136 auto file_or_error = addWatch (file->file_ , file->events_ , file->callback_ , true );
132- RETURN_IF_NOT_OK_REF (file_or_error.status ());
137+ if (!file_or_error.ok ()) {
138+ ENVOY_LOG (warn, " Failed to re-add watch for '{}': {}" , file->file_ ,
139+ file_or_error.status ().message ());
140+ continue ;
141+ }
133142 FileWatchPtr new_file = file_or_error.value ();
134143 if (new_file != nullptr ) {
135144 removeWatch (file);
@@ -150,7 +159,11 @@ absl::Status WatcherImpl::onKqueueEvent() {
150159 removeWatch (file);
151160
152161 auto file_or_error = addWatch (file->file_ , file->events_ , file->callback_ , true );
153- RETURN_IF_NOT_OK_REF (file_or_error.status ());
162+ if (!file_or_error.ok ()) {
163+ ENVOY_LOG (warn, " Failed to re-add watch for '{}': {}" , file->file_ ,
164+ file_or_error.status ().message ());
165+ continue ;
166+ }
154167 FileWatchPtr new_file = file_or_error.value ();
155168 if (new_file == nullptr ) {
156169 return absl::OkStatus ();
@@ -173,11 +186,29 @@ absl::Status WatcherImpl::onKqueueEvent() {
173186
174187 if (events & file->events_ ) {
175188 ENVOY_LOG (debug, " matched callback: file: {}" , file->file_ );
176- RETURN_IF_NOT_OK (file->callback_ ( events) );
189+ callAndLogOnError (file->callback_ , events, file-> file_ );
177190 }
178191 }
179192 return absl::OkStatus ();
180193}
181194
195+ void WatcherImpl::callAndLogOnError (Watcher::OnChangedCb& cb, uint32_t events,
196+ const std::string& file) {
197+ TRY_ASSERT_MAIN_THREAD {
198+ const absl::Status status = cb (events);
199+ if (!status.ok ()) {
200+ ENVOY_LOG (warn, " Filesystem watch callback for '{}' returned error: {}" , file,
201+ status.message ());
202+ }
203+ }
204+ END_TRY
205+ MULTI_CATCH (
206+ const std::exception& e,
207+ {
208+ ENVOY_LOG (warn, " Filesystem watch callback for '{}' threw exception: {}" , file, e.what ());
209+ },
210+ { ENVOY_LOG (warn, " Filesystem watch callback for '{}' threw unknown exception" , file); });
211+ }
212+
182213} // namespace Filesystem
183214} // namespace Envoy
0 commit comments