Catch file opening errors
This commit is contained in:
parent
ca3276cd87
commit
7d7b81cbe3
@ -25,6 +25,7 @@ library
|
|||||||
, Network.Minio.Data.Time
|
, Network.Minio.Data.Time
|
||||||
, Network.Minio.Sign.V4
|
, Network.Minio.Sign.V4
|
||||||
, Network.Minio.API
|
, Network.Minio.API
|
||||||
|
, Network.Minio.Utils
|
||||||
, Network.Minio.XmlParser
|
, Network.Minio.XmlParser
|
||||||
, Network.Minio.XmlGenerator
|
, Network.Minio.XmlGenerator
|
||||||
build-depends: base >= 4.7 && < 5
|
build-depends: base >= 4.7 && < 5
|
||||||
@ -37,7 +38,6 @@ library
|
|||||||
, containers
|
, containers
|
||||||
, cryptonite
|
, cryptonite
|
||||||
, cryptonite-conduit
|
, cryptonite-conduit
|
||||||
, errors
|
|
||||||
, filepath
|
, filepath
|
||||||
, http-client
|
, http-client
|
||||||
, http-conduit
|
, http-conduit
|
||||||
@ -57,6 +57,7 @@ library
|
|||||||
, MultiParamTypeClasses
|
, MultiParamTypeClasses
|
||||||
, MultiWayIf
|
, MultiWayIf
|
||||||
, RankNTypes
|
, RankNTypes
|
||||||
|
, TupleSections
|
||||||
|
|
||||||
executable minio-hs-exe
|
executable minio-hs-exe
|
||||||
hs-source-dirs: app
|
hs-source-dirs: app
|
||||||
@ -87,7 +88,6 @@ test-suite minio-hs-test
|
|||||||
, containers
|
, containers
|
||||||
, cryptonite
|
, cryptonite
|
||||||
, cryptonite-conduit
|
, cryptonite-conduit
|
||||||
, errors
|
|
||||||
, filepath
|
, filepath
|
||||||
, http-client
|
, http-client
|
||||||
, http-conduit
|
, http-conduit
|
||||||
@ -112,6 +112,7 @@ test-suite minio-hs-test
|
|||||||
, MultiParamTypeClasses
|
, MultiParamTypeClasses
|
||||||
, MultiWayIf
|
, MultiWayIf
|
||||||
, RankNTypes
|
, RankNTypes
|
||||||
|
, TupleSections
|
||||||
other-modules: Lib.Prelude
|
other-modules: Lib.Prelude
|
||||||
, Network.Minio
|
, Network.Minio
|
||||||
, Network.Minio.API
|
, Network.Minio.API
|
||||||
@ -121,6 +122,7 @@ test-suite minio-hs-test
|
|||||||
, Network.Minio.Data.Time
|
, Network.Minio.Data.Time
|
||||||
, Network.Minio.S3API
|
, Network.Minio.S3API
|
||||||
, Network.Minio.Sign.V4
|
, Network.Minio.Sign.V4
|
||||||
|
, Network.Minio.Utils
|
||||||
, Network.Minio.XmlGenerator
|
, Network.Minio.XmlGenerator
|
||||||
, Network.Minio.XmlParser
|
, Network.Minio.XmlParser
|
||||||
, XmlTests
|
, XmlTests
|
||||||
|
|||||||
@ -32,6 +32,7 @@ import Lib.Prelude
|
|||||||
|
|
||||||
import Network.Minio.Data
|
import Network.Minio.Data
|
||||||
import Network.Minio.S3API
|
import Network.Minio.S3API
|
||||||
|
import Network.Minio.Utils
|
||||||
|
|
||||||
fGetObject :: Bucket -> Object -> FilePath -> Minio ()
|
fGetObject :: Bucket -> Object -> FilePath -> Minio ()
|
||||||
fGetObject bucket object fp = do
|
fGetObject bucket object fp = do
|
||||||
@ -40,10 +41,7 @@ fGetObject bucket object fp = do
|
|||||||
|
|
||||||
fPutObject :: Bucket -> Object -> FilePath -> Minio ()
|
fPutObject :: Bucket -> Object -> FilePath -> Minio ()
|
||||||
fPutObject bucket object fp = do
|
fPutObject bucket object fp = do
|
||||||
-- allocate file handle and register cleanup action
|
(releaseKey, h) <- allocateReadFile fp
|
||||||
(releaseKey, h) <- R.allocate
|
|
||||||
(IO.openBinaryFile fp IO.ReadMode)
|
|
||||||
IO.hClose
|
|
||||||
|
|
||||||
size <- liftIO $ IO.hFileSize h
|
size <- liftIO $ IO.hFileSize h
|
||||||
putObject bucket object [] 0 (fromIntegral size) h
|
putObject bucket object [] 0 (fromIntegral size) h
|
||||||
|
|||||||
@ -90,6 +90,7 @@ data MinioErr = MErrMsg ByteString -- generic
|
|||||||
| MErrXml ByteString -- XML parsing/generation errors
|
| MErrXml ByteString -- XML parsing/generation errors
|
||||||
| MErrService ByteString -- error response from service
|
| MErrService ByteString -- error response from service
|
||||||
| MErrValidation MErrV -- client-side validation errors
|
| MErrValidation MErrV -- client-side validation errors
|
||||||
|
| MErrIO IOException -- exceptions while working with files
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
newtype Minio a = Minio {
|
newtype Minio a = Minio {
|
||||||
|
|||||||
17
src/Network/Minio/Utils.hs
Normal file
17
src/Network/Minio/Utils.hs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
module Network.Minio.Utils where
|
||||||
|
|
||||||
|
import qualified Control.Monad.Trans.Resource as R
|
||||||
|
import qualified System.IO as IO
|
||||||
|
|
||||||
|
import Lib.Prelude
|
||||||
|
|
||||||
|
import Network.Minio.Data
|
||||||
|
|
||||||
|
allocateReadFile :: (R.MonadResource m, MonadError MinioErr m)
|
||||||
|
=> FilePath -> m (R.ReleaseKey, Handle)
|
||||||
|
allocateReadFile fp = do
|
||||||
|
(rk, hdlE) <- R.allocate (openReadFile fp) cleanup
|
||||||
|
either (throwError . MErrIO) (return . (rk,)) hdlE
|
||||||
|
where
|
||||||
|
openReadFile f = runExceptT $ tryIO $ IO.openBinaryFile f IO.ReadMode
|
||||||
|
cleanup = either (const $ return ()) IO.hClose
|
||||||
@ -7,7 +7,6 @@ import Text.XML
|
|||||||
import Text.XML.Cursor
|
import Text.XML.Cursor
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Time
|
import Data.Time
|
||||||
import Control.Error
|
|
||||||
|
|
||||||
import Lib.Prelude
|
import Lib.Prelude
|
||||||
|
|
||||||
|
|||||||
13
test/Spec.hs
13
test/Spec.hs
@ -64,10 +64,17 @@ unitTests = testGroup "Unit tests"
|
|||||||
step "Running test.."
|
step "Running test.."
|
||||||
ret <- runResourceT $ runMinio mc $
|
ret <- runResourceT $ runMinio mc $
|
||||||
fPutObject "testbucket" "lsb-release" "/etc/lsb-release"
|
fPutObject "testbucket" "lsb-release" "/etc/lsb-release"
|
||||||
-- h <- SIO.openBinaryFile "/etc/lsb-release" SIO.ReadMode
|
|
||||||
-- ret <- runResourceT $ runMinio mc $
|
|
||||||
-- putObject "testbucket" "lsb-release" [] 0 105 h
|
|
||||||
isRight ret @? ("putObject failure => " ++ show ret)
|
isRight ret @? ("putObject failure => " ++ show ret)
|
||||||
|
|
||||||
|
, testCaseSteps "Simple putObject fails with non-existent file" $ \step -> do
|
||||||
|
step "Preparing..."
|
||||||
|
|
||||||
|
mc <- connect defaultConnectInfo
|
||||||
|
|
||||||
|
step "Running test.."
|
||||||
|
ret <- runResourceT $ runMinio mc $
|
||||||
|
fPutObject "testbucket" "lsb-release" "/etc/lsb-releaseXXX"
|
||||||
|
isLeft ret @? ("putObject unexpected success => " ++ show ret)
|
||||||
|
|
||||||
, testCase "Test mkCreateBucketConfig." testMkCreateBucketConfig
|
, testCase "Test mkCreateBucketConfig." testMkCreateBucketConfig
|
||||||
]
|
]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user