Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display errors encountered during conversation imports #240

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ChatAddRegular } from '@fluentui/react-icons';
import React from 'react';
import { useConversationUtility } from '../../../libs/useConversationUtility';
import { useCreateConversation } from '../../../libs/useCreateConversation';
import { useNotify } from '../../../libs/useNotify';
import { DialogControl } from '../../App/DialogControl';
import { ConversationsImport } from '../../Conversations/ConversationsImport';
import { NewConversationForm } from './NewConversationForm';
Expand All @@ -17,6 +18,7 @@ export const NewConversationButton: React.FC = () => {
const [assistantServiceId, setAssistantServiceId] = React.useState<string>();
const [submitted, setSubmitted] = React.useState(false);
const { navigateToConversation } = useConversationUtility();
const { notifyError } = useNotify();

const handleCreate = React.useCallback(async () => {
if (submitted || !isValid || !title || !assistantId) {
Expand Down Expand Up @@ -57,6 +59,16 @@ export const NewConversationButton: React.FC = () => {
[navigateToConversation],
);

const handleError = React.useCallback(
(error: Error) =>
notifyError({
id: 'new-conversation-error',
title: 'Error creating conversation',
message: error.message,
}),
[notifyError],
);

return (
<DialogControl
open={open}
Expand All @@ -78,7 +90,13 @@ export const NewConversationButton: React.FC = () => {
}
hideDismissButton
additionalActions={[
<ConversationsImport key="import" appearance="outline" onImport={handleImport} disabled={submitted} />,
<ConversationsImport
key="import"
appearance="outline"
onImport={handleImport}
onError={handleError}
disabled={submitted}
/>,
<DialogTrigger key="cancel" disableButtonEnhancement>
<Button appearance="secondary" disabled={submitted}>
Cancel
Expand Down
62 changes: 31 additions & 31 deletions workbench-app/src/libs/useWorkbenchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,23 @@ export const useWorkbenchService = () => {
async (operationTitle: string, url: string, options?: RequestInit): Promise<Response> => {
const accessToken = await getAccessTokenAsync();
const idToken = await getIdTokenAsync();
try {
const response = await fetch(url, {
...options,
headers: {
...options?.headers,
Authorization: `Bearer ${accessToken}`,
'X-OpenIdToken': idToken,
},
});

if (!response.ok) {
dispatch(addError({ title: operationTitle, message: response.statusText }));
throw new Error(`failed to fetch: ${response.statusText}`);
}
const response = await fetch(url, {
...options,
headers: {
...options?.headers,
Authorization: `Bearer ${accessToken}`,
'X-OpenIdToken': idToken,
},
});

return response;
} catch (error) {
dispatch(addError({ title: operationTitle, message: (error as Error).message }));
throw error;
if (!response.ok) {
const json = await response.json();
const message = json?.detail ?? json?.detail ?? response.statusText;
dispatch(addError({ title: operationTitle, message }));
throw new Error(`Failed to ${operationTitle}: ${message}`);
}

return response;
},
[dispatch, getAccessTokenAsync, getIdTokenAsync],
);
Expand All @@ -106,20 +103,23 @@ export const useWorkbenchService = () => {
async (operationTitle: string, url: string, options?: RequestInit): Promise<Response> => {
const accessToken = await getAccessTokenAsync();
const idToken = await getIdTokenAsync();
try {
const response = await fetch(url, {
...options,
headers: {
...options?.headers,
Authorization: `Bearer ${accessToken}`,
'X-OpenIdToken': idToken,
},
});
return response;
} catch (error) {
dispatch(addError({ title: operationTitle, message: (error as Error).message }));
throw error;
const response = await fetch(url, {
...options,
headers: {
...options?.headers,
Authorization: `Bearer ${accessToken}`,
'X-OpenIdToken': idToken,
},
});

if (!response.ok) {
const json = await response.json();
const message = json?.detail ?? json?.detail ?? response.statusText;
dispatch(addError({ title: operationTitle, message }));
throw new Error(`Failed to ${operationTitle}: ${message}`);
}

return response;
},
[dispatch, getAccessTokenAsync, getIdTokenAsync],
);
Expand Down