errorMapping.test.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import { ApiClientError } from "@/lib/frontend/apiClient";
  4. import { mapSearchError } from "./errorMapping.js";
  5. describe("lib/frontend/search/errorMapping", () => {
  6. it("returns null for missing error", () => {
  7. expect(mapSearchError(null)).toBe(null);
  8. expect(mapSearchError(undefined)).toBe(null);
  9. });
  10. it("maps unauthenticated", () => {
  11. const err = new ApiClientError({
  12. status: 401,
  13. code: "AUTH_UNAUTHENTICATED",
  14. message: "Unauthorized",
  15. });
  16. const mapped = mapSearchError(err);
  17. expect(mapped).toMatchObject({ kind: "unauthenticated" });
  18. });
  19. it("maps forbidden", () => {
  20. const err = new ApiClientError({
  21. status: 403,
  22. code: "AUTH_FORBIDDEN_BRANCH",
  23. message: "Forbidden",
  24. });
  25. const mapped = mapSearchError(err);
  26. expect(mapped).toMatchObject({ kind: "forbidden" });
  27. });
  28. it("maps known validation errors to friendly German copy", () => {
  29. const err = new ApiClientError({
  30. status: 400,
  31. code: "VALIDATION_SEARCH_MISSING_FILTER",
  32. message: "At least one of q or date range must be provided",
  33. });
  34. const mapped = mapSearchError(err);
  35. expect(mapped).toEqual({
  36. kind: "validation",
  37. title: "Kein Suchbegriff",
  38. description: "Bitte geben Sie einen Suchbegriff ein.",
  39. });
  40. });
  41. it("maps unknown validation errors to a generic validation message", () => {
  42. const err = new ApiClientError({
  43. status: 400,
  44. code: "VALIDATION_SOMETHING_NEW",
  45. message: "nope",
  46. });
  47. const mapped = mapSearchError(err);
  48. expect(mapped?.kind).toBe("validation");
  49. expect(mapped?.title).toBe("Ungültige Eingabe");
  50. });
  51. it("maps network errors", () => {
  52. const err = new ApiClientError({
  53. status: 0,
  54. code: "CLIENT_NETWORK_ERROR",
  55. message: "Network error",
  56. });
  57. const mapped = mapSearchError(err);
  58. expect(mapped).toMatchObject({ kind: "generic", title: "Netzwerkfehler" });
  59. });
  60. it("maps unknown errors to generic", () => {
  61. const mapped = mapSearchError(new Error("boom"));
  62. expect(mapped).toMatchObject({ kind: "generic" });
  63. });
  64. it("maps VALIDATION_SEARCH_RANGE to a specific German message", () => {
  65. const err = new ApiClientError({
  66. status: 400,
  67. code: "VALIDATION_SEARCH_RANGE",
  68. message: "Invalid date range",
  69. });
  70. const mapped = mapSearchError(err);
  71. expect(mapped).toMatchObject({
  72. kind: "validation",
  73. title: "Ungültiger Zeitraum",
  74. });
  75. expect(mapped.description).toMatch(/Startdatum/i);
  76. });
  77. });