{-# LANGUAGE DeriveDataTypeable #-}

module Error
(
  PullE(..),
  CommandError(..),
  putErr
)
where

import System.IO
import System.Exit (ExitCode(..))

import Data.Typeable
import Control.Exception
import Data.ConfigFile

data CommandError =
    ProjectExist
  | NoProject
  | PatchingError
  | DiffError ExitCode
  | NothingToCommit
  | ServerAlreadyStarted
  | ServerNotStarted
  | CanNotConnect (String, String)
  deriving (Eq, Typeable)

data PullE = 
    UncommitedChanges 
  | MergeError
  | PatchError
  | OtherError
  deriving (Eq, Typeable)

instance Exception PullE

instance Exception CommandError

instance Exception CPErrorData

instance Show PullE where
  show UncommitedChanges = "Uncommited Changes."
  show MergeError = "The pulled commit create conflicts."
  show PatchError = "An error occured while patching a pulled commit."
  show _ = "An error occured."

instance Show CommandError where
  show ProjectExist = "Project already exist."
  show NoProject = "No Project here."
  show PatchingError = "An error occured while patching commits."
  show (DiffError x) = case x of
    ExitFailure 1 -> "Differences were found."
    ExitFailure _ -> "An error occured."
  show NothingToCommit = "No change to commit."
  show ServerAlreadyStarted = "A server has already been started."
  show ServerNotStarted = "No server has been started."
  show (CanNotConnect (ip, port)) = "Can not connect to " ++ ip ++ ":" ++ port
  show _ = "An error occured."

putErr :: String -> IO ()
putErr = hPutStrLn stderr