From 60cf5a221dcc65fc8f454d7aad0af7dde582ae3c Mon Sep 17 00:00:00 2001 From: "Grigorii K. Shartsev" Date: Thu, 17 Oct 2024 14:10:47 +0200 Subject: [PATCH] fixup! fix(files): handle empty view with error --- apps/files/src/utils/davUtils.ts | 36 ++++++++++++++++++++++++++++++ apps/files/src/views/FilesList.vue | 19 ++-------------- 2 files changed, 38 insertions(+), 17 deletions(-) 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 @@