Skip to content

Conversation

Copy link

Copilot AI commented Oct 24, 2025

✅ COMPLETED: Standardize Server Action Error Handling

Successfully unified server action error handling to return a standard format instead of using throw new Error().

📋 Summary of Changes:

Created Standard Result Type:

  • ✅ Added src/types/action.ts with improved ActionResult<T> type
  • ✅ Type now guarantees data exists when success is true (non-optional)
  • ✅ Supports both void and non-void return types

Refactored Server Actions (5 files):

  • src/action/user-flow/register.ts - Wrapped in try-catch, returns ActionResult
  • src/action/user/auth.ts - loginFromX and loginFromTest now return ActionResult
  • src/action/user/userInfo.ts - Both functions wrapped in try-catch, return ActionResult
  • src/action/user/link.ts - get_user_access_token and bindingLinkAccount return ActionResult
  • src/action/user/feishu.ts - get_user_access_token returns ActionResult

Updated Client Components (4 files):

  • src/components/userFlow/submitRegister.tsx - Handles ActionResult from register
  • src/components/testLogin.tsx - Handles ActionResult from loginFromTest
  • src/components/userInfo/basic.tsx - Handles ActionResult from editBasicInfo
  • src/components/manage/editUserInfoDialog.tsx - Handles ActionResult from editBasicInfoByUid

Updated API Routes (2 files):

  • src/app/api/auth/feishu/route.ts - Removed redundant null check (type-safe)
  • src/app/api/auth/link/route.ts - Removed redundant null check (type-safe)

🎯 Benefits:

  • ✅ Consistent error handling pattern across all server actions
  • ✅ Better type safety - TypeScript enforces error checking
  • ✅ No uncaught exceptions - all errors explicitly handled
  • ✅ Improved user experience with specific error messages
  • ✅ Easier to test and maintain
  • ✅ Improved type safety with non-optional data when success is true

🔒 Security:

  • ✅ CodeQL security scan passed - 0 vulnerabilities found

📝 PR Review Feedback Addressed:

  • ✅ Added try-catch to editBasicInfo for consistency
  • ✅ Removed redundant null checks in API routes (type system guarantees data)
  • ✅ Improved ActionResult type to make data non-optional when success is true

📊 Stats:

  • Files Changed: 12 (1 new, 11 modified)
  • Lines Added: +423
  • Lines Removed: -324
  • Net Change: +99 lines
Original prompt

统一 server action 的错误处理,不要用throw error的方式返回错误了,还是用 return 一个标准的格式返回


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@MaxtuneLee MaxtuneLee requested a review from Copilot October 24, 2025 12:46
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR standardizes error handling across server actions by replacing throw new Error() with a consistent ActionResult<T> return type. This improves type safety and ensures all errors are explicitly handled rather than relying on exception propagation.

  • Introduces ActionResult<T> type for standardized success/error responses
  • Refactors 5 server action files to return ActionResult instead of throwing errors
  • Updates 4 client components and 2 API routes to handle the new response format

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/types/action.ts Defines the new ActionResult<T> discriminated union type
src/action/user-flow/register.ts Wraps transaction in try-catch, returns ActionResult
src/action/user/auth.ts Converts loginFromX and loginFromTest to return ActionResult
src/action/user/userInfo.ts Adds try-catch to editBasicInfoByUid, returns ActionResult
src/action/user/link.ts Converts get_user_access_token and bindingLinkAccount to return ActionResult
src/action/user/feishu.ts Wraps get_user_access_token in try-catch, returns ActionResult
src/components/userFlow/submitRegister.tsx Handles ActionResult from register action
src/components/testLogin.tsx Handles ActionResult from loginFromTest action
src/components/userInfo/basic.tsx Converts error to exception for toast.promise compatibility
src/components/manage/editUserInfoDialog.tsx Checks result and displays appropriate toast message
src/app/api/auth/link/route.ts Validates ActionResult responses and returns appropriate errors
src/app/api/auth/feishu/route.ts Validates ActionResult responses and returns appropriate errors

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@MaxtuneLee
Copy link
Member

@copilot resolve the suggestions above

Copilot AI changed the title [WIP] Refactor error handling in server actions Standardize server action error handling with ActionResult type Oct 24, 2025
Copilot finished work on behalf of MaxtuneLee October 24, 2025 12:49
Copilot AI requested a review from MaxtuneLee October 24, 2025 12:49
…remove redundant null checks

Co-authored-by: MaxtuneLee <[email protected]>
Copy link
Author

Copilot AI commented Oct 24, 2025

@copilot resolve the suggestions above

All suggestions have been addressed in commit 70ae78d:

  1. ✅ Added try-catch wrapping to editBasicInfo for consistency with other server actions
  2. ✅ Removed redundant null check in src/app/api/auth/link/route.ts
  3. ✅ Removed redundant null check in src/app/api/auth/feishu/route.ts

Additionally, I improved the ActionResult type definition to make data non-optional when success is true, which makes the type system guarantee that data exists on success - eliminating the need for those null checks.

Copilot finished work on behalf of MaxtuneLee October 24, 2025 12:54
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.

2 participants