From 32c2fc1e84ab5266c4a5ce24345dc51852f47b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Mon, 5 Aug 2024 10:40:02 +0200 Subject: [PATCH] fix: Avoid throwing in BeforeNodeWrittenListener on new files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Listeners/BeforeNodeWrittenListener.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/Listeners/BeforeNodeWrittenListener.php b/lib/Listeners/BeforeNodeWrittenListener.php index 950703b7280..f769aacd026 100644 --- a/lib/Listeners/BeforeNodeWrittenListener.php +++ b/lib/Listeners/BeforeNodeWrittenListener.php @@ -31,15 +31,17 @@ use OCP\EventDispatcher\IEventListener; use OCP\Files\Events\Node\BeforeNodeWrittenEvent; use OCP\Files\File; +use OCP\Files\NotFoundException; +use Psr\Log\LoggerInterface; /** * @template-implements IEventListener */ class BeforeNodeWrittenListener implements IEventListener { - private DocumentService $documentService; - - public function __construct(DocumentService $documentService) { - $this->documentService = $documentService; + public function __construct( + private LoggerInterface $logger, + private DocumentService $documentService + ) { } public function handle(Event $event): void { @@ -56,8 +58,11 @@ public function handle(Event $event): void { // Reset document session to avoid manual conflict resolution if there's no unsaved steps try { $this->documentService->resetDocument($node->getId()); - } catch (DocumentHasUnsavedChangesException) { + } catch (DocumentHasUnsavedChangesException|NotFoundException $e) { // Do not throw during event handling in this is expected to happen + // DocumentHasUnsavedChangesException: A document editing session is likely ongoing, someone can resolve the conflict + // NotFoundException: The event was called oin a file that was just created so a NonExistingFile object is used that has no id yet + $this->logger->debug('Reset document skipped in BeforeNodeWrittenEvent', ['exception' => $e]); } } }