batchCooking/packages/shared/src/errors/error-codes.ts
Nicolas 1d03effc77 API: endpoints foyer/profil (nom, régime, allergènes) (step 2/6)
- GET/PATCH /house/current — renomme le foyer de l'utilisateur connecté.
  PATCH avec houseId null -> 404 HOUSE_NOT_FOUND.
- PATCH /profile/diet { dietId: number | null } — régime du profil ;
  null l'efface (étape skippable du parcours). dietId invalide ->
  404 DIET_NOT_FOUND.
- GET/PATCH /profile/allergies — allergènes/intolérances, liste d'IDs ;
  PATCH remplace l'ensemble complet (pas une fusion, cohérent avec un
  multi-select). ID invalide -> 404 ALLERGY_NOT_FOUND.
- 3 nouveaux ErrorCode (4041-4043) + libellés fr.
- Extraction de toSafeProfile() dans src/lib/safe-profile.ts —
  auparavant dupliqué dans auth.service.ts et require-auth.ts,
  profile.service.ts le réutilise aussi.
- Tests Mocha (28 passing) + Cucumber (15 scenarios) — même convention
  que le reste, doc README.

Deuxième commit de la feature profil/foyer/régime/allergènes —
composants front partagés dans le commit suivant.
2026-08-16 23:18:46 +02:00

61 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Enumeration of every business/domain error code the API can return.
*
* This is the single source of truth for error identification across the
* whole monorepo: `apps/api` throws errors carrying one of these codes,
* and `apps/web` maps each code to a localized, user-facing label (see
* `apps/web/src/services/error-message.service.ts`, backed by i18next
* locale files under `apps/web/src/locales/`). Neither side should ever
* hardcode a raw error value that the other side has to guess at — always
* reference `ErrorCode.XXX`, never a bare number/string.
*
* Numeric values (not string codes): grouped by category so the number
* itself hints at the kind of failure, similar in spirit to HTTP status
* code families —
* - `4000``4099`: request validation
* - `4010``4019`: authentication
* - `4040``4049`: not found
* - `5000``5099`: internal/unexpected
*
* When adding a new failure case in the API:
* 1. Add a new member here, in the right range, with the next free number.
* 2. Throw it via `HttpError` (`@batch-cooking/express-tools`).
* 3. Add its translation key to every locale file under
* `apps/web/src/locales` (one `translation.json` per language).
*/
export enum ErrorCode {
/** Request body/query failed zod schema validation. */
VALIDATION_ERROR = 4000,
/** Signup attempted with an email that already has a profile. */
EMAIL_ALREADY_IN_USE = 4001,
/** Login failed — wrong email or wrong password (never say which). */
INVALID_CREDENTIALS = 4010,
/** Request required a session cookie/JWT that is missing, invalid, or stale. */
NOT_AUTHENTICATED = 4011,
/** No route/resource matches the request. */
NOT_FOUND = 4040,
/** The profile making the request has no household yet (`houseId` is `null`). */
HOUSE_NOT_FOUND = 4041,
/** A `dietId` was given that doesn't match any reference `Diet` row. */
DIET_NOT_FOUND = 4042,
/** One or more `allergyIds` don't match any reference `Allergy` row. */
ALLERGY_NOT_FOUND = 4043,
/** Unexpected/unhandled failure — the catch-all, always logged server-side. */
INTERNAL_ERROR = 5000,
}
/**
* Shape of every JSON error body the API returns, whatever the failure.
* Kept intentionally small and stable: `code` is what clients should
* branch on, `message` is a human-readable (English, developer-facing)
* description useful for logs/debugging — never shown to end users as-is,
* since end-user-facing text is localized client-side from `code`.
*/
export interface ApiErrorResponse {
/** Machine-readable error identifier — see {@link ErrorCode}. */
code: ErrorCode;
/** Developer-facing description (English). Not localized, not for UI display. */
message: string;
/** Present only for VALIDATION_ERROR: per-field error messages from zod. */
details?: Record<string, string[] | undefined>;
}