Improve qualification of rest errors (#17629)

Will help close
[sentry](https://twenty-v7.sentry.io/issues/6827612895/?environment=prod&environment=prod-eu&project=4507072499810304&query=is%3Aunresolved%20%21issue.type%3A%5Bperformance_consecutive_db_queries%2Cperformance_consecutive_http%2Cperformance_file_io_main_thread%2Cperformance_db_main_thread%2Cperformance_n_plus_one_db_queries%2Cperformance_n_plus_one_api_calls%2Cperformance_p95_endpoint_regression%2Cperformance_slow_db_query%2Cperformance_render_blocking_asset_span%2Cperformance_uncompressed_assets%2Cperformance_http_overhead%2Cperformance_large_http_payload%5D%20timesSeen%3A%3E10&referrer=issue-stream&sort=date):
a 400 error (attempt to filter on a `null` id) qualified as a 500 error
from rest api.

I don't know what was our long term plan on validation for rest api, I
suppose it's not a priority, making it acceptable to interceipt some
postgres errors based on their messages. Ideally we would have a
validation earlier, at args parsing level.

before
<img width="1286" height="460" alt="Capture d’écran 2026-02-02 à 14 08
53"
src="https://github.com/user-attachments/assets/bf3d34f9-1436-4739-8f8e-95b18f59045b"
/>

after
<img width="1222" height="438" alt="Capture d’écran 2026-02-02 à 14 09
01"
src="https://github.com/user-attachments/assets/e817eaff-1ab9-49fb-869d-ddfd00671fbf"
/>
This commit is contained in:
Marie
2026-02-04 15:33:30 +01:00
committed by GitHub
parent 4629fb90be
commit d4303469ff
6 changed files with 295 additions and 297 deletions
@@ -35,10 +35,10 @@ export const computeTwentyORMException = async (
);
}
const errorCode = (error as QueryFailedErrorWithCode).code;
if (
error.message.includes(
'duplicate key value violates unique constraint',
) &&
errorCode === POSTGRESQL_ERROR_CODES.UNIQUE_VIOLATION &&
isDefined(objectMetadata) &&
isDefined(entityManager) &&
isDefined(internalContext)
@@ -51,17 +51,18 @@ export const computeTwentyORMException = async (
);
}
if (error.message.includes('invalid input value for')) {
if (errorCode === POSTGRESQL_ERROR_CODES.INVALID_TEXT_REPRESENTATION) {
return new TwentyORMException(
error.message,
error.message, // safe and useful
TwentyORMExceptionCode.INVALID_INPUT,
);
}
const errorCode = (error as QueryFailedErrorWithCode).code;
if (isDefined(errorCode) && POSTGRESQL_ERROR_CODES.includes(errorCode)) {
throw new PostgresException(error.message, errorCode);
if (
isDefined(errorCode) &&
Object.values(POSTGRESQL_ERROR_CODES).includes(errorCode)
) {
throw new PostgresException('Data validation error.', errorCode);
}
throw error;
}