fix(files_sharing): fix share creation error handling

Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
This commit is contained in:
skjnldsv 2025-04-09 16:12:18 +02:00
parent c00d61949f
commit 066e3977f5
4 changed files with 21 additions and 5 deletions

View file

@ -81,6 +81,7 @@ use OCP\IUserManager;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCP\Server;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IShare;
@ -804,6 +805,9 @@ class ShareAPIController extends OCSController {
} catch (HintException $e) {
$code = $e->getCode() === 0 ? 403 : $e->getCode();
throw new OCSException($e->getHint(), $code);
} catch (GenericShareException|\InvalidArgumentException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSForbiddenException($e->getMessage(), $e);
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSForbiddenException('Failed to create share.', $e);

View file

@ -26,10 +26,12 @@
// TODO: remove when ie not supported
import 'url-search-params-polyfill'
import { emit } from '@nextcloud/event-bus'
import { showError } from '@nextcloud/dialogs'
import { generateOcsUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
import Share from '../models/Share.js'
import { emit } from '@nextcloud/event-bus'
const shareUrl = generateOcsUrl('apps/files_sharing/api/v1/shares')
@ -65,7 +67,7 @@ export default {
} catch (error) {
console.error('Error while creating share', error)
const errorMessage = error?.response?.data?.ocs?.meta?.message
OC.Notification.showTemporary(
showError(
errorMessage ? t('files_sharing', 'Error creating the share: {errorMessage}', { errorMessage }) : t('files_sharing', 'Error creating the share'),
{ type: 'error' },
)

View file

@ -230,6 +230,7 @@
</NcButton>
<NcButton type="primary"
data-cy-files-sharing-share-editor-action="save"
:disabled="creating"
@click="saveShare">
{{ shareButtonText }}
<template v-if="creating" #icon>
@ -899,8 +900,16 @@ export default {
incomingShare.password = this.share.password
}
this.creating = true
const share = await this.addShare(incomingShare)
let share
try {
this.creating = true
share = await this.addShare(incomingShare)
} catch (error) {
this.creating = false
// Error is already handled by ShareRequests mixin
return
}
// ugly hack to make code work - we need the id to be set but at the same time we need to keep values we want to update
this.share._share.id = share.id
await this.queueUpdate(...permissionsAndAttributes)
@ -914,6 +923,7 @@ export default {
}
}
}
this.share = share
this.creating = false
this.$emit('add:share', this.share)

View file

@ -2273,7 +2273,7 @@ class ManagerTest extends \Test\TestCase {
public function testPathCreateChecksContainsSharedMount() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Path contains files shared with you');
$this->expectExceptionMessage('You cannot share a folder that contains other shares');
$path = $this->createMock(Folder::class);
$path->method('getPath')->willReturn('path');