Improve userFriendlyMessage devX (#16815)

Two challenges with error messages
- always provide a useful/meaningful error message for the end user
instead of the generic one. eg: show "Wrong password" and not "An error
occured"
- avoid technical details unless error regards a technical feature. eg:
show "An error occured" and not "Invalid post-hook payload."; but do
show "Invalid issuer URL." as it occurs while configuring SSO

What this PR does
- Make userFriendlyMessage mandatory for widely used
GraphqlQueryRunnerException and CommonQueryRunnerException, so that
developers are forced to ask themselves what the error message should
be, and as it contains very wide error codes (eg: "Bad request") which
should not be mapped to just one default message
- Keep userFriendlyMessage optional for service-specific exceptions (eg:
workflowStepExecutorException), but convert the error code to
userFriendlyMessage mapper to a switch case function with a typecheck
ensuring that all codes are mapped to a message. These default messages
are still overridable where they are thrown.
This commit is contained in:
Marie
2025-12-30 10:08:43 +01:00
committed by GitHub
parent 7522ff6675
commit 19c9f957b1
135 changed files with 1672 additions and 845 deletions
@@ -1,6 +1,8 @@
import { type MessageDescriptor } from '@lingui/core';
import { msg } from '@lingui/core/macro';
import { assertUnreachable } from 'twenty-shared/utils';
import { STANDARD_ERROR_MESSAGE } from 'src/engine/api/common/common-query-runners/errors/standard-error-message.constant';
import {
appendCommonExceptionCode,
CustomException,
@@ -11,13 +13,19 @@ export const FileExceptionCode = appendCommonExceptionCode({
FILE_NOT_FOUND: 'FILE_NOT_FOUND',
} as const);
const fileExceptionUserFriendlyMessages: Record<
keyof typeof FileExceptionCode,
MessageDescriptor
> = {
UNAUTHENTICATED: msg`Authentication is required.`,
FILE_NOT_FOUND: msg`File not found.`,
INTERNAL_SERVER_ERROR: msg`An unexpected error occurred.`,
const getFileExceptionUserFriendlyMessage = (
code: keyof typeof FileExceptionCode,
) => {
switch (code) {
case FileExceptionCode.UNAUTHENTICATED:
return msg`Authentication is required.`;
case FileExceptionCode.FILE_NOT_FOUND:
return msg`File not found.`;
case FileExceptionCode.INTERNAL_SERVER_ERROR:
return STANDARD_ERROR_MESSAGE;
default:
assertUnreachable(code);
}
};
export class FileException extends CustomException<
@@ -30,7 +38,7 @@ export class FileException extends CustomException<
) {
super(message, code, {
userFriendlyMessage:
userFriendlyMessage ?? fileExceptionUserFriendlyMessages[code],
userFriendlyMessage ?? getFileExceptionUserFriendlyMessage(code),
});
}
}