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 { CustomException } from 'src/utils/custom-exception';
export enum WorkflowTriggerExceptionCode {
@@ -14,18 +16,29 @@ export enum WorkflowTriggerExceptionCode {
INTERNAL_ERROR = 'INTERNAL_ERROR',
}
const workflowTriggerExceptionUserFriendlyMessages: Record<
WorkflowTriggerExceptionCode,
MessageDescriptor
> = {
[WorkflowTriggerExceptionCode.INVALID_INPUT]: msg`Invalid workflow trigger input.`,
[WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER]: msg`Invalid workflow trigger configuration.`,
[WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION]: msg`Invalid workflow version.`,
[WorkflowTriggerExceptionCode.INVALID_WORKFLOW_STATUS]: msg`Invalid workflow status.`,
[WorkflowTriggerExceptionCode.INVALID_ACTION_TYPE]: msg`Invalid action type.`,
[WorkflowTriggerExceptionCode.NOT_FOUND]: msg`Workflow trigger not found.`,
[WorkflowTriggerExceptionCode.FORBIDDEN]: msg`You do not have permission to access this workflow.`,
[WorkflowTriggerExceptionCode.INTERNAL_ERROR]: msg`An unexpected workflow error occurred.`,
const getWorkflowTriggerExceptionUserFriendlyMessage = (
code: WorkflowTriggerExceptionCode,
) => {
switch (code) {
case WorkflowTriggerExceptionCode.INVALID_INPUT:
return msg`Invalid workflow trigger input.`;
case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER:
return msg`Invalid workflow trigger configuration.`;
case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION:
return msg`Invalid workflow version.`;
case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_STATUS:
return msg`Invalid workflow status.`;
case WorkflowTriggerExceptionCode.INVALID_ACTION_TYPE:
return msg`Invalid action type.`;
case WorkflowTriggerExceptionCode.NOT_FOUND:
return msg`Workflow not found.`;
case WorkflowTriggerExceptionCode.FORBIDDEN:
return msg`You do not have permission to access this workflow.`;
case WorkflowTriggerExceptionCode.INTERNAL_ERROR:
return STANDARD_ERROR_MESSAGE;
default:
assertUnreachable(code);
}
};
export class WorkflowTriggerException extends CustomException<WorkflowTriggerExceptionCode> {
@@ -37,7 +50,7 @@ export class WorkflowTriggerException extends CustomException<WorkflowTriggerExc
super(message, code, {
userFriendlyMessage:
userFriendlyMessage ??
workflowTriggerExceptionUserFriendlyMessages[code],
getWorkflowTriggerExceptionUserFriendlyMessage(code),
});
}
}