Skip to content

refactor: isORPCErrorStatus -> ORPCError.isValidStatus#339

Closed
dinwwwh wants to merge 1 commit intomainfrom
refactor/isORPCErrorStatus-to-ORPCError-isValidStatus
Closed

refactor: isORPCErrorStatus -> ORPCError.isValidStatus#339
dinwwwh wants to merge 1 commit intomainfrom
refactor/isORPCErrorStatus-to-ORPCError-isValidStatus

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented Apr 8, 2025

Summary by CodeRabbit

  • Refactor

    • Enhanced error validation logic to provide more consistent handling of unexpected responses.
  • Tests

    • Updated the testing framework to verify the new error validation approach, ensuring greater reliability across the system.

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 8, 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 Apr 8, 2025 4:35am

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 8, 2025

Walkthrough

The changes refactor error handling across multiple modules by replacing the standalone function isORPCErrorStatus with the static method ORPCError.isValidStatus. Imports have been streamlined to directly import ORPCError. Both production code and test files in client, contract, and openapi adapters now use the new method for validating HTTP status codes, and test assertions have been updated accordingly, including new test cases verifying status validity.

Changes

Files Change Summary
packages/client/src/adapters/standard/rpc-link-codec.test.ts, packages/openapi-client/src/adapters/standard/openapi-link-codec.test.ts, packages/contract/src/procedure.test.ts Updated imports to directly import ORPCError, replaced spying on isORPCErrorStatus with ORPCError.isValidStatus, and modified assertions on the spy calls.
packages/client/src/adapters/standard/rpc-link-codec.ts, packages/openapi-client/src/adapters/standard/openapi-link-codec.ts, packages/contract/src/procedure.ts Replaced calls to isORPCErrorStatus with ORPCError.isValidStatus in status validations and conditional checks.
packages/client/src/error.test.ts Removed isORPCErrorStatus import and added a new test case for ORPCError.isValidStatus, including additional assertions on HTTP status codes.
packages/client/src/error.ts Removed the standalone isORPCErrorStatus function and introduced a static isValidStatus method on the ORPCError class for error status validation.

Sequence Diagram(s)

sequenceDiagram
    participant Decoder as Decoder (e.g., rpc-link-codec.ts)
    participant ORPCError as ORPCError
    participant ErrorHandler as Error Handler
    Decoder->>ORPCError: isValidStatus(response.status)
    alt Status Valid
        ORPCError-->>Decoder: true
        Decoder->>Decoder: Process response normally
    else Status Invalid
        ORPCError-->>Decoder: false
        Decoder->>ErrorHandler: Throw error using ORPCError
    end
Loading

Possibly related PRs

  • unnoq/orpc#317: Updates error handling in rpc-link-codec.test.ts by migrating to ORPCError.isValidStatus.
  • unnoq/orpc#298: Replaces isORPCErrorStatus with ORPCError.isValidStatus in test cases for consistent error validation.
  • unnoq/orpc#239: Adjusts error handling logic in rpc-link-codec by transitioning to the ORPCError.isValidStatus method for validating error statuses.

Poem

In the code field where errors do hide,
I hop with joy, my rabbit eyes wide.
Function replaced, a method so bright,
ORPCError now steers error’s flight.
With each test that passes, I dance in delight!
🐇💻

✨ 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 Apr 8, 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 Apr 8, 2025

More templates

@orpc/arktype

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

@orpc/client

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

@orpc/contract

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

@orpc/openapi

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

@orpc/openapi-client

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

@orpc/react

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

@orpc/react-query

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

@orpc/server

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

@orpc/shared

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

@orpc/solid-query

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

@orpc/standard-server

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

@orpc/standard-server-fetch

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

@orpc/standard-server-node

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

@orpc/svelte-query

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

@orpc/valibot

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

@orpc/vue-colada

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

@orpc/vue-query

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

@orpc/zod

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

commit: 2bf125a

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 (1)
packages/contract/src/procedure.ts (1)

33-33: Consider using optional chaining for cleaner code.

The static analysis tool suggested that this line would benefit from optional chaining for better readability and to prevent potential errors.

-    if (Object.values(def.errorMap).some(val => val && val.status && !ORPCError.isValidStatus(val.status))) {
+    if (Object.values(def.errorMap).some(val => val?.status && !ORPCError.isValidStatus(val.status))) {
🧰 Tools
🪛 Biome (1.9.4)

[error] 33-33: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 32a6de9 and 2bf125a.

📒 Files selected for processing (8)
  • packages/client/src/adapters/standard/rpc-link-codec.test.ts (2 hunks)
  • packages/client/src/adapters/standard/rpc-link-codec.ts (2 hunks)
  • packages/client/src/error.test.ts (2 hunks)
  • packages/client/src/error.ts (3 hunks)
  • packages/contract/src/procedure.test.ts (1 hunks)
  • packages/contract/src/procedure.ts (2 hunks)
  • packages/openapi-client/src/adapters/standard/openapi-link-codec.test.ts (3 hunks)
  • packages/openapi-client/src/adapters/standard/openapi-link-codec.ts (2 hunks)
🧰 Additional context used
🧬 Code Definitions (4)
packages/contract/src/procedure.ts (3)
packages/client/src/error.ts (1)
  • ORPCError (100-165)
packages/server/src/index.ts (1)
  • ORPCError (23-23)
packages/contract/src/index.ts (1)
  • ORPCError (17-17)
packages/client/src/error.test.ts (1)
packages/client/src/error.ts (1)
  • ORPCError (100-165)
packages/openapi-client/src/adapters/standard/openapi-link-codec.ts (1)
packages/client/src/error.ts (1)
  • ORPCError (100-165)
packages/client/src/adapters/standard/rpc-link-codec.ts (1)
packages/client/src/error.ts (1)
  • ORPCError (100-165)
🪛 Biome (1.9.4)
packages/contract/src/procedure.ts

[error] 33-33: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (19)
packages/client/src/error.test.ts (3)

1-1: LGTM! Import statement updated to reflect refactoring.

The import statement has been correctly updated to import ORPCError directly, removing the now-deleted isORPCErrorStatus function reference.


83-83: Good addition - comprehensive test coverage for invalid status in JSON.

This new test case ensures proper validation when a JSON object contains an invalid status code. This provides good complementary coverage to the isValidStatus test.


87-94: LGTM! Comprehensive test coverage for the new static method.

The new test case for isValidStatus properly verifies the behavior of the method with multiple status code inputs, ensuring it correctly identifies valid error status codes (< 200 or >= 400) and rejects invalid ones.

packages/client/src/error.ts (3)

107-107: LGTM! Correctly updated to use the new static method.

The constructor now correctly references the static isValidStatus method instead of the previous standalone function.


131-133: Good implementation of the static method.

The method correctly implements the same logic as the previous standalone function. This refactoring improves encapsulation by keeping the status validation logic within the ORPCError class.


161-161: LGTM! Updated reference to the static method.

The isValidJSON method now correctly calls the static isValidStatus method.

packages/contract/src/procedure.test.ts (2)

1-1: LGTM! Updated import to directly reference ORPCError.

The import statement has been correctly updated to import ORPCError directly rather than the entire ClientModule.


5-5: LGTM! Spy correctly updated to reference the new static method.

The spy has been properly updated to target ORPCError.isValidStatus instead of the previous standalone function.

packages/contract/src/procedure.ts (2)

5-5: LGTM! Updated import to reference ORPCError directly.

The import statement has been correctly updated to import ORPCError from '@orpc/client'.


29-29: LGTM! Updated to use static method.

The conditional check now correctly uses ORPCError.isValidStatus to validate the route's success status.

packages/openapi-client/src/adapters/standard/openapi-link-codec.ts (1)

191-191: Good refactoring to use ORPCError.isValidStatus

The change from using the standalone function isORPCErrorStatus to the static method ORPCError.isValidStatus is a good encapsulation of error handling logic within the ORPCError class.

packages/client/src/adapters/standard/rpc-link-codec.ts (2)

7-7: Clean up imports

Good update to directly import ORPCError from the error module instead of importing the standalone function.


108-108: Improved error handling approach

Using ORPCError.isValidStatus instead of a standalone function provides better encapsulation of error handling logic within the ORPCError class while maintaining the same functionality.

packages/client/src/adapters/standard/rpc-link-codec.test.ts (3)

2-2: Updated import to match implementation changes

Correctly updated the import to directly import ORPCError instead of the entire ErrorModule, which aligns with the implementation changes.


8-8: Updated test spy to reflect implementation change

The spy has been correctly updated to target ORPCError.isValidStatus instead of the previous standalone function.


189-189: Test assertion updated for flexibility

Changed from checking the exact number of calls to simply verifying the method was called, which provides more flexibility for future modifications.

packages/openapi-client/src/adapters/standard/openapi-link-codec.test.ts (3)

1-1: Updated import statement

Correctly updated the import to directly import ORPCError from '@orpc/client', aligning with the implementation changes.


10-10: Updated test spy to reflect implementation change

The spy is now correctly targeting ORPCError.isValidStatus instead of the previous standalone function.


380-380: More flexible test assertion

Changed from checking the exact number of calls to simply verifying the method was called, which provides more flexibility while still ensuring the expected behavior.

@dinwwwh
Copy link
Copy Markdown
Member Author

dinwwwh commented Apr 8, 2025

I don't like this, I think we should migrate out of static methods

@dinwwwh dinwwwh closed this Apr 8, 2025
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