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:
@@ -1,5 +1,6 @@
|
||||
import { type MessageDescriptor } from '@lingui/core';
|
||||
import { msg } from '@lingui/core/macro';
|
||||
import { assertUnreachable } from 'twenty-shared/utils';
|
||||
|
||||
import { CustomException } from 'src/utils/custom-exception';
|
||||
|
||||
@@ -10,14 +11,19 @@ export enum UserExceptionCode {
|
||||
EMAIL_UPDATE_RESTRICTED_TO_SINGLE_WORKSPACE = 'EMAIL_UPDATE_RESTRICTED_TO_SINGLE_WORKSPACE',
|
||||
}
|
||||
|
||||
const userExceptionUserFriendlyMessages: Record<
|
||||
UserExceptionCode,
|
||||
MessageDescriptor
|
||||
> = {
|
||||
[UserExceptionCode.USER_NOT_FOUND]: msg`User not found.`,
|
||||
[UserExceptionCode.EMAIL_ALREADY_IN_USE]: msg`This email is already in use.`,
|
||||
[UserExceptionCode.EMAIL_UNCHANGED]: msg`Email is unchanged.`,
|
||||
[UserExceptionCode.EMAIL_UPDATE_RESTRICTED_TO_SINGLE_WORKSPACE]: msg`Email update is restricted to single workspace users.`,
|
||||
const getUserExceptionUserFriendlyMessage = (code: UserExceptionCode) => {
|
||||
switch (code) {
|
||||
case UserExceptionCode.USER_NOT_FOUND:
|
||||
return msg`User not found.`;
|
||||
case UserExceptionCode.EMAIL_ALREADY_IN_USE:
|
||||
return msg`This email is already in use.`;
|
||||
case UserExceptionCode.EMAIL_UNCHANGED:
|
||||
return msg`Email is unchanged.`;
|
||||
case UserExceptionCode.EMAIL_UPDATE_RESTRICTED_TO_SINGLE_WORKSPACE:
|
||||
return msg`Email update is restricted to single workspace users.`;
|
||||
default:
|
||||
assertUnreachable(code);
|
||||
}
|
||||
};
|
||||
|
||||
export class UserException extends CustomException<UserExceptionCode> {
|
||||
@@ -28,7 +34,7 @@ export class UserException extends CustomException<UserExceptionCode> {
|
||||
) {
|
||||
super(message, code, {
|
||||
userFriendlyMessage:
|
||||
userFriendlyMessage ?? userExceptionUserFriendlyMessages[code],
|
||||
userFriendlyMessage ?? getUserExceptionUserFriendlyMessage(code),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user