Skip to content

Instantly share code, notes, and snippets.

@cgibbard
Last active May 23, 2019 09:12
Show Gist options
  • Save cgibbard/c0eda4da4f6118aa58fee923010fee17 to your computer and use it in GitHub Desktop.
Save cgibbard/c0eda4da4f6118aa58fee923010fee17 to your computer and use it in GitHub Desktop.
example of refinements of IOException
module Control.Exception.IOException where
import Control.Exception
import System.IO.Error
newtype AlreadyExists = AlreadyExists IOException
deriving (Eq, Show)
instance Exception AlreadyExists where
toException (AlreadyExists e) = toException e
fromException e = case fromException e of
Just e | isAlreadyExistsError e -> Just (AlreadyExists e)
_ -> Nothing
newtype DoesNotExistError = DoesNotExistError IOException
deriving (Eq, Show)
instance Exception DoesNotExistError where
toException (DoesNotExistError e) = toException e
fromException e = case fromException e of
Just e | isDoesNotExistError e -> Just (DoesNotExistError e)
_ -> Nothing
newtype AlreadyInUseError = AlreadyInUseError IOException
deriving (Eq, Show)
instance Exception AlreadyInUseError where
toException (AlreadyInUseError e) = toException e
fromException e = case fromException e of
Just e | isAlreadyInUseError e -> Just (AlreadyInUseError e)
_ -> Nothing
newtype FullError = FullError IOException
deriving (Eq, Show)
instance Exception FullError where
toException (FullError e) = toException e
fromException e = case fromException e of
Just e | isFullError e -> Just (FullError e)
_ -> Nothing
newtype EOFError = EOFError IOException
deriving (Eq, Show)
instance Exception EOFError where
toException (EOFError e) = toException e
fromException e = case fromException e of
Just e | isEOFError e -> Just (EOFError e)
_ -> Nothing
newtype IllegalOperation = IllegalOperation IOException
deriving (Eq, Show)
instance Exception IllegalOperation where
toException (IllegalOperation e) = toException e
fromException e = case fromException e of
Just e | isIllegalOperation e -> Just (IllegalOperation e)
_ -> Nothing
newtype PermissionError = PermissionError IOException
deriving (Eq, Show)
instance Exception PermissionError where
toException (PermissionError e) = toException e
fromException e = case fromException e of
Just e | isPermissionError e -> Just (PermissionError e)
_ -> Nothing
newtype UserError = UserError IOException
deriving (Eq, Show)
instance Exception UserError where
toException (UserError e) = toException e
fromException e = case fromException e of
Just e | isUserError e -> Just (UserError e)
_ -> Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment