From 77c64cf8683dab8623377675706ff2f2148cbf9f Mon Sep 17 00:00:00 2001 From: icarus Date: Sat, 20 Sep 2025 00:52:39 +0800 Subject: [PATCH] refactor(hooks): improve error handling in useSessions hook - Remove redundant agentId checks as they're handled by the API client - Add consistent error formatting using formatErrorMessageWithPrefix - Update error messages for all session operations --- src/renderer/src/hooks/agents/useSessions.ts | 23 ++++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/renderer/src/hooks/agents/useSessions.ts b/src/renderer/src/hooks/agents/useSessions.ts index 7471edd5d8..1ed40d2f1b 100644 --- a/src/renderer/src/hooks/agents/useSessions.ts +++ b/src/renderer/src/hooks/agents/useSessions.ts @@ -1,4 +1,5 @@ import { CreateSessionForm, UpdateSessionForm } from '@renderer/types' +import { formatErrorMessageWithPrefix } from '@renderer/utils/error' import { useCallback } from 'react' import { useTranslation } from 'react-i18next' import useSWR from 'swr' @@ -11,9 +12,6 @@ export const useSessions = (agentId: string) => { const key = client.getSessionPaths(agentId).base const fetcher = async () => { - if (!agentId) { - return [] - } const data = await client.listSessions(agentId) return data.data } @@ -21,12 +19,11 @@ export const useSessions = (agentId: string) => { const createSession = useCallback( async (form: CreateSessionForm) => { - if (!agentId) return try { const result = await client.createSession(agentId, form) mutate((prev) => [...(prev ?? []), result]) } catch (error) { - window.toast.error(t('agent.session.create.error.failed')) + window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.create.error.failed'))) } }, [agentId, client, mutate, t] @@ -35,12 +32,14 @@ export const useSessions = (agentId: string) => { // TODO: including messages field const getSession = useCallback( async (id: string) => { - if (!agentId) return - const result = await client.getSession(agentId, id) - mutate((prev) => prev?.map((session) => (session.id === result.id ? result : session))) - return result + try { + const result = await client.getSession(agentId, id) + mutate((prev) => prev?.map((session) => (session.id === result.id ? result : session))) + } catch (error) { + window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.get.error.failed'))) + } }, - [agentId, client, mutate] + [agentId, client, mutate, t] ) const deleteSession = useCallback( @@ -50,7 +49,7 @@ export const useSessions = (agentId: string) => { await client.deleteSession(agentId, id) mutate((prev) => prev?.filter((session) => session.id !== id)) } catch (error) { - window.toast.error(t('agent.session.delete.error.failed')) + window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.delete.error.failed'))) } }, [agentId, client, mutate, t] @@ -63,7 +62,7 @@ export const useSessions = (agentId: string) => { const result = await client.updateSession(agentId, form) mutate((prev) => prev?.map((session) => (session.id === form.id ? result : session))) } catch (error) { - window.toast.error(t('agent.session.update.error.failed')) + window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.update.error.failed'))) } }, [agentId, client, mutate, t]