@@ -34,7 +34,9 @@ module Database.InfluxDB.JSON
34
34
import Control.Applicative
35
35
import Control.Exception
36
36
import Control.Monad
37
+ import Data.Foldable
37
38
import Data.Maybe
39
+ import Prelude
38
40
39
41
import Data.Aeson
40
42
import Data.HashMap.Strict (HashMap )
@@ -81,13 +83,26 @@ parseResultsWithDecoder
81
83
-- to construct a value.
82
84
-> Value
83
85
-> A. Parser (Vector a )
84
- parseResultsWithDecoder Decoder {.. } row val0 = success
86
+ parseResultsWithDecoder Decoder {.. } row val0 = do
87
+ r <- foldr1 (<|>)
88
+ [ Left <$> parseErrorObject val0
89
+ , Right <$> success
90
+ ]
91
+ case r of
92
+ Left err -> fail err
93
+ Right vec -> return vec
85
94
where
86
95
success = do
87
96
results <- parseResultsObject val0
88
97
89
- (join -> series) <- V. forM results $ \ val ->
90
- parseSeriesObject val <|> parseErrorObject val
98
+ (join -> series) <- V. forM results $ \ val -> do
99
+ r <- foldr1 (<|>)
100
+ [ Left <$> parseErrorObject val
101
+ , Right <$> parseSeriesObject val
102
+ ]
103
+ case r of
104
+ Left err -> fail err
105
+ Right vec -> return vec
91
106
values <- V. forM series $ \ val -> do
92
107
(name, tags, columns, values) <- parseSeriesBody val
93
108
decodeFold $ V. forM values $ A. withArray " values" $ \ fields -> do
@@ -165,10 +180,8 @@ parseSeriesBody = A.withObject "series" $ \obj -> do
165
180
return (name, tags, columns, values)
166
181
167
182
-- | Parse the common JSON structure used in failure response.
168
- parseErrorObject :: A. Value -> A. Parser a
169
- parseErrorObject = A. withObject " error" $ \ obj -> do
170
- message <- obj .: " error"
171
- fail $ T. unpack message
183
+ parseErrorObject :: A. Value -> A. Parser String
184
+ parseErrorObject = A. withObject " error" $ \ obj -> obj .: " error"
172
185
173
186
-- | Parse either a POSIX timestamp or RFC3339 formatted timestamp as 'UTCTime'.
174
187
parseUTCTime :: Precision ty -> A. Value -> A. Parser UTCTime
0 commit comments