Skip to content

Commit

Permalink
[editor] Debounced Error Handling for Prompt Name and Input (#724)
Browse files Browse the repository at this point in the history
[editor] Debounced Error Handling for Prompt Name and Input


# [editor] Debounced Error Handling for Prompt Name and Input

`debounced` callbacks won't propagate thrown errors so we should use an
`onError` callback to handle those. This PR handles them for updating
prompt name and input.

## Testing:
Added the following in /update_prompt endpoint to return an error:
```
return HttpResponseWithAIConfig(
        #
        message=f"Failed to update prompt",
        code=500,
        aiconfig=None,
    ).to_flask_format()
```

See notifications:


https://github.com/lastmile-ai/aiconfig/assets/5060851/04270541-eca9-4d41-8b7c-1e456d992814

---
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with
[ReviewStack](https://reviewstack.dev/lastmile-ai/aiconfig/pull/724).
* #725
* __->__ #724
  • Loading branch information
rholinshead authored Jan 3, 2024
2 parents 559b6f7 + 500829a commit 1c3e844
Showing 1 changed file with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,19 @@ export default function EditorContainer({
async (
promptName: string,
newPrompt: Prompt,
onSuccess?: (aiconfigRes: AIConfig) => void
onSuccess: (aiconfigRes: AIConfig) => void,
onError: (err: unknown) => void
) => {
const serverConfigRes = await updatePromptCallback(
promptName,
newPrompt
);
if (serverConfigRes && onSuccess) {
onSuccess(serverConfigRes.aiconfig);
try {
const serverConfigRes = await updatePromptCallback(
promptName,
newPrompt
);
if (serverConfigRes?.aiconfig) {
onSuccess(serverConfigRes.aiconfig);
}
} catch (err: unknown) {
onError(err);
}
},
DEBOUNCE_MS
Expand All @@ -152,6 +157,15 @@ export default function EditorContainer({

dispatch(action);

const onError = (err: unknown) => {
const message = (err as RequestCallbackError).message ?? null;
showNotification({
title: "Error updating prompt input",
message,
color: "red",
});
};

try {
const statePrompt = getPrompt(stateRef.current, promptId);
if (!statePrompt) {
Expand All @@ -165,27 +179,32 @@ export default function EditorContainer({
...prompt,
input: newPromptInput,
},
(serverConfigRes) =>
(config) =>
dispatch({
type: "CONSOLIDATE_AICONFIG",
action,
config: serverConfigRes,
})
config,
}),
onError
);
} catch (err: unknown) {
const message = (err as RequestCallbackError).message ?? null;
showNotification({
title: "Error updating prompt input",
message,
color: "red",
});
onError(err);
}
},
[debouncedUpdatePrompt, dispatch]
);

const onChangePromptName = useCallback(
async (promptId: string, newName: string) => {
const onError = (err: unknown) => {
const message = err instanceof Error ? err.message : null;
showNotification({
title: "Error updating prompt name",
message,
color: "red",
});
};

try {
const statePrompt = getPrompt(stateRef.current, promptId);
if (!statePrompt) {
Expand All @@ -207,15 +226,11 @@ export default function EditorContainer({
type: "UPDATE_PROMPT_NAME",
id: promptId,
name: newName,
})
}),
onError
);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : null;
showNotification({
title: "Error updating prompt name",
message,
color: "red",
});
onError(err);
}
},
[debouncedUpdatePrompt]
Expand Down

0 comments on commit 1c3e844

Please sign in to comment.