Skip to content

Commit 4a211b3

Browse files
committed
fix(files_sharing): ensure the server share API errors are shown
- fix #58359 To reproduce: 1. Setup password policy. 2. Try to set a share password like `1234` 3. See that no visual error message is show but only in the console Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent f540b1a commit 4a211b3

3 files changed

Lines changed: 35 additions & 43 deletions

File tree

apps/files_sharing/src/mixins/ShareRequests.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
// TODO: remove when ie not supported
7-
import 'url-search-params-polyfill'
8-
9-
import { emit } from '@nextcloud/event-bus'
6+
import axios, { isAxiosError } from '@nextcloud/axios'
107
import { showError } from '@nextcloud/dialogs'
8+
import { emit } from '@nextcloud/event-bus'
119
import { generateOcsUrl } from '@nextcloud/router'
12-
import axios from '@nextcloud/axios'
13-
1410
import Share from '../models/Share.ts'
11+
import logger from '../services/logger.ts'
1512

1613
const shareUrl = generateOcsUrl('apps/files_sharing/api/v1/shares')
1714

@@ -45,13 +42,9 @@ export default {
4542
emit('files_sharing:share:created', { share })
4643
return share
4744
} catch (error) {
48-
console.error('Error while creating share', error)
49-
const errorMessage = error?.response?.data?.ocs?.meta?.message
50-
showError(
51-
errorMessage ? t('files_sharing', 'Error creating the share: {errorMessage}', { errorMessage }) : t('files_sharing', 'Error creating the share'),
52-
{ type: 'error' },
53-
)
54-
throw error
45+
const errorMessage = getErrorMessage(error) ?? t('files_sharing', 'Error creating the share')
46+
showError(errorMessage)
47+
throw new Error(errorMessage, { cause: error })
5548
}
5649
},
5750

@@ -70,13 +63,9 @@ export default {
7063
emit('files_sharing:share:deleted', { id })
7164
return true
7265
} catch (error) {
73-
console.error('Error while deleting share', error)
74-
const errorMessage = error?.response?.data?.ocs?.meta?.message
75-
OC.Notification.showTemporary(
76-
errorMessage ? t('files_sharing', 'Error deleting the share: {errorMessage}', { errorMessage }) : t('files_sharing', 'Error deleting the share'),
77-
{ type: 'error' },
78-
)
79-
throw error
66+
const errorMessage = getErrorMessage(error) ?? t('files_sharing', 'Error deleting the share')
67+
showError(errorMessage)
68+
throw new Error(errorMessage, { cause: error })
8069
}
8170
},
8271

@@ -96,17 +85,27 @@ export default {
9685
return request.data.ocs.data
9786
}
9887
} catch (error) {
99-
console.error('Error while updating share', error)
100-
if (error.response.status !== 400) {
101-
const errorMessage = error?.response?.data?.ocs?.meta?.message
102-
OC.Notification.showTemporary(
103-
errorMessage ? t('files_sharing', 'Error updating the share: {errorMessage}', { errorMessage }) : t('files_sharing', 'Error updating the share'),
104-
{ type: 'error' },
105-
)
106-
}
107-
const message = error.response.data.ocs.meta.message
108-
throw new Error(message)
88+
logger.error('Error while updating share', { error })
89+
const errorMessage = getErrorMessage(error) ?? t('files_sharing', 'Error updating the share')
90+
// the error will be shown in apps/files_sharing/src/mixins/SharesMixin.js
91+
throw new Error(errorMessage, { cause: error })
10992
}
11093
},
11194
},
11295
}
96+
97+
/**
98+
* Handle an error response from the server and show a notification with the error message if possible
99+
*
100+
* @param {unknown} error - The received error
101+
* @return {string|undefined} the error message if it could be extracted from the response, otherwise undefined
102+
*/
103+
function getErrorMessage(error) {
104+
if (isAxiosError(error) && error.response.data?.ocs) {
105+
/** @type {import('@nextcloud/typings/ocs').OCSResponse} */
106+
const response = error.response.data
107+
if (response.ocs.meta?.message) {
108+
return response.ocs.meta.message
109+
}
110+
}
111+
}

package-lock.json

Lines changed: 5 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@
107107
"snap.js": "^2.0.9",
108108
"strengthify": "github:nextcloud/strengthify#0.5.9",
109109
"throttle-debounce": "^5.0.2",
110-
"underscore": "1.13.7",
111-
"url-search-params-polyfill": "^8.1.1",
110+
"underscore": "1.13.8",
112111
"v-click-outside": "^3.2.0",
113112
"v-tooltip": "^2.1.3",
114113
"vue": "^2.7.16",

0 commit comments

Comments
 (0)