Skip to content

Validate quoteCategory and return proper error status#363

Open
bhavneet334 wants to merge 1 commit intozhravan:mainfrom
bhavneet334:fix/error-handling-and-validation
Open

Validate quoteCategory and return proper error status#363
bhavneet334 wants to merge 1 commit intozhravan:mainfrom
bhavneet334:fix/error-handling-and-validation

Conversation

@bhavneet334
Copy link

@bhavneet334 bhavneet334 commented Dec 30, 2025

  • Add status 500 to error responses in quoteController
  • Add validation for the quoteCategory parameter in quotesService, preventing "Cannot read properties of undefined" errors when invalid categories are used

Summary by CodeRabbit

  • Bug Fixes

    • Added validation for quote categories with descriptive error messages for invalid or empty selections
    • Improved error responses with proper HTTP status codes
    • Fixed background color handling for Unsplash image sources to correctly set transparency
  • Chores

    • Added SVG response headers including Content-Type and cache-control directives

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

@vercel
Copy link

vercel bot commented Dec 30, 2025

@bhavneet334 is attempting to deploy a commit to the shravan20's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Dec 30, 2025

Walkthrough

This PR normalizes formatting across quotesController.js and quotesService.js using consistent double-quote syntax and multi-line structures. It adds validation for quote category existence and non-empty state, introduces explicit background color handling for unsplash sources, sets SVG response headers with cache-control directives, and refines error status codes.

Changes

Cohort / File(s) Summary
Controller Formatting & Response Headers
src/api/controllers/quotesController.js
Normalizes string literals to double quotes; converts single-line defaults to multi-line ternaries. Adds explicit bgSource === "unsplash" handling to set bg_color transparent. Introduces SVG Content-Type and cache-control response headers before sending. Refines error handling with explicit 500 status code. Updates imageController require formatting and module.exports with trailing commas.
Service Validation
src/api/services/quotesService.js
Adds validation to throw "Invalid quote category" if category key missing. Adds check to throw error if category exists but contains no quotes. Preserves existing logic for custom URL and default API fetch branches.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰✨ With double quotes so clean and neat,
And category checks—oh what a treat!
Cache headers set with graceful care,
The quotes now flow through crystal air! 🌟

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Validate quoteCategory and return proper error status' accurately reflects the main changes: adding validation for quoteCategory parameter and improving error handling with proper HTTP status codes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bc57607 and e8360e1.

📒 Files selected for processing (2)
  • src/api/controllers/quotesController.js
  • src/api/services/quotesService.js
🧰 Additional context used
🧬 Code graph analysis (2)
src/api/services/quotesService.js (1)
src/api/controllers/quotesController.js (1)
  • quoteCategory (37-37)
src/api/controllers/quotesController.js (3)
frontend/src/config/cardTemplate/index.js (6)
  • themes (1-47)
  • themes (1-47)
  • animations (48-48)
  • animations (48-48)
  • fonts (50-57)
  • fonts (50-57)
src/fonts/fonts.js (1)
  • fonts (1-30)
src/common/getTemplate.js (1)
  • escapeHtml (2-2)
🔇 Additional comments (10)
src/api/services/quotesService.js (1)

39-46: Excellent validation logic to prevent undefined access errors.

The two-step validation correctly handles both missing categories and empty category arrays:

  1. Line 39 catches undefined/non-existent categories
  2. Line 44 catches valid categories with no quotes

This directly addresses the "Cannot read properties of undefined" error mentioned in the PR objectives.

src/api/controllers/quotesController.js (9)

9-11: Formatting improvement for theme selection.

The multi-line ternary enhances readability. Logic remains unchanged.


18-23: Good addition of explicit transparent background handling for Unsplash images.

Setting theme.bg_color = "transparent" when bgSource === "unsplash" (lines 19-20) ensures background images display correctly. The conditional ordering (bgSource check before bgColor check) is appropriate.


25-25: Consistent default value assignment.

The borderColor default is now explicitly set using the || operator for consistency with other parameters.


27-29: Formatting consistency for animation, layout, and font selection.

Multi-line ternaries improve readability across all parameter selections.

Also applies to: 31-33, 39-39


35-35: Proper default value handling for optional parameters.

Empty string defaults for quotesUrl, quoteCategory, quoteType, and unsplashQuery align with the validation logic in quotesService.js (line 38: else if (quoteCategory)).

Also applies to: 37-37, 41-42


54-55: Added unsplashQuery to quoteObject.

This ensures the unsplash query parameter is properly passed to the service layer. Trailing comma follows modern JavaScript conventions.


70-73: Critical fix: Proper HTTP status code for error responses.

Adding res.status(500) ensures the API returns the correct HTTP status code for server errors, addressing one of the key objectives of this PR. Previously, errors would return status 200 with error details in the body, which violates HTTP semantics.


79-82: Formatting consistency in imageController.

Multi-line function call and double-quote require statement align with the formatting changes throughout the file.


94-97: Module exports formatting update.

Trailing comma added for consistency with modern JavaScript style.


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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant