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:
+17
-10
@@ -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';
|
||||
|
||||
@@ -11,15 +12,21 @@ export enum ApiKeyExceptionCode {
|
||||
ROLE_CANNOT_BE_ASSIGNED_TO_API_KEYS = 'ROLE_CANNOT_BE_ASSIGNED_TO_API_KEYS',
|
||||
}
|
||||
|
||||
const apiKeyExceptionUserFriendlyMessages: Record<
|
||||
ApiKeyExceptionCode,
|
||||
MessageDescriptor
|
||||
> = {
|
||||
[ApiKeyExceptionCode.API_KEY_NOT_FOUND]: msg`API key not found.`,
|
||||
[ApiKeyExceptionCode.API_KEY_REVOKED]: msg`This API key has been revoked.`,
|
||||
[ApiKeyExceptionCode.API_KEY_EXPIRED]: msg`This API key has expired.`,
|
||||
[ApiKeyExceptionCode.API_KEY_NO_ROLE_ASSIGNED]: msg`This API key has no role assigned.`,
|
||||
[ApiKeyExceptionCode.ROLE_CANNOT_BE_ASSIGNED_TO_API_KEYS]: msg`This role cannot be assigned to API keys.`,
|
||||
const getApiKeyExceptionUserFriendlyMessage = (code: ApiKeyExceptionCode) => {
|
||||
switch (code) {
|
||||
case ApiKeyExceptionCode.API_KEY_NOT_FOUND:
|
||||
return msg`API key not found.`;
|
||||
case ApiKeyExceptionCode.API_KEY_REVOKED:
|
||||
return msg`This API key has been revoked.`;
|
||||
case ApiKeyExceptionCode.API_KEY_EXPIRED:
|
||||
return msg`This API key has expired.`;
|
||||
case ApiKeyExceptionCode.API_KEY_NO_ROLE_ASSIGNED:
|
||||
return msg`This API key has no role assigned.`;
|
||||
case ApiKeyExceptionCode.ROLE_CANNOT_BE_ASSIGNED_TO_API_KEYS:
|
||||
return msg`This role cannot be assigned to API keys.`;
|
||||
default:
|
||||
assertUnreachable(code);
|
||||
}
|
||||
};
|
||||
|
||||
export class ApiKeyException extends CustomException<ApiKeyExceptionCode> {
|
||||
@@ -30,7 +37,7 @@ export class ApiKeyException extends CustomException<ApiKeyExceptionCode> {
|
||||
) {
|
||||
super(message, code, {
|
||||
userFriendlyMessage:
|
||||
userFriendlyMessage ?? apiKeyExceptionUserFriendlyMessages[code],
|
||||
userFriendlyMessage ?? getApiKeyExceptionUserFriendlyMessage(code),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user