diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php index bd4e1e77a..7cb339ce9 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -34,6 +34,7 @@ use OCP\Search\ISearchQuery; use OCP\Security\ISecureRandom; use OCP\Share\IShare; +use Psr\Log\LoggerInterface; /** * Trait for getting forms information in a service @@ -62,6 +63,7 @@ public function __construct( private IRootFolder $rootFolder, private IL10N $l10n, private IEventDispatcher $eventDispatcher, + private LoggerInterface $logger, ) { $this->currentUser = $userSession->getUser(); } @@ -562,19 +564,30 @@ public function getShareDisplayName(array $share): string { * @param Share $share The new Share */ public function notifyNewShares(Form $form, Share $share): void { - switch ($share->getShareType()) { - case IShare::TYPE_USER: - $this->activityManager->publishNewShare($form, $share->getShareWith()); - break; - case IShare::TYPE_GROUP: - $this->activityManager->publishNewGroupShare($form, $share->getShareWith()); - break; - case IShare::TYPE_CIRCLE: - $this->activityManager->publishNewCircleShare($form, $share->getShareWith()); - break; - default: - // Do nothing. + try { + switch ($share->getShareType()) { + case IShare::TYPE_USER: + $this->activityManager->publishNewShare($form, $share->getShareWith()); + break; + case IShare::TYPE_GROUP: + $this->activityManager->publishNewGroupShare($form, $share->getShareWith()); + break; + case IShare::TYPE_CIRCLE: + $this->activityManager->publishNewCircleShare($form, $share->getShareWith()); + break; + default: + // Do nothing. + } + } catch (\Exception $e) { + // Handle exceptions silently, as this is not critical. + // We don't want to break the share creation process just because of an activity error. + $this->logger->error( + 'Error while publishing new share activity', + [$e] + ); + } + } /** @@ -585,14 +598,31 @@ public function notifyNewShares(Form $form, Share $share): void { */ public function notifyNewSubmission(Form $form, Submission $submission): void { $shares = $this->getShares($form->getId()); - $this->activityManager->publishNewSubmission($form, $submission->getUserId()); + try { + $this->activityManager->publishNewSubmission($form, $submission->getUserId()); + } catch (\Exception $e) { + // Handle exceptions silently, as this is not critical. + // We don't want to break the submission process just because of an activity error. + $this->logger->error( + 'Error while publishing new submission activity', + [$e] + ); + } foreach ($shares as $share) { if (!in_array(Constants::PERMISSION_RESULTS, $share['permissions'])) { continue; } - - $this->activityManager->publishNewSharedSubmission($form, $share['shareType'], $share['shareWith'], $submission->getUserId()); + try { + $this->activityManager->publishNewSharedSubmission($form, $share['shareType'], $share['shareWith'], $submission->getUserId()); + } catch (\Exception $e) { + // Handle exceptions silently, as this is not critical. + // We don't want to break the submission process just because of an activity error. + $this->logger->error( + 'Error while publishing new shared submission activity', + [$e] + ); + } } $this->eventDispatcher->dispatchTyped(new FormSubmittedEvent($form, $submission)); diff --git a/src/views/Submit.vue b/src/views/Submit.vue index 0a4314ebf..a5a15fd6e 100644 --- a/src/views/Submit.vue +++ b/src/views/Submit.vue @@ -818,12 +818,21 @@ export default { this.deleteFormFieldFromLocalStorage() emit('forms:last-updated:set', this.form.id) } catch (error) { + const errorMessage = error.response?.data?.ocs?.meta?.message logger.error('Error while submitting the form', { error }) - showError( - t('forms', 'There was an error submitting the form: {message}', { - message: error.response.data.ocs.meta.message, - }), - ) + if (errorMessage) { + showError( + t( + 'forms', + 'There was an error submitting the form: {message}', + { + message: errorMessage, + }, + ), + ) + } else { + showError(t('forms', 'There was an error submitting the form')) + } } finally { this.loading = false if (!this.publicView) { diff --git a/tests/Unit/Service/FormsServiceTest.php b/tests/Unit/Service/FormsServiceTest.php index 260ed1b07..f759eb45a 100644 --- a/tests/Unit/Service/FormsServiceTest.php +++ b/tests/Unit/Service/FormsServiceTest.php @@ -58,8 +58,9 @@ function microtime(bool|float $asFloat = false) { use OCP\IUserSession; use OCP\Security\ISecureRandom; use OCP\Share\IShare; - use PHPUnit\Framework\MockObject\MockObject; + +use Psr\Log\LoggerInterface; use Test\TestCase; class FormsServiceTest extends TestCase { @@ -105,6 +106,8 @@ class FormsServiceTest extends TestCase { /** @var IL10N|MockObject */ private $l10n; + /** @var LoggerInterface|MockObject */ + private $logger; public function setUp(): void { parent::setUp(); @@ -120,6 +123,7 @@ public function setUp(): void { $this->userManager = $this->createMock(IUserManager::class); $this->secureRandom = $this->createMock(ISecureRandom::class); $this->circlesService = $this->createMock(CirclesService::class); + $this->logger = $this->createMock(LoggerInterface::class); $userSession = $this->createMock(IUserSession::class); $user = $this->createMock(IUser::class); @@ -155,6 +159,7 @@ public function setUp(): void { $this->storage, $this->l10n, \OCP\Server::get(IEventDispatcher::class), + $this->logger, ); } @@ -632,6 +637,7 @@ public function testGetPermissions_NotLoggedIn() { $this->storage, $this->l10n, \OCP\Server::get(IEventDispatcher::class), + $this->logger, ); $form = new Form(); @@ -871,6 +877,7 @@ public function testPublicCanSubmit() { $this->storage, $this->l10n, \OCP\Server::get(IEventDispatcher::class), + $this->logger, ); $this->assertEquals(true, $formsService->canSubmit($form)); @@ -982,6 +989,7 @@ public function testHasUserAccess_NotLoggedIn() { $this->storage, $this->l10n, \OCP\Server::get(IEventDispatcher::class), + $this->logger, ); $form = new Form(); @@ -1213,6 +1221,7 @@ public function testNotifyNewSubmission($shares, $shareNotifications) { $this->storage, $this->l10n, $eventDispatcher, + $this->logger, ]) ->getMock();