Skip to content

Commit

Permalink
Better error message when nix-prefetch-url return an error 404
Browse files Browse the repository at this point in the history
  • Loading branch information
refnil committed Sep 26, 2021
1 parent 8d37a17 commit a2b1e4e
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Niv/GitHub/Cmd.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Niv.GitHub.Cmd
where

import Control.Applicative
import Control.Monad
import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
import Data.Bifunctor
Expand Down Expand Up @@ -160,7 +161,10 @@ nixPrefetchURL unpack packageName (T.unpack -> url) = do
(exitCode, sout, serr) <- runNixPrefetch
case (exitCode, lines sout) of
(ExitSuccess, l : _) -> pure $ T.pack l
_ -> abortNixPrefetchExpectedOutput (T.pack <$> args) (T.pack sout) (T.pack serr)
_ -> do
let tserr = T.pack serr
checkNixPrefetchUrlNotFound (T.pack url) tserr
abortNixPrefetchExpectedOutput (T.pack <$> args) (T.pack sout) tserr
where
args = (if unpack then ["--unpack"] else []) <> [url, "--name", sanitizeName basename <> "-src"]
runNixPrefetch = readProcessWithExitCode "nix-prefetch-url" args ""
Expand All @@ -172,6 +176,16 @@ nixPrefetchURL unpack packageName (T.unpack -> url) = do
-- (note: we assume they don't begin with a period)
isOk = \c -> isAlphaNum c || T.any (c ==) "+-._?="

checkNixPrefetchUrlNotFound :: T.Text -> T.Text -> IO ()
checkNixPrefetchUrlNotFound url serr = do
guard ("HTTP error 404" `T.isInfixOf` serr)
abort $ T.unlines
[ "The url '" <> url <> "' was not found by 'nix-prefetch-url'."
, "Changing your source's template or attributes may help you resolve the situation."
, "For further help, you can read the niv readme at https://github.com/nmattia/niv"
, "or open an ticket at https://github.com/nmattia/niv/issues/new."
]

abortNixPrefetchExpectedOutput :: [T.Text] -> T.Text -> T.Text -> IO a
abortNixPrefetchExpectedOutput args sout serr =
abort $
Expand Down

0 comments on commit a2b1e4e

Please sign in to comment.