-
Notifications
You must be signed in to change notification settings - Fork 6
Add checked exceptions for run
#3
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
Open
joneshf
wants to merge
1
commit into
natefaubion:master
Choose a base branch
from
joneshf:run-time
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
-- | ## Extensible checked exceptions with polymorphic variants | ||
-- | | ||
-- | This module provides helpers for using `Variant` with `Except`. When | ||
-- | combined, we get a mechanism for extensible, checked exceptions. That | ||
-- | is, exceptions can be defined and used anywhere, and handled as needed. | ||
-- | Handling an exception eliminates it from the type, giving us proof | ||
-- | that it no longer occurs. | ||
module Run.Except.Checked | ||
( EXCEPTV | ||
, handleError | ||
, safe | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Either (either) | ||
import Data.Variant (class VariantMatchCases, Variant, case_, onMatch) | ||
import Run (Run, expand) | ||
import Run.Except (EXCEPT, runExcept, throw) | ||
import Type.Row (class RowToList) | ||
|
||
type EXCEPTV exc = EXCEPT (Variant exc) | ||
|
||
-- | Catches and eliminates exceptions given a record of handlers. | ||
-- | Unhandled exceptions are re-propragated. Record fields map | ||
-- | to the label for the exception being handled. | ||
-- | | ||
-- | An example for handling HTTP exceptions might be: | ||
-- | ```purescript | ||
-- | request # handleError | ||
-- | { httpNotFound: \_ -> ... | ||
-- | , httpServerError: \error -> ... | ||
-- | } | ||
-- | ``` | ||
handleError | ||
∷ ∀ m handlers excHandled excIn excOut rl r a | ||
. RowToList handlers rl | ||
⇒ VariantMatchCases rl excHandled (Run (except :: EXCEPTV excOut | r) a) | ||
⇒ Union excHandled excOut excIn | ||
⇒ Union r m (except :: EXCEPTV excOut | r) | ||
⇒ { | handlers } | ||
→ Run (except :: EXCEPTV excIn | r) a | ||
→ Run (except :: EXCEPTV excOut | r) a | ||
handleError cases = runExcept >>> expand >=> either (onMatch cases throw) pure | ||
|
||
-- | Safely removes the `Except` layer when all exceptions have been handled. | ||
safe | ||
∷ ∀ r a | ||
. Run (except :: EXCEPTV () | r) a | ||
→ Run r a | ||
safe = runExcept >>> map (either case_ id) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure about this bit of the type.
The
expand
below forced this constraint. Is it right? Maybe there's another way to do the same thing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the expand necessary if you account for duplicate labels? That is, something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like dropping the
expand
and changing to the signature you suggest allows dropping theUnion r m (except :: EXCEPTV excOut | r)
constraint.Which would you prefer: with expand and constraint, or without?