fix: show a proper error message on download failure (#1345)

This commit is contained in:
Louis 2024-01-04 23:00:29 +07:00 committed by GitHub
parent f11a59bece
commit d0edcbb8b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 18 deletions

View File

@ -22,8 +22,12 @@ export default function EventListenerWrapper({ children }: PropsWithChildren) {
const modelsRef = useRef(models) const modelsRef = useRef(models)
const { setDownloadedModels, downloadedModels } = useGetDownloadedModels() const { setDownloadedModels, downloadedModels } = useGetDownloadedModels()
const { setDownloadState, setDownloadStateSuccess, setDownloadStateFailed } = const {
useDownloadState() setDownloadState,
setDownloadStateSuccess,
setDownloadStateFailed,
setDownloadStateCancelled,
} = useDownloadState()
const downloadedModelRef = useRef(downloadedModels) const downloadedModelRef = useRef(downloadedModels)
useEffect(() => { useEffect(() => {
@ -52,13 +56,18 @@ export default function EventListenerWrapper({ children }: PropsWithChildren) {
window.electronAPI.onFileDownloadError( window.electronAPI.onFileDownloadError(
async (_event: string, state: any) => { async (_event: string, state: any) => {
if (state.err?.message !== 'aborted')
console.error('Download error', state)
const modelName = await baseName(state.fileName) const modelName = await baseName(state.fileName)
const model = modelsRef.current.find( const model = modelsRef.current.find(
(model) => modelBinFileName(model) === modelName (model) => modelBinFileName(model) === modelName
) )
if (model) setDownloadStateFailed(model.id) if (model) {
if (state.err?.message !== 'aborted') {
console.error('Download error', state)
setDownloadStateFailed(model.id, state.err.message)
} else {
setDownloadStateCancelled(model.id)
}
}
} }
) )

View File

@ -29,26 +29,49 @@ const setDownloadStateSuccessAtom = atom(null, (get, set, modelId: string) => {
}) })
}) })
const setDownloadStateFailedAtom = atom(null, (get, set, modelId: string) => { const setDownloadStateFailedAtom = atom(
const currentState = { ...get(modelDownloadStateAtom) } null,
const state = currentState[modelId] (get, set, modelId: string, error: string) => {
if (!state) { const currentState = { ...get(modelDownloadStateAtom) }
console.debug(`Cannot find download state for ${modelId}`) const state = currentState[modelId]
toaster({ if (!state) {
title: 'Cancel Download', console.debug(`Cannot find download state for ${modelId}`)
description: `Model ${modelId} cancel download`, toaster({
}) title: 'Download Failed',
return description: `Model ${modelId} download failed: ${error}`,
type: 'error',
})
return
}
delete currentState[modelId]
set(modelDownloadStateAtom, currentState)
} }
delete currentState[modelId] )
set(modelDownloadStateAtom, currentState) const setDownloadStateCancelledAtom = atom(
}) null,
(get, set, modelId: string) => {
const currentState = { ...get(modelDownloadStateAtom) }
const state = currentState[modelId]
if (!state) {
console.debug(`Cannot find download state for ${modelId}`)
toaster({
title: 'Cancel Download',
description: `Model ${modelId} cancel download`,
})
return
}
delete currentState[modelId]
set(modelDownloadStateAtom, currentState)
}
)
export function useDownloadState() { export function useDownloadState() {
const modelDownloadState = useAtomValue(modelDownloadStateAtom) const modelDownloadState = useAtomValue(modelDownloadStateAtom)
const setDownloadState = useSetAtom(setDownloadStateAtom) const setDownloadState = useSetAtom(setDownloadStateAtom)
const setDownloadStateSuccess = useSetAtom(setDownloadStateSuccessAtom) const setDownloadStateSuccess = useSetAtom(setDownloadStateSuccessAtom)
const setDownloadStateFailed = useSetAtom(setDownloadStateFailedAtom) const setDownloadStateFailed = useSetAtom(setDownloadStateFailedAtom)
const setDownloadStateCancelled = useSetAtom(setDownloadStateCancelledAtom)
const downloadStates: DownloadState[] = [] const downloadStates: DownloadState[] = []
for (const [, value] of Object.entries(modelDownloadState)) { for (const [, value] of Object.entries(modelDownloadState)) {
@ -61,6 +84,7 @@ export function useDownloadState() {
setDownloadState, setDownloadState,
setDownloadStateSuccess, setDownloadStateSuccess,
setDownloadStateFailed, setDownloadStateFailed,
setDownloadStateCancelled,
downloadStates, downloadStates,
} }
} }