Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/parsonsmatt/exception-via
Derive hierarchical exception instances for your datatypes
https://github.com/parsonsmatt/exception-via
Last synced: 10 days ago
JSON representation
Derive hierarchical exception instances for your datatypes
- Host: GitHub
- URL: https://github.com/parsonsmatt/exception-via
- Owner: parsonsmatt
- License: bsd-3-clause
- Created: 2020-10-26T03:13:15.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-08-19T00:23:23.000Z (about 1 year ago)
- Last Synced: 2024-04-26T00:30:55.493Z (6 months ago)
- Language: Haskell
- Size: 9.77 KB
- Stars: 5
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
- License: LICENSE
Awesome Lists containing this project
README
# exception-via
Haskell supports hierarchical exceptions, but there's a bit of boilerplate involved.
The [documentation for `Control.Exception`](https://www.stackage.org/haddock/lts-16.20/base-4.13.0.0/Control-Exception.html#t:Exception) has a write-up:```haskell
---------------------------------------------------------------------
-- Make the root exception type for all the exceptions in a compilerdata SomeCompilerException = forall e . Exception e => SomeCompilerException e
instance Show SomeCompilerException where
show (SomeCompilerException e) = show einstance Exception SomeCompilerException
compilerExceptionToException :: Exception e => e -> SomeException
compilerExceptionToException = toException . SomeCompilerExceptioncompilerExceptionFromException :: Exception e => SomeException -> Maybe e
compilerExceptionFromException x = do
SomeCompilerException a <- fromException x
cast a---------------------------------------------------------------------
-- Make a subhierarchy for exceptions in the frontend of the compilerdata SomeFrontendException = forall e . Exception e => SomeFrontendException e
instance Show SomeFrontendException where
show (SomeFrontendException e) = show einstance Exception SomeFrontendException where
toException = compilerExceptionToException
fromException = compilerExceptionFromExceptionfrontendExceptionToException :: Exception e => e -> SomeException
frontendExceptionToException = toException . SomeFrontendExceptionfrontendExceptionFromException :: Exception e => SomeException -> Maybe e
frontendExceptionFromException x = do
SomeFrontendException a <- fromException x
cast a---------------------------------------------------------------------
-- Make an exception type for a particular frontend compiler exceptiondata MismatchedParentheses = MismatchedParentheses
deriving Showinstance Exception MismatchedParentheses where
toException = frontendExceptionToException
fromException = frontendExceptionFromException
```Woof! That's a lot of code just to have nested exceptions.
Especially since Java devs can just write```java
public class CompilerException extends Exception { ... }
public class FrontendException extends CompilerException {....}
```This library attempts to help by providing a `newtype` wrapper you can use with `DerivingVia`.
With basic exceptions, you don't need this - the default methods on `Exception` default to a top-level exception.```haskell
data EasyException = EasyException
deriving stock Show
deriving anyclass Exception
```Let's make those nested exceptions.
```haskell
data SomeCompilerException = forall e . Exception e => SomeCompilerException ederiving stock instance Show SomeCompilerException
deriving anyclass instance Exception SomeCompilerExceptioninstance Hierarchy SomeCompilerException where
toParent = SomeCompilerException
fromParent (SomeCompilerException e) = cast e
```The `Hierarchy` class is required to tell us how to unpack and pack things in the exception type.
Now let's get to the frontend exception.
It's a subtype of `SomeCompilerException`, so we'll derive the `Exception` instance using our `via`-type.```haskell
data SomeFrontendException = forall e . Exception e => SomeFrontendException ederiving stock instance Show SomeFrontendException
deriving
via (SomeFrontendException