Files
faunosteo/src/pdf/import.ts
T
Bastien ChanotandClaude Opus 4.7 9916c42484 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>
2026-05-20 07:36:34 +02:00

41 lines
1.1 KiB
TypeScript

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(
public readonly kind: ImportErrorKind,
public readonly foundVersion?: number,
) {
super(kind)
this.name = 'ImportError'
}
}
export async function importReportFromPdf(pdfBytes: Uint8Array): Promise<Report> {
let pdfDoc: PDFDocument
try {
pdfDoc = await PDFDocument.load(pdfBytes, { ignoreEncryption: true })
} catch {
throw new ImportError('invalid_pdf')
}
try {
return await extractReportJson(pdfDoc)
} catch (err) {
if (err instanceof IncompatibleReportVersionError) {
throw new ImportError('incompatible_version', err.foundVersion)
}
throw new ImportError('no_attachment')
}
}