Skip to content

Commit

Permalink
fix: atc should not throw 404
Browse files Browse the repository at this point in the history
Signed-off-by: Maud Royer <[email protected]>
  • Loading branch information
jillro committed Nov 12, 2024
1 parent 8790907 commit 6df1828
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/data/atc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import "server-only";
import { notFound } from "next/navigation";
import { cache } from "react";

import atcOfficialLabels from "@/data/ATC 2024 02 15.json";
Expand Down Expand Up @@ -63,7 +62,9 @@ export const getAtc1 = cache(async function (code: string): Promise<ATC1> {
const record = data.find(
(record) => record.fields.Lettre_1_ATC_1 === code.slice(0, 1),
);
if (!record) notFound();
if (!record) {
throw new Error(`ATC code not found: ${code.slice(0, 3)}`);
}

const childrenData = await getGristTableData("Table_Niveau_2", [
"Libelles_niveau_2",
Expand Down Expand Up @@ -94,15 +95,19 @@ export const getAtc2 = cache(async function (code: string): Promise<ATC> {
(record: any) => record.fields.Lettre_2_ATC2 === code.slice(0, 3),
);

if (!record) notFound();
if (!record) {
throw new Error(`ATC code not found: ${code.slice(0, 3)}`);
}

const libeleId = record.fields.Libelles_niveau_2;
const libeleData = await getGristTableData("Intitules_possibles", [
"Libelles_niveau_2",
]);
const libeleRecord = libeleData.find((record) => record.id === libeleId);

if (!libeleRecord) notFound();
if (!libeleRecord) {
throw new Error(`ATC code not found: ${code.slice(0, 3)}`);
}

return {
code: record.fields.Lettre_2_ATC2 as string,
Expand Down

0 comments on commit 6df1828

Please sign in to comment.