packages feed

hix-0.1.0.0: lib/Hix/Data/Error.hs

module Hix.Data.Error where

import Control.Monad.Trans.Except (ExceptT, throwE)
import qualified Data.Text.IO as Text
import Exon (exon)
import Path (Path, toFilePath)
import System.IO (stderr)
import System.IO.Error (tryIOError)

data Error =
  PreprocError Text
  |
  EnvError Text
  |
  GhciError Text
  |
  NoMatch Text
  |
  Fatal Text
  deriving stock (Eq, Show, Generic)

pathText :: Path b t -> Text
pathText =
  toText . toFilePath

printPreprocError ::
  MonadIO m =>
  Text ->
  m ()
printPreprocError msg =
  liftIO (Text.hPutStrLn stderr [exon|>>> Preprocessor generator failed: #{msg}|])

printEnvError ::
  MonadIO m =>
  Text ->
  m ()
printEnvError msg =
  liftIO (Text.hPutStrLn stderr [exon|>>> Invalid env config: #{msg}|])

printGhciError ::
  MonadIO m =>
  Text ->
  m ()
printGhciError msg =
  liftIO (Text.hPutStrLn stderr [exon|>>> Invalid ghci config: #{msg}|])

printFatalError ::
  MonadIO m =>
  Text ->
  m ()
printFatalError msg =
  liftIO (Text.hPutStrLn stderr [exon|>>> Fatal error: #{msg}|])

sourceError :: Text -> Path b t -> Text
sourceError reason source =
  [exon|#{reason} the source file '#{pathText source}'|]

tryIO ::
  IO a ->
  ExceptT Error IO a
tryIO ma =
  liftIO (tryIOError ma) >>= \case
    Right a -> pure a
    Left err -> throwE (Fatal (show err))

note :: Text -> Maybe a -> ExceptT Error IO a
note err =
  maybe (throwE (GhciError err)) pure