Skip to content

fix: add error handling to authentication controllers#56

Open
om-dev007 wants to merge 1 commit into
Rishabhworkspace:mainfrom
om-dev007:fix/issue-53-auth-error-handling
Open

fix: add error handling to authentication controllers#56
om-dev007 wants to merge 1 commit into
Rishabhworkspace:mainfrom
om-dev007:fix/issue-53-auth-error-handling

Conversation

@om-dev007

@om-dev007 om-dev007 commented Jun 23, 2026

Copy link
Copy Markdown

Summary

This PR adds proper error handling to authentication controllers by wrapping asynchronous operations in try-catch blocks.

Changes Made
Added try-catch blocks to registerUser
Added try-catch blocks to loginUser
Improved error handling for authentication-related operations
Added server error responses for unexpected failures
Added error logging for easier debugging
Why

Authentication controllers perform multiple asynchronous operations such as database queries and password hashing. Without proper error handling, unexpected failures can lead to unhandled exceptions and inconsistent API responses.

Issue

Closes #53

Summary by CodeRabbit

  • Bug Fixes
    • User registration now returns descriptive error messages when unexpected errors occur
    • Google authentication error responses are now more consistent and informative

@vercel

vercel Bot commented Jun 23, 2026

Copy link
Copy Markdown

@om-dev007 is attempting to deploy a commit to the rishabhjtripathi2903-3434's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

registerUser in authController.ts is wrapped in a try/catch block so unhandled runtime errors return a 500 response with { success: false, message } instead of crashing the request. googleAuth's catch block is changed from returning 400 with { message: 'Google Auth Failed' } to logging Google Auth Error: and returning 500 with { success: false, message: 'Google authentication failed' }.

Changes

Auth Controller Error Handling

Layer / File(s) Summary
registerUser try/catch and googleAuth 500 response
backend/src/controllers/authController.ts
registerUser wraps the full registration flow in a try/catch; unhandled exceptions are logged and return 500 with { success: false, message }. googleAuth's catch block switches from 400 { message: 'Google Auth Failed' } to a logged 500 { success: false, message: 'Google authentication failed' }. getMe is unchanged beyond minor formatting.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐇 Hop hop, no more crashes in the night,
When Prisma stumbles, we catch it right!
A try wraps the login, a catch holds the fall,
Five-hundred replies stand guard for us all.
The rabbit sleeps soundly — errors are tamed. 🌙

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding error handling to authentication controllers, which is the core objective of this PR.
Linked Issues check ✅ Passed The PR implements try-catch blocks in registerUser and googleAuth, and adds standardized error responses and error logging as required by issue #53, though loginUser changes are not detailed.
Out of Scope Changes check ✅ Passed All changes are focused on error handling in authentication controllers, directly aligned with issue #53; no out-of-scope modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/src/controllers/authController.ts`:
- Around line 64-67: The catch blocks in authController.ts are exposing raw
error messages to clients, which leaks internal implementation details and
violates security practices. In both the catch block around lines 64-67 and the
one around lines 103-105, replace the dynamic error.message with a fixed generic
message like "Internal server error" in the res.status(500).json() response.
Keep the detailed error information (error.message and full error object) only
in server-side logging using a logger or console.error so debugging information
is available internally but not exposed to clients.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 049a1f9a-7534-4ace-90f3-9eabb34ea26f

📥 Commits

Reviewing files that changed from the base of the PR and between e38b451 and b83d353.

📒 Files selected for processing (1)
  • backend/src/controllers/authController.ts

Comment on lines +64 to 67
res.status(500).json({
success: false,
message: error.message ||'Internal server error'
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

Do not return raw exception messages in auth 500 responses (Line 66, Line 105).

Both catch blocks currently expose error.message to clients, which can leak internal implementation details and conflicts with the PR’s “no internal error details” requirement. Return a fixed generic message to clients and keep detailed error info only in server logs.

Suggested patch
-        res.status(500).json({
-            success: false,
-            message: error.message ||'Internal server error'
-        });
+        res.status(500).json({
+            success: false,
+            message: 'Internal server error'
+        });
@@
-        res.status(500).json({
-            success: false,
-            message: error.message || 'Internal server error'
-        });
+        res.status(500).json({
+            success: false,
+            message: 'Internal server error'
+        });

Also applies to: 103-105

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/src/controllers/authController.ts` around lines 64 - 67, The catch
blocks in authController.ts are exposing raw error messages to clients, which
leaks internal implementation details and violates security practices. In both
the catch block around lines 64-67 and the one around lines 103-105, replace the
dynamic error.message with a fixed generic message like "Internal server error"
in the res.status(500).json() response. Keep the detailed error information
(error.message and full error object) only in server-side logging using a logger
or console.error so debugging information is available internally but not
exposed to clients.

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.

Add Proper Error Handling in Authentication Controllers

1 participant