Skip to content

Commit

Permalink
fixup! fix(files): handle empty view with error
Browse files Browse the repository at this point in the history
  • Loading branch information
ShGKme committed Oct 17, 2024
1 parent db5cb12 commit 60cf5a2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
36 changes: 36 additions & 0 deletions apps/files/src/utils/davUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
}
19 changes: 2 additions & 17 deletions apps/files/src/views/FilesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@
<script lang="ts">
import type { ContentsWithRoot, INode } from '@nextcloud/files'
import type { Upload } from '@nextcloud/upload'
import type { AxiosError } from '@nextcloud/axios'
import type { CancelablePromise } from 'cancelable-promise'
import type { ComponentPublicInstance } from 'vue'
import type { Route } from 'vue-router'
Expand Down Expand Up @@ -177,6 +176,7 @@ import filesListWidthMixin from '../mixins/filesListWidth.ts'
import filesSortingMixin from '../mixins/filesSorting.ts'
import logger from '../logger.ts'
import DragAndDropNotice from '../components/DragAndDropNotice.vue'
import { humanizeWebDAVError } from '../utils/davUtils.ts'
const isSharingEnabled = (getCapabilities() as { files_sharing?: boolean })?.files_sharing !== undefined
Expand Down Expand Up @@ -557,22 +557,7 @@ export default defineComponent({
})
} catch (error) {
logger.error('Error while fetching content', { error })
if (error instanceof Error) {
const status = ('status' in error && error.status as number) || ('response' in error && (error.response as AxiosError).status) || 0
if ([400, 404, 405].includes(status)) {
this.error = t('files', 'Folder not found')
} else if (status === 403) {
this.error = t('files', 'This operation is forbidden')
} else if (status === 500) {
this.error = t('files', 'This directory is unavailable, please check the logs or contact the administrator')
} else if (status === 503) {
this.error = t('files', 'Storage is temporarily not available')
} else {
this.error = t('files', 'Unexpected error: {error}', { error: error.message })
}
} else {
this.error = t('files', 'Unknown error')
}
this.error = humanizeWebDAVError(error)
} finally {
this.loading = false
}
Expand Down

0 comments on commit 60cf5a2

Please sign in to comment.