a453d53656
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
25 lines
710 B
TypeScript
25 lines
710 B
TypeScript
import { t } from '@lingui/core/macro';
|
|
import { isDefined } from 'twenty-shared/utils';
|
|
|
|
export const sanitizeMessageToRenderInSnackbar = (
|
|
messageToRenderInSnackbar: any,
|
|
) => {
|
|
if (!isDefined(messageToRenderInSnackbar)) {
|
|
return null;
|
|
} else if (
|
|
typeof messageToRenderInSnackbar === 'string' ||
|
|
typeof messageToRenderInSnackbar === 'string' ||
|
|
typeof messageToRenderInSnackbar === 'boolean'
|
|
) {
|
|
return `${messageToRenderInSnackbar}`;
|
|
} else if (typeof messageToRenderInSnackbar === 'object') {
|
|
try {
|
|
return JSON.stringify(messageToRenderInSnackbar);
|
|
} catch {
|
|
return t`Cannot display message`;
|
|
}
|
|
} else {
|
|
return t`Cannot display message`;
|
|
}
|
|
};
|