From ee73f8a087eab4169143370a5b528366a0fa9577 Mon Sep 17 00:00:00 2001 From: Sonarly Claude Code Date: Fri, 20 Feb 2026 00:34:28 +0000 Subject: [PATCH] fix: handle foreign key violations as 400 Bad Request instead of 500 Foreign key constraint violations (PostgreSQL error 23503) from user input (e.g., POST /rest/people with a non-existent companyId) were falling through to the generic PostgresException catch-all, which returned a 500 Internal Server Error with an unhelpful "Data validation error." message. Add a specific handler for FOREIGN_KEY_VIOLATION that returns a TwentyORMException with INVALID_INPUT code, resulting in a 400 Bad Request with a descriptive message. Co-Authored-By: Claude Opus 4.6 --- .../error-handling/compute-twenty-orm-exception.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/twenty-server/src/engine/twenty-orm/error-handling/compute-twenty-orm-exception.ts b/packages/twenty-server/src/engine/twenty-orm/error-handling/compute-twenty-orm-exception.ts index f9fad80e6b1..168ea792e5c 100644 --- a/packages/twenty-server/src/engine/twenty-orm/error-handling/compute-twenty-orm-exception.ts +++ b/packages/twenty-server/src/engine/twenty-orm/error-handling/compute-twenty-orm-exception.ts @@ -58,6 +58,13 @@ export const computeTwentyORMException = async ( ); } + if (errorCode === POSTGRESQL_ERROR_CODES.FOREIGN_KEY_VIOLATION) { + return new TwentyORMException( + 'Record violates a foreign key constraint. Please ensure related records exist.', + TwentyORMExceptionCode.INVALID_INPUT, + ); + } + if ( isDefined(errorCode) && Object.values(POSTGRESQL_ERROR_CODES).includes(errorCode)