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

+16 -1
View File
@@ -29,7 +29,22 @@ export default function HomeRoute() {
hydrate(report) hydrate(report)
navigate('/editor') navigate('/editor')
} catch (err) { } catch (err) {
setError(err instanceof ImportError ? err.message : t('home.import.error_generic')) if (err instanceof ImportError) {
if (err.kind === 'incompatible_version') {
setError(
t('home.import.error_incompatible_version').replace(
'{version}',
String(err.foundVersion ?? '?'),
),
)
} else if (err.kind === 'invalid_pdf') {
setError(t('home.import.error_invalid_pdf'))
} else {
setError(t('home.import.error_no_attachment'))
}
} else {
setError(t('home.import.error_generic'))
}
} finally { } finally {
setBusy(false) setBusy(false)
if (fileRef.current) fileRef.current.value = '' if (fileRef.current) fileRef.current.value = ''
+5
View File
@@ -24,6 +24,11 @@ export const en: Dict = {
'home.import.cta': 'Choose a PDF', 'home.import.cta': 'Choose a PDF',
'home.import.loading': 'Reading…', 'home.import.loading': 'Reading…',
'home.import.error_generic': 'Failed to read the PDF.', 'home.import.error_generic': 'Failed to read the PDF.',
'home.import.error_invalid_pdf': 'This file is not a readable PDF.',
'home.import.error_no_attachment':
'This PDF was not generated by Osteo Report (no data detected).',
'home.import.error_incompatible_version':
'This PDF uses format v{version}, incompatible with the current version (v2). Re-export the report from a recent version of the app.',
'not_found.title': 'Page not found', 'not_found.title': 'Page not found',
'not_found.description': 'The requested path does not exist.', 'not_found.description': 'The requested path does not exist.',
+6
View File
@@ -27,6 +27,12 @@ export const fr: Dict = {
'home.import.cta': 'Choisir un PDF', 'home.import.cta': 'Choisir un PDF',
'home.import.loading': 'Lecture…', 'home.import.loading': 'Lecture…',
'home.import.error_generic': 'Erreur de lecture du PDF.', 'home.import.error_generic': 'Erreur de lecture du PDF.',
'home.import.error_invalid_pdf':
"Ce fichier n'est pas un PDF lisible.",
'home.import.error_no_attachment':
"Ce PDF n'a pas été généré par Osteo Report (pas de données détectées).",
'home.import.error_incompatible_version':
"Ce PDF est au format v{version}, incompatible avec la version actuelle (v2). Réexportez le bilan depuis une version récente de l'application.",
// not found // not found
'not_found.title': 'Page introuvable', 'not_found.title': 'Page introuvable',
+7 -4
View File
@@ -3,12 +3,15 @@ import { ReportSchema, type Report } from '@/domain/schema/report'
const FILENAME = 'osteo-report.json' 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 { export class IncompatibleReportVersionError extends Error {
constructor(public readonly foundVersion: number) { constructor(public readonly foundVersion: number) {
super( super(`incompatible_version:${foundVersion}`)
`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.`,
)
this.name = 'IncompatibleReportVersionError' this.name = 'IncompatibleReportVersionError'
} }
} }
+18 -10
View File
@@ -2,12 +2,22 @@ import { PDFDocument } from 'pdf-lib'
import { extractReportJson, IncompatibleReportVersionError } from './attachment' import { extractReportJson, IncompatibleReportVersionError } from './attachment'
import type { Report } from '@/domain/schema/report' 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 { export class ImportError extends Error {
constructor(message?: string) { constructor(
super( public readonly kind: ImportErrorKind,
message ?? public readonly foundVersion?: number,
"Ce PDF n'a pas été généré par Osteo Report (pas de données détectées).", ) {
) super(kind)
this.name = 'ImportError' this.name = 'ImportError'
} }
} }
@@ -17,16 +27,14 @@ export async function importReportFromPdf(pdfBytes: Uint8Array): Promise<Report>
try { try {
pdfDoc = await PDFDocument.load(pdfBytes, { ignoreEncryption: true }) pdfDoc = await PDFDocument.load(pdfBytes, { ignoreEncryption: true })
} catch { } catch {
throw new ImportError() throw new ImportError('invalid_pdf')
} }
try { try {
return await extractReportJson(pdfDoc) return await extractReportJson(pdfDoc)
} catch (err) { } 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) { if (err instanceof IncompatibleReportVersionError) {
throw new ImportError(err.message) throw new ImportError('incompatible_version', err.foundVersion)
} }
throw new ImportError() throw new ImportError('no_attachment')
} }
} }
+9 -1
View File
@@ -83,6 +83,14 @@ describe('PDF roundtrip', () => {
afRelationship: AFRelationship.Data, afRelationship: AFRelationship.Data,
}) })
const pdfBytes = await doc.save() const pdfBytes = await doc.save()
await expect(importReportFromPdf(pdfBytes)).rejects.toThrow(/v1/) try {
await importReportFromPdf(pdfBytes)
throw new Error('expected rejection')
} catch (err) {
expect(err).toBeInstanceOf(ImportError)
const e = err as ImportError
expect(e.kind).toBe('incompatible_version')
expect(e.foundVersion).toBe(1)
}
}) })
}) })