Skip to content

refactor: dedupe with PromiseWithError#310

Merged
dinwwwh merged 1 commit into
mainfrom
refactor/dedupe-with-PromiseWithError
Mar 30, 2025
Merged

refactor: dedupe with PromiseWithError#310
dinwwwh merged 1 commit into
mainfrom
refactor/dedupe-with-PromiseWithError

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented Mar 30, 2025

Summary by CodeRabbit

  • New Features

    • Introduced an enhanced error-handling mechanism for asynchronous operations, offering more robust error management.
  • Refactor

    • Unified and streamlined error processing across different modules for improved stability and consistency.
  • Tests

    • Updated test cases to validate the new error-handling approach and ensure continued system reliability.

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 30, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
orpc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 30, 2025 3:24am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 30, 2025

Walkthrough

This pull request updates several type definitions and test expectations to use the new PromiseWithError type. The changes introduce an import for PromiseWithError and modify previously complex union types combining Promise and an optional error property to use the newly defined type. Updates were made in client types, interceptor implementation and tests, as well as shared type tests to harmonize error handling and improve type safety across the codebase.

Changes

Files Change Summary
packages/client/src/types.ts Added import for PromiseWithError from @orpc/shared and updated ClientPromiseResult type definition to use PromiseWithError<TOutput, TError>.
packages/shared/src/interceptor.ts,
packages/shared/src/interceptor.test-d.ts
Updated function signatures and test cases in interceptor flow to return and expect PromiseWithError instead of the previous union of Promise and an optional error property.
packages/shared/src/types.ts,
packages/shared/src/types.test-d.ts
Introduced new type definition for PromiseWithError<T, TError> in types, updated import statements, and added tests to verify that it extends Promise correctly and carries the appropriate error type.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Interceptor
    participant NextFunction
    participant ErrorHandler
    participant FinishHandler

    Client->>Interceptor: Invoke operation with options
    Interceptor->>NextFunction: Call next() -> PromiseWithError
    alt Success Response
        NextFunction-->>Interceptor: Return success result
        Interceptor->>FinishHandler: Execute onFinish(success state)
    else Error Occurs
        NextFunction-->>Interceptor: Return promise with __error set
        Interceptor->>ErrorHandler: Execute onError(extracted error)
        Interceptor->>FinishHandler: Execute onFinish(error state)
    end
Loading

Possibly related PRs

  • unnoq/orpc#210: Updates to return type expectations using PromiseWithError align closely with the changes introduced here.
  • unnoq/orpc#247: Involves similar modifications to type definitions and error handling flow using PromiseWithError, creating a direct code-level connection.

Poem

I hopped through code with glee,
Changing types so gracefully.
Errors now have a home to stay,
In PromiseWithError they safely play.
🐇 CodeRabbit cheers on this bright day!

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 30, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Mar 30, 2025

Open in Stackblitz

More templates

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@310

@orpc/client

npm i https://pkg.pr.new/@orpc/client@310

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@310

@orpc/openapi-client

npm i https://pkg.pr.new/@orpc/openapi-client@310

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@310

@orpc/react

npm i https://pkg.pr.new/@orpc/react@310

@orpc/react-query

npm i https://pkg.pr.new/@orpc/react-query@310

@orpc/server

npm i https://pkg.pr.new/@orpc/server@310

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@310

@orpc/solid-query

npm i https://pkg.pr.new/@orpc/solid-query@310

@orpc/standard-server

npm i https://pkg.pr.new/@orpc/standard-server@310

@orpc/standard-server-fetch

npm i https://pkg.pr.new/@orpc/standard-server-fetch@310

@orpc/standard-server-node

npm i https://pkg.pr.new/@orpc/standard-server-node@310

@orpc/svelte-query

npm i https://pkg.pr.new/@orpc/svelte-query@310

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@310

@orpc/vue-colada

npm i https://pkg.pr.new/@orpc/vue-colada@310

@orpc/vue-query

npm i https://pkg.pr.new/@orpc/vue-query@310

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@310

commit: f20da1c

Copy link
Copy Markdown

@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 (2)
packages/shared/src/interceptor.ts (2)

60-60: Consider avoiding any type

While the code functions correctly, the use of as any bypasses TypeScript's type checking. Consider using a more specific type or a type guard if possible.

- await callback(error as any, options, ...rest)
+ await callback(error as ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : unknown, options, ...rest)

81-81: Consider using a more specific type than any

Using any for the state variable reduces type safety. Consider using the OnFinishState type with appropriate generic parameters:

- let state: any
+ let state: OnFinishState<
+   Awaited<ReturnType<TOptions['next']>>,
+   ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : unknown
+ > | undefined
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 783ae4e and f20da1c.

📒 Files selected for processing (5)
  • packages/client/src/types.ts (2 hunks)
  • packages/shared/src/interceptor.test-d.ts (4 hunks)
  • packages/shared/src/interceptor.ts (4 hunks)
  • packages/shared/src/types.test-d.ts (2 hunks)
  • packages/shared/src/types.ts (1 hunks)
🧰 Additional context used
🧬 Code Definitions (3)
packages/shared/src/interceptor.test-d.ts (2)
packages/shared/src/interceptor.ts (2)
  • Interceptor (14-18)
  • onStart (23-30)
packages/shared/src/types.ts (1)
  • PromiseWithError (5-5)
packages/client/src/types.ts (1)
packages/shared/src/types.ts (1)
  • PromiseWithError (5-5)
packages/shared/src/types.test-d.ts (1)
packages/shared/src/types.ts (1)
  • PromiseWithError (5-5)
🔇 Additional comments (14)
packages/shared/src/types.ts (1)

5-5: Well-designed type for error handling

The new PromiseWithError type creates a clean, reusable pattern for attaching error information to Promise objects. This type will help standardize error handling across the codebase and reduce duplication.

packages/shared/src/interceptor.test-d.ts (2)

2-2: Good addition of import for new type

Adding the import for PromiseWithError makes sense given the type test updates that follow.


9-9: Nice simplification of type tests

Replacing the complex intersection type with the new PromiseWithError type makes these tests more readable and maintainable while still testing the same functionality.

Also applies to: 19-19, 29-29, 39-39

packages/shared/src/types.test-d.ts (2)

1-1: Appropriate import update

Adding the PromiseWithError import here is necessary for the new test case.


16-21: Good test coverage for the new type

This test case thoroughly validates that the PromiseWithError type behaves as expected with respect to type inference. Testing with union types including undefined and null is particularly valuable as these are common edge cases.

packages/client/src/types.ts (2)

1-2: Appropriate new import

Adding the import for PromiseWithError from @orpc/shared is necessary for its usage in the updated type definition.


18-18: Good refactoring to use the shared type

Replacing the custom promise-with-error implementation with the shared PromiseWithError type reduces duplication and creates a more consistent API across the codebase. The constraint TError extends Error is maintained from the original definition, ensuring type safety.

packages/shared/src/interceptor.ts (7)

2-2: Import successfully adds the new PromiseWithError type

This import aligns with the PR objective of deduplicating code by introducing the shared PromiseWithError type.


11-11: Improved type clarity with PromiseWithError

The return type update for the next method improves type safety by using the dedicated PromiseWithError type instead of a more complex union type construction.


18-18: Consistent return type update

The Interceptor return type is now consistently using PromiseWithError, maintaining alignment with the updated InterceptorOptions type.


48-54: Enhanced type extraction for error handling

The updated onError function now properly extracts the error type from PromiseWithError using conditional types, providing better type inference when using the interceptor pattern.


71-80: Improved state type handling for onFinish

The onFinish function now correctly derives the error type from PromiseWithError, maintaining consistency with the onError implementation and improving type inference.


90-91: Simplified error handling

The error handling is now simpler without explicit type casting, leveraging the PromiseWithError type structure effectively.


94-94: Cleaner callback implementation

The callback now receives the correctly typed state without needing additional type assertions, improving code readability.

@dinwwwh dinwwwh merged commit aac73c2 into main Mar 30, 2025
8 checks passed
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