Implement fetchAPIDOrH using a dedicated error type

This commit is contained in:
fr33domlover 2019-04-16 16:10:17 +00:00
parent 25fcceabde
commit 7c2fad7417

View file

@ -709,6 +709,33 @@ fetchAPID m getId h lu = first showError <$> fetchAPID' m getId h lu
showError Nothing = "Object @id doesn't match the URI we fetched" showError Nothing = "Object @id doesn't match the URI we fetched"
showError (Just e) = displayException e showError (Just e) = displayException e
data FetchAPError
= FetchAPErrorGet APGetError
-- Object @id doesn't match the URI we fetched
| FetchAPErrorIdMismatch
-- Object @id URI's host doesn't match the URI we fetched
| FetchAPErrorHostMismatch
deriving Show
fetchAPIDOrH'
:: (MonadIO m, ActivityPub a, ActivityPub b)
=> Manager
-> (a -> LocalURI)
-> Text
-> LocalURI
-> ExceptT FetchAPError m (Either a b)
fetchAPIDOrH' m getId h lu = do
e <- withExceptT FetchAPErrorGet $ fetchAP' m $ l2f h lu
case e of
Left' (Doc h' x) ->
if h == h' && getId x == lu
then return $ Left x
else throwE FetchAPErrorIdMismatch
Right' (Doc h' y) ->
if h == h'
then return $ Right y
else throwE FetchAPErrorHostMismatch
fetchAPIDOrH fetchAPIDOrH
:: (MonadIO m, ActivityPub a, ActivityPub b) :: (MonadIO m, ActivityPub a, ActivityPub b)
=> Manager => Manager
@ -716,17 +743,7 @@ fetchAPIDOrH
-> Text -> Text
-> LocalURI -> LocalURI
-> ExceptT String m (Either a b) -> ExceptT String m (Either a b)
fetchAPIDOrH m getId h lu = do fetchAPIDOrH m getId h lu = withExceptT show $ fetchAPIDOrH' m getId h lu
e <- fetchAP m $ l2f h lu
case e of
Left' (Doc h' x) ->
if h == h' && getId x == lu
then return $ Left x
else throwE "Object @id doesn't match the URI we fetched"
Right' (Doc h' y) ->
if h == h'
then return $ Right y
else throwE "Object @id URI's host doesn't match the URI we fetched"
-- | Fetches the given actor and checks whether it lists the given key (as a -- | Fetches the given actor and checks whether it lists the given key (as a
-- URI, not as an embedded object). If it does, returns 'Right' the fetched -- URI, not as an embedded object). If it does, returns 'Right' the fetched