admin/home: show error if custom update fails

Massimo Melina committed Mar 17, 2024 at 23:00 UTC a86c38adc12d602e4a46855bfb846fb9a71aca1a
5 files changed +9 -5
admin/src/HomePage.ts
+3 -1
@@ -143,7 +143,9 @@ function renderChangelog(s: string) {
143 async function update(tag?: string) {
144 if (!await confirmDialog("Installation may take less than a minute, depending on the speed of your server")) return
145 toast('Downloading')
146 - await apiCall('update', { tag })
146 + const err = await apiCall('update', { tag }).then(() => 0, e => e)
147 + if (err)
148 + return alertDialog(err)
149 toast("Restarting")
150 const restarting = Date.now()
151 let warning: undefined | ReturnType<typeof alertDialog>
admin/src/dialog.ts
+2 -1
@@ -14,6 +14,7 @@ import { useDark } from './theme'
14 import { useWindowSize } from 'usehooks-ts'
15 import md from './md'
16 import _ from 'lodash'
17 +import { err2msg } from './misc'
18 export * from '@hfs/shared/dialogs'
19
20 dialogsDefaults.Container = function Container(d: DialogOptions) {
@@ -84,7 +85,7 @@ export function alertDialog(msg: ReactElement | string | Error, options?: AlertT
85 const opt = typeof options === 'string' ? { type: options } : (options ?? {})
86 let { type='info', ...rest } = opt
87 if (msg instanceof Error) {
87 - msg = msg.message || String(msg)
88 + msg = msg.message || String(err2msg((msg as any).code))
89 type = 'error'
90 }
91
admin/src/misc.ts
+2 -1
@@ -1,6 +1,7 @@
1 // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 import { apiCall } from './api'
4 +import { HTTP_MESSAGES } from '@hfs/shared'
5 export * from '@hfs/shared'
6
7 export async function manipulateConfig(k: string, work:(data:any) => any) {
@@ -16,5 +17,5 @@ export function err2msg(code: string) {
17 github_quota: "Request denied. You may have reached the limit, retry later.",
18 ENOENT: "Not found",
19 ENOTDIR: "Not a folder",
19 - }[code] || code
20 + }[code] || HTTP_MESSAGES[code as any] || code
21 }
src/adminApis.ts
+1 -1
@@ -72,7 +72,7 @@ export const adminApis: ApiHandlers = {
72 }
73 },
74 set_config_text: ({ text }) => configFile.save(text, { reparse: true }),
75 - update: ({ tag }) => update(tag),
75 + update: ({ tag }) => update(tag).catch(e => e.cause?.statusCode ? new ApiError(e.cause?.statusCode) : e),
76 async check_update() {
77 return { options: await getUpdates() }
78 },
src/apiMiddleware.ts
+1 -1
@@ -52,7 +52,7 @@ export function apiMiddleware(apis: ApiHandlers) : Koa.Middleware {
52 }
53 catch(e) {
54 if (typeof e === 'number')
55 - e = new ApiError(HTTP_NOT_FOUND)
55 + e = new ApiError(e)
56 res = e
57 }
58 if (isAsyncGenerator(res))