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 <[email protected]>
This commit is contained in:
Sonarly Claude Code
2026-02-20 00:34:28 +00:00
co-authored by Claude Opus 4.6
parent 783e57f5d8
commit ee73f8a087
@@ -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)