fix(i18n): localize PDF import error messages

Review surfaced 3 critical paths where hardcoded French leaked into
the EN UI:
- src/pdf/import.ts ImportError default message
- src/pdf/attachment.ts IncompatibleReportVersionError message
- src/app/routes/HomeRoute.tsx propagated err.message verbatim

Refactor:
- ImportError carries a typed `kind` discriminant
  ('invalid_pdf' | 'incompatible_version' | 'no_attachment') plus
  optional `foundVersion`. Removes hardcoded localized strings.
- IncompatibleReportVersionError keeps `foundVersion` only; message
  collapses to an internal sentinel.
- HomeRoute resolves the user-facing message via the active i18n
  dict using `err.kind`, with `{version}` template substitution for
  the incompatible-version case.
- 3 new dict keys (fr+en): home.import.error_invalid_pdf,
  home.import.error_no_attachment, home.import.error_incompatible_version
- pdf.roundtrip v1 case asserts ImportError instance + kind +
  foundVersion instead of grepping the message text.

154/154 tests pass, lint clean, typecheck clean, build OK.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Bastien ChanotandClaude Opus 4.7 committed 2026-05-20 07:36:34 +02:00
1 parent fa3eb51394
commit 9916c42484
6 files changed
+61 -16

No files matched your search

+7 -4
View File
@@ -3,12 +3,15 @@ import { ReportSchema, type Report } from '@/domain/schema/report'
const FILENAME = 'osteo-report.json'
/**
* Internal sentinel raised when the attached JSON's `version` field is
* present but not 2. The UI-facing message is composed by the importer
* caller against the active i18n dict — this error only carries the
* discriminating data.
*/
export class IncompatibleReportVersionError extends Error {
constructor(public readonly foundVersion: number) {
super(
`Ce PDF est au format v${foundVersion}, incompatible avec la version actuelle (v2). ` +
`Réexportez le bilan depuis une version récente de l'application.`,
)
super(`incompatible_version:${foundVersion}`)
this.name = 'IncompatibleReportVersionError'
}
}
+18 -10
View File
@@ -2,12 +2,22 @@ import { PDFDocument } from 'pdf-lib'
import { extractReportJson, IncompatibleReportVersionError } from './attachment'
import type { Report } from '@/domain/schema/report'
export type ImportErrorKind =
| 'invalid_pdf'
| 'incompatible_version'
| 'no_attachment'
/**
* Localization-agnostic import error. Carries a kind discriminant + an
* optional foundVersion so the caller (UI layer) can resolve the
* user-facing message via the active i18n dict.
*/
export class ImportError extends Error {
constructor(message?: string) {
super(
message ??
"Ce PDF n'a pas été généré par Osteo Report (pas de données détectées).",
)
constructor(
public readonly kind: ImportErrorKind,
public readonly foundVersion?: number,
) {
super(kind)
this.name = 'ImportError'
}
}
@@ -17,16 +27,14 @@ export async function importReportFromPdf(pdfBytes: Uint8Array): Promise<Report>
try {
pdfDoc = await PDFDocument.load(pdfBytes, { ignoreEncryption: true })
} catch {
throw new ImportError()
throw new ImportError('invalid_pdf')
}
try {
return await extractReportJson(pdfDoc)
} catch (err) {
// Version mismatch is a distinct failure mode — surface its message
// to the user instead of the generic "not an osteo PDF" line.
if (err instanceof IncompatibleReportVersionError) {
throw new ImportError(err.message)
throw new ImportError('incompatible_version', err.foundVersion)
}
throw new ImportError()
throw new ImportError('no_attachment')
}
}