import app from '@adonisjs/core/services/app' import { type HttpContext, ExceptionHandler } from '@adonisjs/core/http' /** * Exception handler API JSON-only. On normalise les erreurs DB / Vine * en réponse `{ errors: [...] }` (cf. backend.md §6 pour le contrat). */ export default class HttpExceptionHandler extends ExceptionHandler { protected debug = !app.inProduction async handle(error: unknown, ctx: HttpContext) { // Postgres unique violation → 422 avec field/code stables, pas un // 500 avec stack pg-protocol. if ( error && typeof error === 'object' && 'code' in error && (error as { code: unknown }).code === '23505' ) { const detail = String((error as { detail?: unknown }).detail ?? '') // Tente d'extraire le nom de colonne du message PG ("Key (numero, ...)"). const fieldMatch = detail.match(/Key \(([^)]+)\)=/) const field = fieldMatch?.[1]?.split(',')[0]?.trim() ctx.response.status(422) return ctx.response.json({ errors: [ { code: 'duplicate', message: 'Cette valeur existe déjà.', field: field ?? undefined, }, ], }) } return super.handle(error, ctx) } async report(error: unknown, ctx: HttpContext) { return super.report(error, ctx) } }