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'
107import { showError } from '@nextcloud/dialogs'
8+ import { emit } from '@nextcloud/event-bus'
119import { generateOcsUrl } from '@nextcloud/router'
12- import axios from '@nextcloud/axios'
13-
1410import Share from '../models/Share.ts'
11+ import logger from '../services/logger.ts'
1512
1613const 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+ }
0 commit comments