{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE RankNTypes #-}

module Managed.Exception
  ( AgentException(..)
  , throwM
  , explain
  , badNumberOfArgs
  , noParseArg
  , probeRuntimeException
  , badProbeID
  ) where

import Control.Exception (Exception(..), SomeException)
import Control.Monad.Catch (throwM)
import Data.Managed
import GHC.Generics (Generic)

data AgentException
  = BadProbeID ProbeID
  | BadNumberOfArguments Int Int
  | NoParseArgument
  | ProbeRuntimeException String
  deriving (Int -> AgentException -> ShowS
[AgentException] -> ShowS
AgentException -> String
(Int -> AgentException -> ShowS)
-> (AgentException -> String)
-> ([AgentException] -> ShowS)
-> Show AgentException
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AgentException] -> ShowS
$cshowList :: [AgentException] -> ShowS
show :: AgentException -> String
$cshow :: AgentException -> String
showsPrec :: Int -> AgentException -> ShowS
$cshowsPrec :: Int -> AgentException -> ShowS
Show, AgentException -> AgentException -> Bool
(AgentException -> AgentException -> Bool)
-> (AgentException -> AgentException -> Bool) -> Eq AgentException
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AgentException -> AgentException -> Bool
$c/= :: AgentException -> AgentException -> Bool
== :: AgentException -> AgentException -> Bool
$c== :: AgentException -> AgentException -> Bool
Eq, (forall x. AgentException -> Rep AgentException x)
-> (forall x. Rep AgentException x -> AgentException)
-> Generic AgentException
forall x. Rep AgentException x -> AgentException
forall x. AgentException -> Rep AgentException x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AgentException x -> AgentException
$cfrom :: forall x. AgentException -> Rep AgentException x
Generic)

instance Exception AgentException where
  displayException :: AgentException -> String
displayException = AgentException -> String
explain

explain :: AgentException -> String
explain :: AgentException -> String
explain (BadProbeID String
pid) =
  String
"Unrecognized ProbeID: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> String
show String
pid
explain (BadNumberOfArguments Int
expected Int
got) =
  String
"Bad number of arguments. Expected " String -> ShowS
forall a. [a] -> [a] -> [a]
++
  Int -> String
forall a. Show a => a -> String
show Int
expected String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", but got " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
got
explain AgentException
NoParseArgument =
  String
"Can't parse probe input argument."
explain (ProbeRuntimeException String
reason) =
  String
"Exception thrown in probe invocation:\n" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
reason

badNumberOfArgs :: Int -> [a] -> AgentException
badNumberOfArgs :: Int -> [a] -> AgentException
badNumberOfArgs Int
n [a]
xs = Int -> Int -> AgentException
BadNumberOfArguments Int
n ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs)

noParseArg :: AgentException
noParseArg :: AgentException
noParseArg = AgentException
NoParseArgument

probeRuntimeException :: SomeException -> AgentException
probeRuntimeException :: SomeException -> AgentException
probeRuntimeException SomeException
exception =
  String -> AgentException
ProbeRuntimeException (SomeException -> String
forall a. Show a => a -> String
show SomeException
exception)

badProbeID :: ProbeID -> AgentException
badProbeID :: String -> AgentException
badProbeID = String -> AgentException
BadProbeID