fix: admin: missing error handling while picking a file on server

Massimo Melina committed Jun 6, 2022 at 14:56 UTC 27c6aed8145f43873e5dee7282a93d12cef7666a
6 files changed +16 -6
admin/src/FilePicker.ts
+2 -2
@@ -14,7 +14,7 @@ import {
14 TextField,
15 Typography
16 } from '@mui/material'
17 -import { enforceFinal, formatBytes, isWindowsDrive, spinner, Center } from './misc'
17 +import { enforceFinal, formatBytes, isWindowsDrive, spinner, Center, err2msg } from './misc'
18 import { ArrowUpward, Home } from '@mui/icons-material'
19 import { StringField } from '@hfs/mui-grid-form'
20 import { FileIcon, FolderIcon } from './VfsTree'
@@ -79,7 +79,7 @@ export default function FilePicker({ onSelect, multiple=true, files=true, folder
79 onChange: setCwd as any,
80 }),
81 ),
82 - error ? h(Alert, { severity:'error' }, String(error))
82 + error ? h(Alert, { severity:'error' }, err2msg(error))
83 : h(Fragment, {},
84 h(Box, { sx: { flex: 1 } },
85 !list.length ? h(Center, { flex: 1, mt: '4em' }, "No elements in this folder") : h(AutoSizer, {
admin/src/api.ts
+1
@@ -127,6 +127,7 @@ export function useApiList<T=any>(cmd:string|Falsy, params: Dict={}, { addId=fal
127 if (chunk.length)
128 setList(list => [ ...list, ...chunk ])
129 }
130 + setError(undefined)
131 setLoading(true)
132 setInitializing(true)
133 setList([])
admin/src/misc.ts
+7
@@ -126,3 +126,10 @@ export function xlate(input: any, table: Record<string, any>) {
126 export function useBreakpoint(name: Breakpoint) {
127 return useMediaQuery((theme: any) => theme.breakpoints.up(name), { noSsr:true }) // without noSsr, first execution always returns false
128 }
129 +
130 +export function err2msg(code: string) {
131 + return {
132 + ENOENT: "Not found",
133 + ENOTDIR: "Not a folder",
134 + }[code] || code
135 +}
server/src/api.vfs.ts
+2 -3
@@ -155,9 +155,8 @@ const apis: ApiHandlers = {
155 }
156 catch {} // just ignore entries we can't stat
157 }
158 - } catch (e) {
159 - if ((e as any).code !== 'ENOTDIR')
160 - throw e
158 + } catch (e: any) {
159 + yield { error: e.code || e.message || String(e) }
160 }
161 }
162
server/src/misc.ts
+3
@@ -258,6 +258,9 @@ export function isLocalHost(c: Connection | Koa.Context) {
258 }
259
260 export async function* dirStream(path: string) {
261 + const stats = await fs.stat(path)
262 + if (!stats.isDirectory())
263 + throw Error('ENOTDIR')
264 const dirStream = glob.stream('*', {
265 cwd: path,
266 dot: true,
todo.md
+1 -1
@@ -2,7 +2,7 @@
2 - plugin api to read hfs config
3 - fix: root without can_download breaks
4 - use dialogs instead of side-forms on mobile (admin/fs+accounts)
5 -- easier deploy on cloud server
5 +- easier deploy on cloud server: --create-admin <password>
6 - admin/fs: sort items
7 - plugin.api.subscribeConfig
8 - admin/fs: render virtual folders differently