Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dart_frog): add tryRead method to RequestContext #1652

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mtwichel
Copy link
Contributor

Status

READY

Description

Closes #1651. Adds a tryRead function to the RequestContext that works exactly like read, but instead of throwing a StateError when a provided value isn't found, it return null. This is useful for objects that may or not be provided depending on certain factors, like a User being provided only if they are authenticated. The change has also been reflected in the docs site.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@mtwichel mtwichel requested a review from a team as a code owner December 19, 2024 19:07
///
/// Returns `null` if [T] is not available within the provided
/// [request] context.
T? tryRead<T>() {
Copy link
Contributor

@alestiago alestiago Jan 2, 2025

Choose a reason for hiding this comment

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

Nit: If we end up adding tryRead I think we should modify the implementation to not catch the StateError, but instead implement tryRead so it does what read is doing right now but without the null check to avoid creating an additional StateError object when using tryRead. We could then base the read implementation on tryRead for code sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

feat: add tryRead<T> function to simplify reading values only if they exist instead
2 participants