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:
1 parent
fa3eb51394
commit
9916c42484
6 files changed
+61
-16
No files matched your search
+18
-10
@@ -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')
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user