Skip to content

Commit

Permalink
Handle error objects properly
Browse files Browse the repository at this point in the history
* Handle top-level error objects
* Do not ignore error objects in the results object (fixes #66)
  • Loading branch information
Mitsutoshi Aoe authored and maoe committed Mar 26, 2019
1 parent abb4e21 commit f11e33d
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Database/InfluxDB/JSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ module Database.InfluxDB.JSON
import Control.Applicative
import Control.Exception
import Control.Monad
import Data.Foldable
import Data.Maybe
import Prelude

import Data.Aeson
import Data.HashMap.Strict (HashMap)
Expand Down Expand Up @@ -81,13 +83,26 @@ parseResultsWithDecoder
-- to construct a value.
-> Value
-> A.Parser (Vector a)
parseResultsWithDecoder Decoder {..} row val0 = success
parseResultsWithDecoder Decoder {..} row val0 = do
r <- foldr1 (<|>)
[ Left <$> parseErrorObject val0
, Right <$> success
]
case r of
Left err -> fail err
Right vec -> return vec
where
success = do
results <- parseResultsObject val0

(join -> series) <- V.forM results $ \val ->
parseSeriesObject val <|> parseErrorObject val
(join -> series) <- V.forM results $ \val -> do
r <- foldr1 (<|>)
[ Left <$> parseErrorObject val
, Right <$> parseSeriesObject val
]
case r of
Left err -> fail err
Right vec -> return vec
values <- V.forM series $ \val -> do
(name, tags, columns, values) <- parseSeriesBody val
decodeFold $ V.forM values $ A.withArray "values" $ \fields -> do
Expand Down Expand Up @@ -165,10 +180,8 @@ parseSeriesBody = A.withObject "series" $ \obj -> do
return (name, tags, columns, values)

-- | Parse the common JSON structure used in failure response.
parseErrorObject :: A.Value -> A.Parser a
parseErrorObject = A.withObject "error" $ \obj -> do
message <- obj .: "error"
fail $ T.unpack message
parseErrorObject :: A.Value -> A.Parser String
parseErrorObject = A.withObject "error" $ \obj -> obj .: "error"

-- | Parse either a POSIX timestamp or RFC3339 formatted timestamp as 'UTCTime'.
parseUTCTime :: Precision ty -> A.Value -> A.Parser UTCTime
Expand Down

0 comments on commit f11e33d

Please sign in to comment.