Skip to content

Conversation

@nileshpahari
Copy link
Contributor

Proposed change

Correct the syntax of the Error constructor, in order to preserve the original error.

Files modified

frontend/src/app/api/auth/[...nextauth]/route.ts
frontend/src/components/ModuleForm.tsx

Resolves #2803

Checklist

  • I've read and followed the contributing guidelines.
  • I've run make check-test locally; all checks and tests passed.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 7, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling and message formatting to provide clearer, more robust error reporting when system failures occur.

✏️ Tip: You can customize this high-level summary in your review settings.

Walkthrough

Updated error handling at two frontend locations: frontend/src/app/api/auth/[...nextauth]/route.ts now interpolates caught errors into a single Error message string; frontend/src/components/ModuleForm.tsx changed its catch path to compute a formatted errorMessage and call setError(...) instead of constructing an Error with multiple arguments. No public API or function signature changes.

Changes

Cohort / File(s) Summary
Auth route error formatting
frontend/src/app/api/auth/[...nextauth]/route.ts
Replaced two-argument new Error(..., err) usages with a single-string template literal that includes the caught err (e.g., Failed to fetch ... Error: ${err}). Control flow and return values unchanged.
ModuleForm error handling
frontend/src/components/ModuleForm.tsx
Catch block updated to build a robust errorMessage (handles Error, string, fallback) and call setError(errorMessage) instead of throwing new Error(...) with multiple arguments. Success path unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Small, localized, consistent changes.
  • Pay extra attention to:
    • Any logging/monitoring expectations that previously relied on thrown Error objects vs. string state in ModuleForm.tsx.
    • Proper sanitization/formatting of interpolated error contents in route.ts.

Suggested reviewers

  • arkid15r
  • kasya

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: fixing incorrect Error constructor syntax in multiple files.
Description check ✅ Passed The description is relevant and explains the fix: correcting Error constructor syntax to preserve original errors.
Linked Issues check ✅ Passed The PR successfully addresses issue #2803 by converting Error constructor calls from two-argument syntax to template string syntax in both specified files.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the Error constructor syntax in the two specified files as required by issue #2803.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1fade10 and bed93a6.

📒 Files selected for processing (1)
  • frontend/src/components/ModuleForm.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: Rajgupta36
Repo: OWASP/Nest PR: 1717
File: frontend/src/app/api/auth/[...nextauth]/route.ts:13-25
Timestamp: 2025-08-10T11:08:47.258Z
Learning: In the OWASP Nest codebase (frontend/src/app/api/auth/[...nextauth]/route.ts), input validation and string trimming for authentication-related queries like `isProjectLeader` and `isMentor` are handled in the backend rather than the frontend. The backend is responsible for sanitizing and validating input parameters.
📚 Learning: 2025-07-12T17:14:28.536Z
Learnt from: Rajgupta36
Repo: OWASP/Nest PR: 1717
File: frontend/src/app/mentorship/programs/[programKey]/edit/page.tsx:90-128
Timestamp: 2025-07-12T17:14:28.536Z
Learning: Both ProgramForm (programCard.tsx) and ModuleForm (mainmoduleCard.tsx) components already implement HTML validation using the `required` attribute on form fields. The browser's native validation prevents form submission and displays error messages when required fields are empty, eliminating the need for additional JavaScript validation before GraphQL mutations.

Applied to files:

  • frontend/src/components/ModuleForm.tsx

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
frontend/src/app/api/auth/[...nextauth]/route.ts (1)

34-36: Error constructor syntax is now correct.

Same fix and recommendations as the checkIfProjectLeader function above. Consider applying consistent error handling improvements to both functions.

🧹 Nitpick comments (1)
frontend/src/app/api/auth/[...nextauth]/route.ts (1)

20-22: Error constructor syntax is now correct.

The fix properly consolidates the error message into a single string parameter. However, consider these improvements:

  1. Use error cause chaining (ES2022) to preserve the original error stack:
throw new Error('Failed to fetch project leader status', { cause: err })
  1. If err is an Error object, extract its message:
throw new Error(`Failed to fetch project leader status: ${err instanceof Error ? err.message : String(err)}`)
  1. Since this function is called during JWT callback (line 78) without visible error handling, consider whether re-throwing is appropriate or if the auth flow should degrade gracefully.
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 78cf54a and 6217a9b.

📒 Files selected for processing (2)
  • frontend/src/app/api/auth/[...nextauth]/route.ts (2 hunks)
  • frontend/src/components/ModuleForm.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-07-12T17:14:28.536Z
Learnt from: Rajgupta36
Repo: OWASP/Nest PR: 1717
File: frontend/src/app/mentorship/programs/[programKey]/edit/page.tsx:90-128
Timestamp: 2025-07-12T17:14:28.536Z
Learning: Both ProgramForm (programCard.tsx) and ModuleForm (mainmoduleCard.tsx) components already implement HTML validation using the `required` attribute on form fields. The browser's native validation prevents form submission and displays error messages when required fields are empty, eliminating the need for additional JavaScript validation before GraphQL mutations.

Applied to files:

  • frontend/src/components/ModuleForm.tsx

@nileshpahari nileshpahari force-pushed the fix/incorrect-error-constructor-usage-2803 branch from cf43342 to 7bcb563 Compare December 7, 2025 21:16
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
frontend/src/components/ModuleForm.tsx (1)

286-290: Good: use setError instead of throwing; consider a better fallback for non-Error values

Switching from throwing to setError fixes the unhandled rejection and correctly surfaces failures in the UI while still clearing results and hiding suggestions. One small improvement: for non-Error values, the message currently ends with an empty suffix; you could preserve more context or at least show a generic fallback, e.g.:

setError(
  `Error fetching suggestions: ${
    err instanceof Error ? err.message : String(err || 'Unknown error')
  }`
)
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6217a9b and 7bcb563.

📒 Files selected for processing (2)
  • frontend/src/app/api/auth/[...nextauth]/route.ts (2 hunks)
  • frontend/src/components/ModuleForm.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • frontend/src/app/api/auth/[...nextauth]/route.ts
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: Rajgupta36
Repo: OWASP/Nest PR: 1717
File: frontend/src/app/mentorship/programs/[programKey]/edit/page.tsx:90-128
Timestamp: 2025-07-12T17:14:28.536Z
Learning: Both ProgramForm (programCard.tsx) and ModuleForm (mainmoduleCard.tsx) components already implement HTML validation using the `required` attribute on form fields. The browser's native validation prevents form submission and displays error messages when required fields are empty, eliminating the need for additional JavaScript validation before GraphQL mutations.
📚 Learning: 2025-07-12T17:14:28.536Z
Learnt from: Rajgupta36
Repo: OWASP/Nest PR: 1717
File: frontend/src/app/mentorship/programs/[programKey]/edit/page.tsx:90-128
Timestamp: 2025-07-12T17:14:28.536Z
Learning: Both ProgramForm (programCard.tsx) and ModuleForm (mainmoduleCard.tsx) components already implement HTML validation using the `required` attribute on form fields. The browser's native validation prevents form submission and displays error messages when required fields are empty, eliminating the need for additional JavaScript validation before GraphQL mutations.

Applied to files:

  • frontend/src/components/ModuleForm.tsx

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
frontend/src/components/ModuleForm.tsx (1)

289-295: Error handling is solid; consider simplifying nested ternary for readability

The new error handling correctly derives a user-facing message from different err types and surfaces it via setError, which is a good improvement over throwing. The nested ternary, though, is a bit hard to scan (and was flagged by Sonar); an if / else if / else chain would be clearer.

You could refactor like this:

-        const errorMessage =
-          err instanceof Error
-            ? err.message
-            : typeof err === 'string'
-              ? err
-              : 'An unexpected error occurred'
-        setError(`Error fetching suggestions: ${errorMessage}`)
+        let errorMessage: string
+
+        if (err instanceof Error) {
+          errorMessage = err.message
+        } else if (typeof err === 'string') {
+          errorMessage = err
+        } else {
+          errorMessage = 'An unexpected error occurred'
+        }
+
+        setError(`Error fetching suggestions: ${errorMessage}`)
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7bcb563 and 1fade10.

📒 Files selected for processing (1)
  • frontend/src/components/ModuleForm.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: Rajgupta36
Repo: OWASP/Nest PR: 1717
File: frontend/src/app/api/auth/[...nextauth]/route.ts:13-25
Timestamp: 2025-08-10T11:08:47.258Z
Learning: In the OWASP Nest codebase (frontend/src/app/api/auth/[...nextauth]/route.ts), input validation and string trimming for authentication-related queries like `isProjectLeader` and `isMentor` are handled in the backend rather than the frontend. The backend is responsible for sanitizing and validating input parameters.
Learnt from: Rajgupta36
Repo: OWASP/Nest PR: 1717
File: frontend/src/app/mentorship/programs/[programKey]/edit/page.tsx:90-128
Timestamp: 2025-07-12T17:14:28.536Z
Learning: Both ProgramForm (programCard.tsx) and ModuleForm (mainmoduleCard.tsx) components already implement HTML validation using the `required` attribute on form fields. The browser's native validation prevents form submission and displays error messages when required fields are empty, eliminating the need for additional JavaScript validation before GraphQL mutations.
📚 Learning: 2025-07-12T17:14:28.536Z
Learnt from: Rajgupta36
Repo: OWASP/Nest PR: 1717
File: frontend/src/app/mentorship/programs/[programKey]/edit/page.tsx:90-128
Timestamp: 2025-07-12T17:14:28.536Z
Learning: Both ProgramForm (programCard.tsx) and ModuleForm (mainmoduleCard.tsx) components already implement HTML validation using the `required` attribute on form fields. The browser's native validation prevents form submission and displays error messages when required fields are empty, eliminating the need for additional JavaScript validation before GraphQL mutations.

Applied to files:

  • frontend/src/components/ModuleForm.tsx
🪛 GitHub Check: SonarCloud Code Analysis
frontend/src/components/ModuleForm.tsx

[warning] 292-294: Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=OWASP_Nest&issues=AZr6uj6rki_2q7OKSyPX&open=AZr6uj6rki_2q7OKSyPX&pullRequest=2824

@nileshpahari nileshpahari force-pushed the fix/incorrect-error-constructor-usage-2803 branch from 1fade10 to bed93a6 Compare December 7, 2025 22:27
@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 8, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect Error Constructor Usage

1 participant