mirror of
https://github.com/haskell-nix/hnix-store.git
synced 2024-09-11 18:05:56 +03:00
remote: add Trace, BasicError, ErrorInfo
This commit is contained in:
parent
8b9eb48b40
commit
241fa58737
@ -26,6 +26,15 @@ deriving via GenericArbitrary ActivityResult
|
||||
deriving via GenericArbitrary Field
|
||||
instance Arbitrary Field
|
||||
|
||||
deriving via GenericArbitrary Trace
|
||||
instance Arbitrary Trace
|
||||
|
||||
deriving via GenericArbitrary BasicError
|
||||
instance Arbitrary BasicError
|
||||
|
||||
deriving via GenericArbitrary ErrorInfo
|
||||
instance Arbitrary ErrorInfo
|
||||
|
||||
deriving via GenericArbitrary LoggerOpCode
|
||||
instance Arbitrary LoggerOpCode
|
||||
|
||||
|
@ -11,6 +11,7 @@ import Data.Serialize.Put (Putter)
|
||||
import Data.Text (Text)
|
||||
import Data.Word (Word8, Word32)
|
||||
|
||||
import qualified Control.Monad
|
||||
import qualified Data.Bits
|
||||
import qualified Data.Bool
|
||||
import qualified Data.Map
|
||||
@ -152,6 +153,44 @@ instance Serialize Field where
|
||||
put (Field_LogInt x) = putInt (0 :: Word8) >> putInt x
|
||||
put (Field_LogStr x) = putInt (1 :: Word8) >> putText x
|
||||
|
||||
instance Serialize Trace where
|
||||
get = do
|
||||
tracePosition <- (\case 0 -> Nothing; x -> Just x) <$> getInt @Int
|
||||
traceHint <- get
|
||||
pure Trace{..}
|
||||
put Trace{..} = do
|
||||
maybe (putInt @Int 0) putInt $ tracePosition
|
||||
put traceHint
|
||||
|
||||
instance Serialize BasicError where
|
||||
get = do
|
||||
basicErrorMessage <- get
|
||||
basicErrorExitStatus <- getInt
|
||||
pure BasicError{..}
|
||||
put BasicError{..} = do
|
||||
put basicErrorMessage
|
||||
putInt basicErrorExitStatus
|
||||
|
||||
instance Serialize ErrorInfo where
|
||||
get = do
|
||||
etyp <- get @Text
|
||||
Control.Monad.unless (etyp == Data.Text.pack "Error")
|
||||
$ fail
|
||||
$ "get ErrorInfo: received unknown error type" ++ show etyp
|
||||
errorInfoLevel <- get
|
||||
_name <- get @Text -- removed error name
|
||||
errorInfoMessage <- get
|
||||
errorInfoPosition <- (\case 0 -> Nothing; x -> Just x) <$> getInt @Int
|
||||
errorInfoTraces <- getMany get
|
||||
pure ErrorInfo{..}
|
||||
put ErrorInfo{..} = do
|
||||
put $ Data.Text.pack "Error"
|
||||
put errorInfoLevel
|
||||
put $ Data.Text.pack "Error" -- removed error name
|
||||
put errorInfoMessage
|
||||
maybe (putInt @Int 0) putInt $ errorInfoPosition
|
||||
putMany put errorInfoTraces
|
||||
|
||||
instance Serialize LoggerOpCode where
|
||||
get = getInt @Int >>= either fail pure . intToLoggerOpCode
|
||||
put = putInt @Int . loggerOpCodeToInt
|
||||
|
@ -35,6 +35,9 @@ module System.Nix.Store.Remote.Serializer
|
||||
, maybeActivity
|
||||
, activityResult
|
||||
, field
|
||||
, trace
|
||||
, basicError
|
||||
, errorInfo
|
||||
, loggerOpCode
|
||||
, logger
|
||||
, verbosity
|
||||
@ -233,6 +236,15 @@ activityResult = liftSerialize
|
||||
field :: NixSerializer r e Field
|
||||
field = liftSerialize
|
||||
|
||||
trace :: NixSerializer r e Trace
|
||||
trace = liftSerialize
|
||||
|
||||
basicError :: NixSerializer r e BasicError
|
||||
basicError = liftSerialize
|
||||
|
||||
errorInfo :: NixSerializer r e ErrorInfo
|
||||
errorInfo = liftSerialize
|
||||
|
||||
loggerOpCode :: NixSerializer r e LoggerOpCode
|
||||
loggerOpCode = liftSerialize
|
||||
|
||||
|
@ -1,5 +1,8 @@
|
||||
module System.Nix.Store.Remote.Types.Logger
|
||||
( Field(..)
|
||||
, Trace(..)
|
||||
, BasicError(..)
|
||||
, ErrorInfo(..)
|
||||
, Logger(..)
|
||||
, LoggerOpCode(..)
|
||||
, loggerOpCodeToInt
|
||||
@ -18,6 +21,29 @@ data Field
|
||||
| Field_LogInt Int
|
||||
deriving (Eq, Generic, Ord, Show)
|
||||
|
||||
-- | Error trace
|
||||
data Trace = Trace
|
||||
{ tracePosition :: Maybe Int -- Error position, Nix always writes 0 here
|
||||
, traceHint :: Text
|
||||
}
|
||||
deriving (Eq, Generic, Ord, Show)
|
||||
|
||||
data BasicError = BasicError
|
||||
{ basicErrorExitStatus :: Int
|
||||
, basicErrorMessage :: Text
|
||||
}
|
||||
deriving (Eq, Generic, Ord, Show)
|
||||
|
||||
-- | Extended error info
|
||||
-- available for protoVersion_minor >= 26
|
||||
data ErrorInfo = ErrorInfo
|
||||
{ errorInfoLevel :: Verbosity
|
||||
, errorInfoMessage :: Text
|
||||
, errorInfoPosition :: Maybe Int -- Error position, Nix always writes 0 here
|
||||
, errorInfoTraces :: [Trace]
|
||||
}
|
||||
deriving (Eq, Generic, Ord, Show)
|
||||
|
||||
data LoggerOpCode
|
||||
= LoggerOpCode_Next
|
||||
| LoggerOpCode_Read
|
||||
|
@ -17,6 +17,7 @@ import System.Nix.Arbitrary ()
|
||||
import System.Nix.Derivation (Derivation(inputDrvs))
|
||||
import System.Nix.Store.Remote.Arbitrary ()
|
||||
import System.Nix.Store.Remote.Serializer
|
||||
import System.Nix.Store.Remote.Types (ErrorInfo(..), Trace(..))
|
||||
|
||||
-- | Test for roundtrip using @NixSerializer@
|
||||
roundtripSReader
|
||||
@ -93,6 +94,19 @@ spec = parallel $ do
|
||||
prop "Maybe Activity" $ roundtripS maybeActivity
|
||||
prop "ActivityResult" $ roundtripS activityResult
|
||||
prop "Field" $ roundtripS field
|
||||
prop "Trace"
|
||||
$ forAll (arbitrary `suchThat` ((/= Just 0) . tracePosition))
|
||||
$ roundtripS trace
|
||||
prop "BasicError" $ roundtripS basicError
|
||||
prop "ErrorInfo"
|
||||
$ forAll (arbitrary
|
||||
`suchThat`
|
||||
(\ErrorInfo{..}
|
||||
-> errorInfoPosition /= Just 0
|
||||
&& all ((/= Just 0) . tracePosition) errorInfoTraces
|
||||
)
|
||||
)
|
||||
$ roundtripS errorInfo
|
||||
prop "LoggerOpCode" $ roundtripS loggerOpCode
|
||||
prop "Verbosity" $ roundtripS verbosity
|
||||
prop "Logger" $ roundtripS logger
|
||||
|
@ -116,6 +116,19 @@ spec = parallel $ do
|
||||
prop "ActivityID" $ roundtripS @ActivityID
|
||||
prop "Activity" $ roundtripS @Activity
|
||||
prop "Field" $ roundtripS @Field
|
||||
prop "Trace"
|
||||
$ forAll (arbitrary `suchThat` ((/= Just 0) . tracePosition))
|
||||
$ roundtripS @Trace
|
||||
prop "BasicError" $ roundtripS @BasicError
|
||||
prop "ErrorInfo"
|
||||
$ forAll (arbitrary
|
||||
`suchThat`
|
||||
(\ErrorInfo{..}
|
||||
-> errorInfoPosition /= Just 0
|
||||
&& all ((/= Just 0) . tracePosition) errorInfoTraces
|
||||
)
|
||||
)
|
||||
$ roundtripS @ErrorInfo
|
||||
prop "LoggerOpCode" $ roundtripS @LoggerOpCode
|
||||
prop "Verbosity" $ roundtripS @Verbosity
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user