Skip to content

Commit

Permalink
fix: Avoid throwing in BeforeNodeWrittenListener on new files
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr authored and backportbot[bot] committed Aug 5, 2024
1 parent de882a4 commit 2c1a055
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/Listeners/BeforeNodeWrittenListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event|BeforeNodeWrittenEvent>
*/
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 {
Expand All @@ -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]);
}
}
}

0 comments on commit 2c1a055

Please sign in to comment.