diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-03-02 21:20:09 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-03-02 21:20:09 +0100 |
| commit | 8908b8ae93d4bd29db149a0a0eb5886d09ac5c3f (patch) | |
| tree | 33b4bca703b0bdf490d889c8839936e5a44beec3 /src/SlackBuilder | |
| parent | 1d81fea1a330a5932f1a1dfae06c89a6e37d69c2 (diff) | |
| download | slackbuilder-8908b8ae93d4bd29db149a0a0eb5886d09ac5c3f.tar.gz | |
Add a printer for HttpExceptionContent
Diffstat (limited to 'src/SlackBuilder')
| -rw-r--r-- | src/SlackBuilder/Update.hs | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/src/SlackBuilder/Update.hs b/src/SlackBuilder/Update.hs index 7987916..8998bec 100644 --- a/src/SlackBuilder/Update.hs +++ b/src/SlackBuilder/Update.hs @@ -18,6 +18,7 @@ import Control.Exception (Exception(..), SomeException(..)) import Control.Monad.Catch (MonadCatch(..), catches, Handler(..)) import Control.Monad.IO.Class (MonadIO(..)) import Control.Monad.Trans.Reader (asks) +import qualified Data.ByteString.Char8 as Char8 import Data.Foldable (Foldable(..), find) import Data.HashMap.Strict (HashMap) import qualified Data.HashMap.Strict as HashMap @@ -28,7 +29,7 @@ import qualified Data.Text as Text import qualified Data.Text.IO as Text.IO import GHC.Records (HasField(..)) import qualified Network.HTTP.Req as Req -import Network.HTTP.Client (HttpException(..)) +import Network.HTTP.Client (HttpException(..), HttpExceptionContent(..), responseStatus) import System.FilePath ( (</>) , (<.>) @@ -71,6 +72,7 @@ import Control.Monad (filterM, void) import Data.List (isPrefixOf, isSuffixOf, partition) import Data.Functor ((<&>)) import Data.Bifunctor (Bifunctor(..)) +import Network.HTTP.Types (Status(..)) getAndLogLatest :: PackageDescription -> SlackBuilderT (Maybe PackageUpdateData) getAndLogLatest description = do @@ -278,11 +280,54 @@ handleExceptions action = catches (void action) = liftIO (setSGR [SetColor Foreground Dull Red]) >> liftIO (putStrLn e) >> liftIO (setSGR [Reset]) + showStatus (Status code message) = + Char8.pack (show code) <> " \"" <> message <> "\"" + showHttpExceptionContent (StatusCodeException response _) = Char8.unpack + $ "The server returned " + <> showStatus (responseStatus response) + <> " response status code." + showHttpExceptionContent (TooManyRedirects _) = + "The server responded with too many redirects for a request." + showHttpExceptionContent OverlongHeaders = "Too many total bytes in the HTTP header were returned by the server." + showHttpExceptionContent TooManyHeaderFields = "Too many HTTP header fields were returned by the server." + showHttpExceptionContent ResponseTimeout = "The server took too long to return a response." + showHttpExceptionContent ConnectionTimeout = "Attempting to connect to the server timed out" + showHttpExceptionContent (ConnectionFailure connectionException) = displayException connectionException + showHttpExceptionContent (InvalidStatusLine statusLine) = Char8.unpack + $ "The status line returned by the server could not be parsed: " + <> statusLine <> "." + showHttpExceptionContent (InvalidHeader headerLine) = Char8.unpack + $ "The given response header line could not be parsed: " + <> headerLine <> "." + showHttpExceptionContent (InvalidRequestHeader headerLine) = Char8.unpack + $ "The given request header is not compliant: " + <> headerLine <> "." + showHttpExceptionContent (InternalException interalException) = displayException interalException + showHttpExceptionContent (ProxyConnectException _ _ status) = Char8.unpack + $ showStatus status + <> " status code was returned when trying to connect to the proxy server on the given host and port." + showHttpExceptionContent NoResponseDataReceived = "No response data was received from the server at all." + showHttpExceptionContent TlsNotSupported = "This HTTP client does not have support for secure connections." + showHttpExceptionContent (WrongRequestBodyStreamSize _ _) + = "The request body provided did not match the expected size." + showHttpExceptionContent (ResponseBodyTooShort _ _) = + "The returned response body is too short. Provides the expected size and actual size." + showHttpExceptionContent InvalidChunkHeaders = "A chunked response body had invalid headers." + showHttpExceptionContent IncompleteHeaders = "An incomplete set of response headers were returned." + showHttpExceptionContent (InvalidDestinationHost hostLine) = Char8.unpack + $ "The host we tried to connect to is invalid" + <> hostLine <> "." + showHttpExceptionContent (HttpZlibException zlibException) = displayException zlibException + showHttpExceptionContent (InvalidProxyEnvironmentVariable environmentName environmentValue) = Text.unpack + $ "Values in the proxy environment variable were invalid: " + <> environmentName <> "=\"" <> environmentValue <> "\"." + showHttpExceptionContent ConnectionClosed = "Attempted to use a Connection which was already closed" + showHttpExceptionContent (InvalidProxySettings _) = "Proxy settings are not valid." handleHttp :: (MonadIO m, MonadCatch m) => Req.HttpException -> m () handleHttp (Req.VanillaHttpException e) | HttpExceptionRequest _ exceptionContent <- e = printException - $ show exceptionContent + $ showHttpExceptionContent exceptionContent | InvalidUrlException url reason <- e = printException $ url <> ": " <> reason handleHttp (Req.JsonHttpException e) = printException e handleSome :: (MonadIO m, MonadCatch m) => SomeException -> m () - handleSome = printException . displayException + handleSome = printException . show |
