diff --git a/src/app/routes/HomeRoute.tsx b/src/app/routes/HomeRoute.tsx index 9ccd9b80..7ea24a0b 100644 --- a/src/app/routes/HomeRoute.tsx +++ b/src/app/routes/HomeRoute.tsx @@ -29,7 +29,22 @@ export default function HomeRoute() { hydrate(report) navigate('/editor') } 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 { setBusy(false) if (fileRef.current) fileRef.current.value = '' diff --git a/src/i18n/en.ts b/src/i18n/en.ts index a53fe18b..0b46b0a9 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -24,6 +24,11 @@ export const en: Dict = { 'home.import.cta': 'Choose a PDF', 'home.import.loading': 'Reading…', '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.description': 'The requested path does not exist.', diff --git a/src/i18n/fr.ts b/src/i18n/fr.ts index 2a63e8fc..ec5eb10d 100644 --- a/src/i18n/fr.ts +++ b/src/i18n/fr.ts @@ -27,6 +27,12 @@ export const fr: Dict = { 'home.import.cta': 'Choisir un PDF', 'home.import.loading': 'Lecture…', '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.title': 'Page introuvable', diff --git a/src/pdf/attachment.ts b/src/pdf/attachment.ts index 0cac7aa4..102d2407 100644 --- a/src/pdf/attachment.ts +++ b/src/pdf/attachment.ts @@ -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' } } diff --git a/src/pdf/import.ts b/src/pdf/import.ts index ec15e916..f1b86419 100644 --- a/src/pdf/import.ts +++ b/src/pdf/import.ts @@ -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 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') } } diff --git a/tests/unit/pdf.roundtrip.test.ts b/tests/unit/pdf.roundtrip.test.ts index ddb79baf..3af90664 100644 --- a/tests/unit/pdf.roundtrip.test.ts +++ b/tests/unit/pdf.roundtrip.test.ts @@ -83,6 +83,14 @@ describe('PDF roundtrip', () => { afRelationship: AFRelationship.Data, }) 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) + } }) })