-
Notifications
You must be signed in to change notification settings - Fork 53
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
); | ||
if (!chapterData) { | ||
return {}; | ||
|
@@ -85,7 +85,7 @@ export default async function Chapter({ params }: Props) { | |
|
||
const chapterData = await getChapterData( | ||
locale, | ||
parseInt(chapterNumber), | ||
Number(chapterNumber) || 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the previous comment, using - Number(chapterNumber) || 1,
+ isNaN(Number(chapterNumber)) ? throw new Error("Invalid chapter number") : Number(chapterNumber), |
||
languageSettings.translationAuthor.id, | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes made here are good for handling edge cases where - 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 |
||
languageSettings.commentaryAuthor.id, | ||
languageSettings.translationAuthor.id, | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CodeRabbit
The change from
parseInt(chapterNumber)
toNumber(chapterNumber) || 1
is a good one as it provides a fallback value of 1 ifchapterNumber
is not a valid number. However, this could potentially introduce unexpected behavior ifchapterNumber
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 whenchapterNumber
is not a valid number so that the issue can be caught and fixed.