From 2ff873b070961aed97ba30649e2f242a636fdcf3 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Sat, 5 Nov 2022 14:50:34 -0400 Subject: [PATCH] Handle HTTP status in index lookup --- src/Main.purs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Main.purs b/src/Main.purs index 5893f6e..b6aed32 100644 --- a/src/Main.purs +++ b/src/Main.purs @@ -53,6 +53,7 @@ data AviaryError | StrangeIndex | IndexMissingField String | ImageNotFound + | IndexNotFound type Image = { key :: Aff CryptoKey @@ -77,6 +78,7 @@ instance showAviaryError :: Show AviaryError where show StrangeIndex = "The gallery index matched with the provided key, but was in a strange format. This could indicate a severe version mismatch, or that the gallery was created by a malfunctioning client." show (IndexMissingField f) = "The gallery index was missing the crucial field " <> f <> ". This is almost certainly the result of a version mismatch or that the creator's client was not fully up-to-spec." show ImageNotFound = "The image was not found, likely indicating that it expired" + show IndexNotFound = "The index was not found, likely indicating that the gallery never existed or expired" show (UnexpectedStatusCode status) = "Server returned an unexpected status code: " <> (show status) type UrlInfo = @@ -173,14 +175,18 @@ main_aff = do Right keyBuffer -> do cryptoKey <- importKey keyBuffer { status, arrayBuffer } <- fetch (urlInfo.server <> "/" <> urlInfo.fileId <> ".bin") {} - encryptedIndex <- arrayBuffer - serializedIndex <- try $ decryptBlob cryptoKey encryptedIndex urlInfo.nonce - case serializedIndex of - Left err -> do - _ <- liftEffect $ log $ message err - pure $ Left $ DecryptFailed - Right serializedIndex' -> do - liftEffect $ parseIndex serializedIndex' + case status of + 200 -> do + encryptedIndex <- arrayBuffer + serializedIndex <- try $ decryptBlob cryptoKey encryptedIndex urlInfo.nonce + case serializedIndex of + Left err -> do + _ <- liftEffect $ log $ message err + pure $ Left $ DecryptFailed + Right serializedIndex' -> do + liftEffect $ parseIndex serializedIndex' + 404 -> pure $ Left ImageNotFound + s -> pure $ Left $ if s / 100 == 5 then ServerError else UnexpectedStatusCode s maybeUrls <- case maybeIndex of Left err -> pure $ Left err Right index -> Right <$> parTraverse (fetchImageAsBlobUrl urlInfo.nonce) index.images