git.iko.soy/typeable/bloodhound
on master help sign in
Made EsProtocolException more helpful
commitc18318de560134ad94d1cd21e3dd8091b8aba36f authorChris Allen <cma@bitemyapp.com> dateSat 5 May 2018, 15:54 (8 years ago) parent9024fcb
3 files changed+26−18
src/Bloodhound/Import.hs+3 src/Database/V5/Bloodhound/Client.hs+20−16 src/Database/V5/Bloodhound/Internal/Client.hs+3−2
src/Bloodhound/Import.hs+3
@@ -79,3 +79,6 @@ deleteSeveral ks hm = foldr HM.delete hm ks
7979oPath :: ToJSON a => NonEmpty Text -> a -> Value
8080oPath (k :| []) v = object [k .= v]
8181oPath (k:| (h:t)) v = object [k .= oPath (h :| t) v]
82+
83+tshow :: Show a => a -> Text
84+tshow = T.pack . show
src/Database/V5/Bloodhound/Client.hs+20−16
@@ -639,19 +639,24 @@ existentialQuery url = do
639639-- categories. If they don't, a 'EsProtocolException' will be
640640-- thrown. If you encounter this, please report the full body it
641641-- reports along with your Elasticsearch verison.
642−parseEsResponse :: (MonadThrow m, FromJSON a) => Reply
642+parseEsResponse :: ( MonadThrow m
643+ , FromJSON a
644+ )
645+ => Reply
643646 -> m (Either EsError a)
644647parseEsResponse reply
645648 | respIsTwoHunna reply = case eitherDecode body of
646649 Right a -> return (Right a)
647− Left _ -> tryParseError
648− | otherwise = tryParseError
650+ Left err ->
651+ tryParseError err
652+ | otherwise = tryParseError "Non-200 status code"
649653 where body = responseBody reply
650− tryParseError = case eitherDecode body of
651− Right e -> return (Left e)
652− -- this case should not be possible
653− Left _ -> explode
654− explode = throwM (EsProtocolException body)
654+ tryParseError originalError
655+ = case eitherDecode body of
656+ Right e -> return (Left e)
657+ -- Failed to parse the error message.
658+ Left err -> explode ("Original error was: " <> originalError <> " Error parse failure was: " <> err)
659+ explode errorMsg = throwM (EsProtocolException (T.pack errorMsg) body)
655660
656661-- | 'indexExists' enables you to check if an index exists. Returns 'Bool'
657662-- in IO
@@ -711,16 +716,15 @@ listIndices =
711716 parse . responseBody =<< get =<< url
712717 where
713718 url = joinPath ["_cat/indices?format=json"]
714− parse body = maybe (throwM (EsProtocolException body)) return $ do
715− vals <- decode body
719+ parse body = either (\msg -> (throwM (EsProtocolException (T.pack msg) body))) return $ do
720+ vals <- eitherDecode body
716721 forM vals $ \val ->
717722 case val of
718− Object obj -> do
719− indexVal <- HM.lookup "index" obj
720− case indexVal of
721− String txt -> Just (IndexName txt)
722− _ -> Nothing
723− _ -> Nothing
723+ Object obj ->
724+ case HM.lookup "index" obj of
725+ (Just (String txt)) -> Right (IndexName txt)
726+ v -> Left $ "indexVal in listIndices failed on non-string, was: " <> show v
727+ v -> Left $ "One of the values parsed in listIndices wasn't an object, it was: " <> show v
724728
725729-- | 'updateIndexAliases' updates the server's index alias
726730-- table. Operations are atomic. Explained in further detail at
src/Database/V5/Bloodhound/Internal/Client.hs+3−2
@@ -693,8 +693,9 @@ sure that your mappings are correct, then this error may be an indication of an
693693incompatibility between Bloodhound and Elasticsearch. Please open a bug report
694694and be sure to include the exception body.
695695-}
696−newtype EsProtocolException = EsProtocolException
697− { esProtoExBody :: LByteString
696+data EsProtocolException = EsProtocolException
697+ { esProtoExMessage :: !Text
698+ , esProtoExBody :: !LByteString
698699 } deriving (Eq, Show)
699700
700701instance Exception EsProtocolException