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

♻️(frontend) improve handleAIError #378

Merged
merged 2 commits into from
Oct 23, 2024
Merged

♻️(frontend) improve handleAIError #378

merged 2 commits into from
Oct 23, 2024

Conversation

AntoLC
Copy link
Collaborator

@AntoLC AntoLC commented Oct 23, 2024

Purpose

To display the throttle error messages, we are doing a condition on the error message that we get from the backend.
It is error prone because the backend error message are internationalized.

It fixes partially this issue: #373

@AntoLC AntoLC added bug Something isn't working frontend labels Oct 23, 2024
@AntoLC AntoLC self-assigned this Oct 23, 2024
@AntoLC AntoLC force-pushed the fix/doc-ai-error branch 3 times, most recently from bfbf295 to 3c98c65 Compare October 23, 2024 13:16
@AntoLC AntoLC requested a review from PanchoutNathan October 23, 2024 13:52
Copy link
Collaborator

@PanchoutNathan PanchoutNathan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blocking things, LGTM ✅


useEffect(() => {
const languages = useMemo(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No point in doing a useMemo here because it is not a heavy processing

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is still doing a sorting on a dict of 100 elements.

Comment on lines 332 to 340
const handleAIError = (error: unknown) => {
if (isAPIError(error)) {
if (error.status === 429) {
toast(
t('Too many requests. Please wait 60 seconds.'),
VariantType.ERROR,
);

return;
}
}

toast(t('AI seems busy! Please try again.'), VariantType.ERROR);
console.error(error);
},
[toast, t],
);
toast(t('AI seems busy! Please try again.'), VariantType.ERROR);
console.error(error);
};

return handleAIError;
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you authorize consoles.error?
You can simplify this hook :

const useHandleAIError = () => {
  const { toast } = useToastProvider();
  const { t } = useTranslation();

  return (error: unknown) => {
    if (isAPIError(error) && error.status === 429) {
      toast(t('Too many requests. Please wait 60 seconds.'), VariantType.ERROR);
      return;
    }

    toast(t('AI seems busy! Please try again.'), VariantType.ERROR);
  };
};

Comment on lines +296 to +298
if (!responseAI) {
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of this generic way. I would explain more because Depending on the type, it can only be a string and never undefined or null. So I would try a string

if (responseAI === '' ) {
    return;
}

@AntoLC AntoLC force-pushed the fix/doc-ai-error branch 2 times, most recently from 75c87bd to 306549c Compare October 23, 2024 15:59
@AntoLC AntoLC enabled auto-merge (rebase) October 23, 2024 15:59
To display the throttle error messages,
we are doing a condition on the error message
that we get from the backend.
It is error prone because the backend error
message are internationalized.
This commit fixes this issue.
It DRY the component as well.
Fix a flaky tests on the e2e test:
- "it renders correctly when we switch from one doc
to another"
- "it saves the doc when we change pages"
@AntoLC AntoLC merged commit af5ffc2 into main Oct 23, 2024
15 of 16 checks passed
@AntoLC AntoLC deleted the fix/doc-ai-error branch October 23, 2024 16:11
This was referenced Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working frontend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants