sends 404s if we can't find the package rather than just blowing up and going 500

This commit is contained in:
Keagan McClelland
2021-11-23 14:09:11 -07:00
parent 2a3069818f
commit c6acbd383c
2 changed files with 12 additions and 5 deletions

View File

@@ -75,7 +75,9 @@ getAppR file = do
`orThrow` sendResponseStatus status404 (NotFoundE [i|#{pkg} satisfying #{versionSpec}|]) `orThrow` sendResponseStatus status404 (NotFoundE [i|#{pkg} satisfying #{versionSpec}|])
addPackageHeader pkg version addPackageHeader pkg version
void $ recordMetrics pkg version void $ recordMetrics pkg version
(len, src) <- getPackage pkg version (len, src) <- getPackage pkg version >>= \case
Nothing -> sendResponseStatus status404 ([i|#{pkg}@#{version} not found|] :: Text)
Just a -> pure a
addHeader "Content-Length" (show len) addHeader "Content-Length" (show len)
respondSource typeOctet $ src .| awaitForever sendChunkBS respondSource typeOctet $ src .| awaitForever sendChunkBS

View File

@@ -96,7 +96,8 @@ import UnliftIO ( MonadUnliftIO
) )
import UnliftIO ( tryPutMVar ) import UnliftIO ( tryPutMVar )
import UnliftIO.Concurrent ( forkIO ) import UnliftIO.Concurrent ( forkIO )
import UnliftIO.Directory ( getFileSize import UnliftIO.Directory ( doesPathExist
, getFileSize
, listDirectory , listDirectory
, removeFile , removeFile
, renameFile , renameFile
@@ -257,9 +258,13 @@ getHash pkg version = do
getPackage :: (MonadResource m, MonadReader r m, Has PkgRepo r) getPackage :: (MonadResource m, MonadReader r m, Has PkgRepo r)
=> PkgId => PkgId
-> Version -> Version
-> m (Integer, ConduitT () ByteString m ()) -> m (Maybe (Integer, ConduitT () ByteString m ()))
getPackage pkg version = do getPackage pkg version = do
root <- asks pkgRepoFileRoot root <- asks pkgRepoFileRoot
let pkgPath = root </> show pkg </> show version </> show pkg <.> "s9pk" let pkgPath = root </> show pkg </> show version </> show pkg <.> "s9pk"
n <- getFileSize pkgPath found <- doesPathExist pkgPath
pure (n, sourceFile pkgPath) if found
then do
n <- getFileSize pkgPath
pure . Just $ (n, sourceFile pkgPath)
else pure Nothing