Files
twenty/packages/twenty-front/src/utils/get-error-message-from-apollo-error.util.ts
T
Lucas Bordeau a453d53656 Fix Snackbar crashing the whole app (#15774)
This PR fixes a crash of the app when a Snackbar tries to render an
object.

<img width="3002" height="754" alt="image"
src="https://github.com/user-attachments/assets/988f97eb-5c8a-44f8-ac8e-e2953b872bac"
/>

What happened : the backend sent a MessageDescriptor in
`extensions.userFriendlyMessage` while it should have sent a translated
string.

But it is a problem that the Snackbar can end up rendering objects and
crashing the whole app because it is a the highest level in the tree.

So this PR hardens many points to avoid future bugs : 
- Sanitize what is rendered by the Snackbar to avoid any crash
- Translate any MessageDescriptor object that could end up in the
frontend
- Fix the places in the backend where a MessageDescriptor wasn't
translated and sent directly to the frontend in
`use-graphql-error-handler.hook.ts`

Fixes https://github.com/twentyhq/twenty/issues/15685
Fixes https://github.com/twentyhq/core-team-issues/issues/1869
2025-11-13 12:01:06 +01:00

21 lines
697 B
TypeScript

import { type ApolloError } from '@apollo/client';
import { type MessageDescriptor } from '@lingui/core';
import { t } from '@lingui/core/macro';
import { type Nullable } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
export const getErrorMessageFromApolloError = (error: ApolloError): string => {
const userFriendlyMessage = error.graphQLErrors?.[0]?.extensions
?.userFriendlyMessage as Nullable<MessageDescriptor | string>;
if (!isDefined(userFriendlyMessage)) {
return t`An error occurred.`;
}
if (typeof userFriendlyMessage === 'object' && 'id' in userFriendlyMessage) {
return t(userFriendlyMessage);
}
return userFriendlyMessage;
};