diff --git a/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/utils/compute-pg-graphql-error.util.ts b/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/utils/compute-pg-graphql-error.util.ts index 8dfa96ed4f..c25e3a0dc5 100644 --- a/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/utils/compute-pg-graphql-error.util.ts +++ b/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/utils/compute-pg-graphql-error.util.ts @@ -8,10 +8,24 @@ interface PgGraphQLErrorMapping { [key: string]: (command: string, objectName: string) => HttpException; } +const pgGraphQLCommandMapping = { + insertInto: 'insert', + update: 'update', + deleteFrom: 'delete', +}; + const pgGraphQLErrorMapping: PgGraphQLErrorMapping = { 'delete impacts too many records': (command, objectName) => new BadRequestException( - `Cannot ${command} ${objectName} because it impacts too many records.`, + `Cannot ${ + pgGraphQLCommandMapping[command] ?? command + } ${objectName} because it impacts too many records.`, + ), + 'duplicate key value violates unique constraint': (command, objectName) => + new BadRequestException( + `Cannot ${ + pgGraphQLCommandMapping[command] ?? command + } ${objectName} because it violates a uniqueness constraint.`, ), }; @@ -22,7 +36,14 @@ export const computePgGraphQLError = ( ) => { const error = errors[0]; const errorMessage = error?.message; - const mappedError = pgGraphQLErrorMapping[errorMessage]; + + const mappedErrorKey = Object.keys(pgGraphQLErrorMapping).find( + (key) => errorMessage?.startsWith(key), + ); + + const mappedError = mappedErrorKey + ? pgGraphQLErrorMapping[mappedErrorKey] + : null; if (mappedError) { return mappedError(command, objectName); diff --git a/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/workspace-query-runner.service.ts b/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/workspace-query-runner.service.ts index ef565b76fb..166cb9081e 100644 --- a/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/workspace-query-runner.service.ts +++ b/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/workspace-query-runner.service.ts @@ -599,13 +599,6 @@ export class WorkspaceQueryRunnerService { const result = graphqlResult?.[0]?.resolve?.data?.[entityKey]; const errors = graphqlResult?.[0]?.resolve?.errors; - if (!result) { - this.logger.log( - `No result found for ${entityKey}, graphqlResult: ` + - JSON.stringify(graphqlResult, null, 3), - ); - } - if ( result && ['update', 'deleteFrom'].includes(command) &&