Skip to content

Commit

Permalink
remote: handle both error types in logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sorki committed Nov 26, 2023
1 parent 8456590 commit 4716a6c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
18 changes: 12 additions & 6 deletions hnix-store-remote/src/System/Nix/Store/Remote/Serializer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import Data.Text (Text)
import Data.Time (UTCTime)

import qualified Control.Monad
import qualified Control.Monad.Reader
import qualified Data.HashSet
import qualified Data.Map.Strict
import qualified Data.Set
Expand Down Expand Up @@ -262,9 +263,11 @@ logger = Serializer
LoggerOpCode_Last ->
pure Logger_Last
LoggerOpCode_Error -> do
errorMessage <- getS byteString
errorExitStatus <- getS int
pure Logger_Error{..}
pv <- Control.Monad.Reader.asks hasProtoVersion
Logger_Error <$>
if protoVersion_minor pv >= 26
then Right <$> getS errorInfo
else Left <$> getS basicError
LoggerOpCode_StartActivity -> do
startActivityID <- getS activityID
startActivityVerbosity <- getS verbosity
Expand Down Expand Up @@ -293,10 +296,13 @@ logger = Serializer
putS byteString s
Logger_Last ->
putS loggerOpCode LoggerOpCode_Last
Logger_Error{..} -> do
Logger_Error basicOrInfo -> do
putS loggerOpCode LoggerOpCode_Error
putS byteString errorMessage
putS int errorExitStatus
-- TODO: throwError if we try to send
-- ErrorInfo to client which has no support for it
case basicOrInfo of
Left e -> putS basicError e
Right e -> putS errorInfo e
Logger_StartActivity{..} -> do
putS loggerOpCode LoggerOpCode_StartActivity
putS activityID startActivityID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ data Logger
| Logger_Read Int -- data needed from source
| Logger_Write ByteString -- data for sink
| Logger_Last
| Logger_Error
{ errorExitStatus :: Int
, errorMessage :: ByteString
}
| Logger_Error (Either BasicError ErrorInfo)
| Logger_StartActivity
{ startActivityID :: ActivityID
, startActivityVerbosity :: Verbosity
Expand Down
18 changes: 15 additions & 3 deletions hnix-store-remote/tests/NixSerializerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Data.Fixed (Uni)
import Data.Time (NominalDiffTime)
import Test.Hspec (Expectation, Spec, describe, parallel, shouldBe)
import Test.Hspec.QuickCheck (prop)
import Test.QuickCheck (arbitrary, forAll, suchThat)
import Test.QuickCheck (Gen, arbitrary, forAll, suchThat)
import Test.QuickCheck.Instances ()

import qualified Data.Time.Clock.POSIX
Expand All @@ -17,7 +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(..), ProtoVersion, Trace(..))
import System.Nix.Store.Remote.Types (ErrorInfo(..), Logger(..), ProtoVersion(..), Trace(..))

-- | Test for roundtrip using @NixSerializer@
roundtripSReader
Expand Down Expand Up @@ -109,4 +109,16 @@ spec = parallel $ do
$ roundtripS errorInfo
prop "LoggerOpCode" $ roundtripS loggerOpCode
prop "Verbosity" $ roundtripS verbosity
prop "Logger" $ roundtripSReader @ProtoVersion logger
prop "Logger"
$ forAll (arbitrary :: Gen ProtoVersion)
$ \pv ->
forAll (arbitrary `suchThat` errorInfoIf (protoVersion_minor pv >= 26))
$ roundtripSReader logger pv
where
errorInfoIf True (Logger_Error (Right x)) = noJust0s x
errorInfoIf False (Logger_Error (Left _)) = True
errorInfoIf _ (Logger_Error _) = False
errorInfoIf _ _ = True
noJust0s ErrorInfo{..} =
errorInfoPosition /= Just 0
&& all ((/= Just 0) . tracePosition) errorInfoTraces

0 comments on commit 4716a6c

Please sign in to comment.