Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make safer parsed values in getVerseData() #217

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
const { chapterNumber } = params;
const chapterData = await getChapterData(
paramsToLocale(params),
parseInt(chapterNumber),
Number(chapterNumber) || 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The change from parseInt(chapterNumber) to Number(chapterNumber) || 1 is a good one as it provides a fallback value of 1 if chapterNumber is not a valid number. However, this could potentially introduce unexpected behavior if chapterNumber is 0 or an invalid number. In such cases, the function will silently use 1 as the chapter number, which might not be the intended behavior. It would be better to throw an error when chapterNumber is not a valid number so that the issue can be caught and fixed.

-    Number(chapterNumber) || 1,
+    isNaN(Number(chapterNumber)) ? throw new Error("Invalid chapter number") : Number(chapterNumber),

);
if (!chapterData) {
return {};
Expand Down Expand Up @@ -85,7 +85,7 @@ export default async function Chapter({ params }: Props) {

const chapterData = await getChapterData(
locale,
parseInt(chapterNumber),
Number(chapterNumber) || 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Similar to the previous comment, using Number(chapterNumber) || 1 could lead to unexpected behavior if chapterNumber is 0 or an invalid number. It's better to throw an error in such cases.

-    Number(chapterNumber) || 1,
+    isNaN(Number(chapterNumber)) ? throw new Error("Invalid chapter number") : Number(chapterNumber),

languageSettings.translationAuthor.id,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ const Verse = async ({ params }: Props) => {
});

const verseData = await getVerseData(
Number(chapterNumber),
Number(verseNumber),
Number(chapterNumber) || 1,
Number(verseNumber) || 1,
Comment on lines +77 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The changes made here are good for handling edge cases where chapterNumber or verseNumber might not be valid numbers. However, using a fallback value of 1 might not always be the best solution as it could lead to unexpected behavior if the user is trying to access a chapter or verse that doesn't exist. It would be better to handle this error more explicitly and provide feedback to the user.

- Number(chapterNumber) || 1,
- Number(verseNumber) || 1,
+ isNaN(Number(chapterNumber)) ? throw new Error("Invalid chapter number") : Number(chapterNumber),
+ isNaN(Number(verseNumber)) ? throw new Error("Invalid verse number") : Number(verseNumber),

This way, an error will be thrown when chapterNumber or verseNumber are not valid numbers, providing clear feedback about what went wrong.

languageSettings.commentaryAuthor.id,
languageSettings.translationAuthor.id,
);
Expand Down
5 changes: 4 additions & 1 deletion src/gqty-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ const queryFetcher: QueryFetcher = async function (
variables,
fetchOptions,
) {
const response = await fetch(process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT!, {
if (!process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT) {
throw new Error("NEXT_PUBLIC_GRAPHQL_ENDPOINT should be present");
}
const response = await fetch(process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
Loading