Skip to content

Commit

Permalink
fix: show a proper error message on download failure (janhq#1345)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored Jan 4, 2024
1 parent f11a59b commit d0edcbb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
19 changes: 14 additions & 5 deletions web/containers/Providers/EventListener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ export default function EventListenerWrapper({ children }: PropsWithChildren) {
const modelsRef = useRef(models)

const { setDownloadedModels, downloadedModels } = useGetDownloadedModels()
const { setDownloadState, setDownloadStateSuccess, setDownloadStateFailed } =
useDownloadState()
const {
setDownloadState,
setDownloadStateSuccess,
setDownloadStateFailed,
setDownloadStateCancelled,
} = useDownloadState()
const downloadedModelRef = useRef(downloadedModels)

useEffect(() => {
Expand Down Expand Up @@ -52,13 +56,18 @@ export default function EventListenerWrapper({ children }: PropsWithChildren) {

window.electronAPI.onFileDownloadError(
async (_event: string, state: any) => {
if (state.err?.message !== 'aborted')
console.error('Download error', state)
const modelName = await baseName(state.fileName)
const model = modelsRef.current.find(
(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)
}
}
}
)

Expand Down
50 changes: 37 additions & 13 deletions web/hooks/useDownloadState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,49 @@ const setDownloadStateSuccessAtom = atom(null, (get, set, modelId: string) => {
})
})

const setDownloadStateFailedAtom = 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
const setDownloadStateFailedAtom = atom(
null,
(get, set, modelId: string, error: string) => {
const currentState = { ...get(modelDownloadStateAtom) }
const state = currentState[modelId]
if (!state) {
console.debug(`Cannot find download state for ${modelId}`)
toaster({
title: 'Download Failed',
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() {
const modelDownloadState = useAtomValue(modelDownloadStateAtom)
const setDownloadState = useSetAtom(setDownloadStateAtom)
const setDownloadStateSuccess = useSetAtom(setDownloadStateSuccessAtom)
const setDownloadStateFailed = useSetAtom(setDownloadStateFailedAtom)
const setDownloadStateCancelled = useSetAtom(setDownloadStateCancelledAtom)

const downloadStates: DownloadState[] = []
for (const [, value] of Object.entries(modelDownloadState)) {
Expand All @@ -61,6 +84,7 @@ export function useDownloadState() {
setDownloadState,
setDownloadStateSuccess,
setDownloadStateFailed,
setDownloadStateCancelled,
downloadStates,
}
}

0 comments on commit d0edcbb

Please sign in to comment.