mrrobot16 / tax-app-ui-v0

https://irs-copilot.vercel.app
2 stars 1 forks source link

Improve error handling for Chat container. #8

Open mrrobot16 opened 8 months ago

mrrobot16 commented 8 months ago

In src/containers/Chat.tsx

    setErrorMessage(`
    We apologize for the inconvenience, our system is having load issues. 
    Our team is working to improve performance try again in a few minutes.
    Thank you for your patience.
    `);
    setCodeRed(true);
    setLoading(false);
  };

  const setStatusErrorMessages = (error?: string | AxiosError | unknown) => {
    setCodeRed(true);
    setErrorMessage(`
    We apologize for the inconvenience, our system is undergoing maintenance to improve your experience. 
    Thank you for your patience.
    `);
  };

Need to refactor these callback functions that are being passed to src/services/openai.ts.

In src/services/openai.ts

export async function newConversationMessage(
    user_id: string,
    conversation_id: string,
    message: Message,
    callback?: (error?: string | null | boolean | Error | unknown) => void
) {
    try {
        const url = `${API_BASE_URL}/conversations/message/new/openai/${conversation_id}`;
        const body = {
            user_id,
            conversation_id,
            message,
        };
        const response = await axios.post(url, body);

        return response;
    } catch (error: AxiosError | unknown) {
        console.error('Error with new conversation with openai', error);

        if(callback) {
            callback(error);
        }

        throw error;
    }
}

type openAIStatusCallback = {
    (error?: string | AxiosError | unknown): void;
}

export async function openAIStatus(callback: openAIStatusCallback) : Promise<{ status: number | undefined, error?: AxiosError }> {
    try {
        const url = `${API_BASE_URL}/openai/status`;
        const response = await axios.get(url);

        return response;
    } catch (error: AxiosError | unknown) {
        console.error('Error with checking openai status', error);
        callback(error);

        return {
            error: error as AxiosError,
            status: (error as AxiosError).response?.status,
        };
    }
}

export async function apiStatus(callback?: (message: string) => void) {
    try {
        const url = `${API_BASE_URL}/`;
        const response = await axios.get(url);

        return response;
    } catch (error: AxiosError | unknown) {
        console.error('Error with checking api status', error);

        return {
            error: error as AxiosError,
            status: (error as AxiosError).response?.status,
        };
    }
}
mrrobot16 commented 8 months ago

Idea: Make the error message an "assistant message"

Example:

Screenshot 2023-11-09 at 11 10 21 AM