CSV upload: surface per-row validation errors with readable messages¶
Problem¶
Issue body (FR): "todo: meilleur message d'erreur lorsque factor non valide (str/float) ou date non valid (l'erreur ne remonte pas dans l'interface)" — better error message when a factor value has a type mismatch (str/float) or a date is invalid, because right now the error doesn't reach the UI.
Two independent bugs, both confirmed in code:
-
Raw pydantic dump as the error message. Both CSV row-validation paths call
handler.validate_create(payload)(backend/app/services/data_ingestion/base_csv_provider.py:1278for data-entries,backend/app/services/data_ingestion/base_factor_csv_provider.py_process_rowfor factors) inside a bareexcept Exception, and format it asf"Validation error: {validation_error}". For a pydanticValidationErrorthis stringifies to a multi-line technical dump ("1 validation error for FactorCreate\nco2_factor\n Input should be a valid number... [type=float_parsing, input_value='abc', input_type=str]") — unreadable for an operator uploading a CSV, and not localized. -
The message never reaches the UI for the main upload widget. There are two different CSV-upload surfaces in the frontend:
ModuleTable.vue(formatRowErrors, lines 570-598) does readpayload.meta.row_errors/row_errors_countand renders one line per row ("row X: reason") in a$q.notify.- The primary "Add data" / "Add factors" cards (
UploadCard.vue+useUploadCard.ts, used viaUploadCardFactors.vueand the data-management upload flow) usegetErrorDetails()(frontend/src/composables/useUploadCard.ts:130-143), which only readsjob.status_messageandmeta.error(a job-level failure key, only set when the whole job throws) — it never readsmeta.row_errors. The tooltip inUploadCard.vue:317-331then dumps the rawstatsobject withv-for="(value, key) in errorDetails.stats", so therow_errorskey renders asrow_errors: [object Object],[object Object]— Vue's default array-of-objects stringification. The actual per-row reason ("Validation error: ...") never appears anywhere in this widget. This is the upload flow issue #659 is about.
Design¶
Backend — replace the generic except Exception around validate_create() in both row-processing loops with a pydantic.ValidationError-specific branch that walks validation_error.errors() and builds one short message per field: f"{err['loc'][-1]}: {err['msg']} (got {err['input']!r})", joined with ;. This turns the dump into e.g. co2_factor: Input should be a valid number (got 'abc') / date: Input should be a valid date or datetime (got '2026-13-40') — still pydantic's wording (no new i18n layer, no per-field custom copy — that's scope creep for a "nice to have"), but on one line and free of the type=... / input_type=... noise. Non-ValidationError exceptions keep the existing f"Validation error: {validation_error}" fallback.
Frontend — extract the row-error formatting already written once (ModuleTable.vue's formatRowErrors) into a shared helper (e.g. add to useUploadCard.ts or a small src/utils/rowErrors.ts), and use it in getErrorDetails() so the tooltip built in UploadCard.vue:317-331 shows real reasons instead of the raw stats dump. Concretely: getErrorDetails gains a rowErrors: string[] field (formatted row N: reason, capped like MAX_DISPLAYED_ROW_ERRORS in ModuleTable.vue), and UploadCard.vue's tooltip renders that list instead of v-for over errorDetails.stats. Drop row_errors/row_errors_count from the raw stats dump loop so they don't double-render as [object Object].
Steps¶
- Backend: catch
pydantic.ValidationErrorinbase_factor_csv_provider.py::_process_row'svalidate_createcall and format a readable per-field message (see Design); keep the generic fallback for other exceptions. - Backend: apply the same fix at
base_csv_provider.py:1278'svalidate_createcall site (data-entry CSV path — samefloat_parsing/date_parsingfailure modes forfactor/datecolumns). - Frontend: extract
formatRowErrors(fromModuleTable.vue) into a shared helper reusable fromuseUploadCard.ts. - Frontend: wire
getErrorDetails()(useUploadCard.ts:130-143) to expose formatted row errors, and updateUploadCard.vue's error tooltip (:317-331) to render them instead of dumpingerrorDetails.stats(excludingrow_errors/row_errors_countfrom that dump). - Add/extend a backend unit test asserting a bad-float and a bad-date CSV row produce the new readable
row_errorsreason string (existing suites:tests/unit/services/data_ingestion/test_base_factor_csv_provider.py,test_base_csv_provider.py).