diff --git a/apps/files/src/utils/davUtils.ts b/apps/files/src/utils/davUtils.ts index 38137a04d16a4..cf49df5b278e0 100644 --- a/apps/files/src/utils/davUtils.ts +++ b/apps/files/src/utils/davUtils.ts @@ -4,6 +4,8 @@ */ import { getCurrentUser } from '@nextcloud/auth' +import { t } from '@nextcloud/l10n' +import type { WebDAVClientError } from 'webdav' /** * Check whether this is a public share @@ -21,3 +23,37 @@ export function getToken() { const tokenElement = document.getElementById('sharingToken') as (HTMLInputElement | null) return tokenElement?.value } + +/** + * Whether error is a WebDAVClientError + * @param error - Any exception + * @return {boolean} - Whether error is a WebDAVClientError + */ +function isWebDAVClientError(error: unknown): error is WebDAVClientError { + return error instanceof Error && 'status' in error && 'response' in error +} + +/** + * Get a localized error message from webdav request + * @param error - An exception from webdav request + * @return {string} Localized error message for end user + */ +export function humanizeWebDAVError(error: unknown) { + if (error instanceof Error) { + if (isWebDAVClientError(error)) { + const status = error.status || error.response?.status || 0 + if ([400, 404, 405].includes(status)) { + return t('files', 'Folder not found') + } else if (status === 403) { + return t('files', 'This operation is forbidden') + } else if (status === 500) { + return t('files', 'This directory is unavailable, please check the logs or contact the administrator') + } else if (status === 503) { + return t('files', 'Storage is temporarily not available') + } + } + return t('files', 'Unexpected error: {error}', { error: error.message }) + } + + return t('files', 'Unknown error') +} diff --git a/apps/files/src/views/FilesList.vue b/apps/files/src/views/FilesList.vue index c157dd804a31a..fdfbed6a4e31a 100644 --- a/apps/files/src/views/FilesList.vue +++ b/apps/files/src/views/FilesList.vue @@ -131,7 +131,6 @@