Skip to content

Added initial Result implementation#523

Merged
allouis merged 1 commit intomainfrom
result-type
Apr 15, 2025
Merged

Added initial Result implementation#523
allouis merged 1 commit intomainfrom
result-type

Conversation

@allouis
Copy link
Collaborator

@allouis allouis commented Apr 15, 2025

We want to stop throwing Errors and instead returning Results from our methods in an effor to force us to handle errors explicitly and correctly. This is because we're working in a decentralised system where errors are abundant and expected, and we want to make them first class citizens of our application.

The current state of the system of throwing errors is leading to a lot of 500 errors in production, most of which can and should be handled at the application level, rather than bubbling up.

This implementation is very small and essentially boils down to 2 element tuples containing either null and the value, or an error and null.

A few helper functions have been provided to make the DX a little better, with the idea being that we can either build this up, or replace it with a library once we have a better understanding of our use-cases and desired feature set.

We want to stop throwing Errors and instead returning Results from our
methods in an effor to force us to handle errors explicitly and
correctly. This is because we're working in a decentralised system
where errors are abundant and expected, and we want to make them first
class citizens of our application.

The current state of the system of throwing errors is leading to a lot
of 500 errors in production, most of which can and should be handled
at the application level, rather than bubbling up.

This implementation is very small and essentially boils down to 2
element tuples containing either null and the value, or an error and null.

A few helper functions have been provided to make the DX a little
better, with the idea being that we can either build this up, or
replace it with a library once we have a better understanding of our
use-cases and desired feature set.
@coderabbitai
Copy link

coderabbitai bot commented Apr 15, 2025

Walkthrough

A new module has been introduced that defines a generic Result type as a tagged union, composed of two tuple forms: Ok<T> for successful results and Error<E> for error cases. The module provides type aliases for these tuples, where Ok<T> is represented as [null, T] and Error<E> as [E, null]. Several utility functions are included: isError is a type predicate to check if a result is an error; getValue and getError are accessors for extracting the value or error from their respective tuple types; unsafeUnwrap retrieves the value or throws the error; and factory functions ok and error construct the corresponding tuple forms. This design enforces explicit, type-safe handling of both success and error outcomes in code that uses this module.

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
✨ 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 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.

Copy link

@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 (4)
src/core/result.ts (4)

1-3: Minimal and clear Result type definitions.

The tuple-based Ok, Error, and Result types are concise and easy to use. This approach is efficient and idiomatic for TypeScript, especially for enforcing explicit error handling.

However, consider using a unique symbol or branding to prevent accidental misuse of tuples with the same shape elsewhere in the codebase. This is an optional enhancement for stronger type safety.


19-21: Accessor functions assume correct input types.

Both getValue and getError assume the input is the correct tuple type. If misused (e.g., passing an Error to getValue), this will result in undefined or incorrect values at runtime.

Consider adding runtime checks or assertions to guard against incorrect usage, or document clearly that these functions are unsafe unless type-checked (e.g., via isError).

Also applies to: 23-25


1-44: Add unit tests for Result utilities.

There are no tests in this file. To ensure correctness and prevent regressions, add unit tests for all exported functions, especially for edge cases (e.g., misuse of accessors, unsafeUnwrap with error, etc.).

Would you like help generating a test suite for these utilities?


1-44: Consider extending JSDoc comments for all functions.

Some functions have JSDoc comments, but not all. For consistency and better developer experience, consider adding JSDoc comments to all exported functions.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9668045 and 176f8ad.

📒 Files selected for processing (1)
  • src/core/result.ts (1 hunks)
🔇 Additional comments (3)
src/core/result.ts (3)

15-17: Type predicate isError is well-implemented.

The isError function is a proper type predicate, enabling type narrowing in downstream code. The implementation is correct and efficient.


30-35: unsafeUnwrap is clear and matches expectations.

The unsafeUnwrap function throws on error and returns the value otherwise, which is the expected behavior for an "unsafe" unwrap. The implementation is correct and leverages the other utilities well.


37-39: Factory functions are correct and idiomatic.

The ok and error functions are simple, correct, and make constructing results easy and explicit.

Also applies to: 41-43

@allouis allouis enabled auto-merge (rebase) April 15, 2025 03:12
@allouis allouis merged commit bd6fed6 into main Apr 15, 2025
4 checks passed
@allouis allouis deleted the result-type branch April 15, 2025 03:16
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