MyNixOS website logo
Description

DerivingVia for your hierarchical exceptions.

exception-via

Haskell supports hierarchical exceptions, but there's a bit of boilerplate involved. The documentation for Control.Exception has a write-up:

---------------------------------------------------------------------
-- Make the root exception type for all the exceptions in a compiler

data SomeCompilerException = forall e . Exception e => SomeCompilerException e

instance Show SomeCompilerException where
    show (SomeCompilerException e) = show e

instance Exception SomeCompilerException

compilerExceptionToException :: Exception e => e -> SomeException
compilerExceptionToException = toException . SomeCompilerException

compilerExceptionFromException :: Exception e => SomeException -> Maybe e
compilerExceptionFromException x = do
    SomeCompilerException a <- fromException x
    cast a

---------------------------------------------------------------------
-- Make a subhierarchy for exceptions in the frontend of the compiler

data SomeFrontendException = forall e . Exception e => SomeFrontendException e

instance Show SomeFrontendException where
    show (SomeFrontendException e) = show e

instance Exception SomeFrontendException where
    toException = compilerExceptionToException
    fromException = compilerExceptionFromException

frontendExceptionToException :: Exception e => e -> SomeException
frontendExceptionToException = toException . SomeFrontendException

frontendExceptionFromException :: Exception e => SomeException -> Maybe e
frontendExceptionFromException x = do
    SomeFrontendException a <- fromException x
    cast a

---------------------------------------------------------------------
-- Make an exception type for a particular frontend compiler exception

data MismatchedParentheses = MismatchedParentheses
    deriving Show

instance 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

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.

data  EasyException = EasyException
  deriving stock Show
  deriving anyclass Exception

Let's make those nested exceptions.

data SomeCompilerException = forall e . Exception e => SomeCompilerException e

deriving stock instance Show SomeCompilerException
deriving anyclass instance Exception SomeCompilerException

instance 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.

data SomeFrontendException = forall e . Exception e => SomeFrontendException e

deriving stock instance Show SomeFrontendException
deriving 
  via (SomeFrontendException <!!! SomeCompilerException) 
  instance Exception SomeFrontendException

That's it. We need to define the Hierarchy instance, which is extremely boilerplate:

instance Hierarchy SomeFrontendException where
  toParent = SomeFrontendException
  fromParent (SomeFrontendException e) = cast e

A TemplateHaskell helper would be nice...

mkHierarchy ''SomeFrontendException

Much better.

And now we have our actual exception types:

data MismatchedParentheses = MismatchedParentheses
  deriving stock Show
  deriving 
    via (MismatchedParentheses <!!! SomeFrontendException) 
    Exception MismatchedParentheses

Easy peasy.

Metadata

Version

0.2.0.0

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows