Move duplicate key error handling in ORM (#13893)

Error available in REST API
<img width="1360" height="653" alt="Screenshot 2025-08-13 at 11 48 19"
src="https://github.com/user-attachments/assets/a055859d-ce54-4ff6-9aba-fac48e03a98d"
/>

`curl http://localhost:3000/rest/people \
  --request POST \
  --header 'Content-Type: application/json' \
  --data '{
  "emails": {
    "primaryEmail": "test@test.com"
  }
}'`

closes https://github.com/twentyhq/twenty/issues/13567
This commit is contained in:
Etienne
2025-08-14 18:38:04 +02:00
committed by GitHub
parent 5c61daa201
commit f6d0af5fb8
13 changed files with 156 additions and 50 deletions
@@ -3,6 +3,7 @@ import {
type HttpException,
Inject,
Injectable,
InternalServerErrorException,
Scope,
} from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
@@ -13,7 +14,12 @@ import { QueryFailedError } from 'typeorm';
import { type ExceptionHandlerUser } from 'src/engine/core-modules/exception-handler/interfaces/exception-handler-user.interface';
import { type ExceptionHandlerWorkspace } from 'src/engine/core-modules/exception-handler/interfaces/exception-handler-workspace.interface';
import { PostgresException } from 'src/engine/api/graphql/workspace-query-runner/utils/postgres-exception';
import { ExceptionHandlerService } from 'src/engine/core-modules/exception-handler/exception-handler.service';
import {
TwentyORMException,
TwentyORMExceptionCode,
} from 'src/engine/twenty-orm/exceptions/twenty-orm.exception';
import { handleException } from 'src/engine/utils/global-exception-handler.util';
interface RequestAndParams {
@@ -84,6 +90,25 @@ export class HttpExceptionHandlerService {
statusCode = 400;
}
if (
exception instanceof TwentyORMException &&
[
TwentyORMExceptionCode.INVALID_INPUT,
TwentyORMExceptionCode.DUPLICATE_ENTRY_DETECTED,
TwentyORMExceptionCode.CONNECT_UNIQUE_CONSTRAINT_ERROR,
TwentyORMExceptionCode.CONNECT_NOT_ALLOWED,
TwentyORMExceptionCode.CONNECT_RECORD_NOT_FOUND,
].includes(exception.code)
) {
exception = new BadRequestException(exception.message);
statusCode = 400;
}
if (exception instanceof PostgresException) {
exception = new InternalServerErrorException(exception.message);
statusCode = 500;
}
handleException({
exception,
exceptionHandlerService: this.exceptionHandlerService,