Skip to content

Commit

Permalink
Merge pull request #5519 from xmbhasin/err-msg-cli-http
Browse files Browse the repository at this point in the history
Improve error messages on CLI HTTP request errors
  • Loading branch information
aryairani authored Dec 31, 2024
2 parents aad1fdd + daefc1d commit b7b3439
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ The format for this list: name, GitHub handle
* Brian McKenna (@puffnfresh)
* Ruslan Simchuk (@SimaDovakin)
* Brandon Barker (@bbarker)
* Manish Bhasin (@xmbhasin)
41 changes: 22 additions & 19 deletions unison-cli/src/Unison/CommandLine/OutputMessages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ import Unison.Term (Term)
import Unison.Term qualified as Term
import Unison.Type (Type)
import Unison.UnisonFile qualified as UF
import Unison.Util.ColorText qualified
import Unison.Util.Conflicted (Conflicted (..))
import Unison.Util.Defn (Defn (..))
import Unison.Util.Defns (Defns (..))
Expand Down Expand Up @@ -971,7 +972,6 @@ notifyUser dir = \case
-- defs in the codebase. In some cases it's fine for bindings to
-- shadow codebase names, but you don't want it to capture them in
-- the decompiled output.

let prettyBindings =
P.bracket . P.lines $
P.wrap "The watch expression(s) reference these definitions:"
Expand Down Expand Up @@ -1771,16 +1771,16 @@ notifyUser dir = \case
<> P.newline
<> P.indentN 2 (P.pshown response)
Servant.FailureResponse request response ->
P.wrap "Oops, I received an unexpected status code from the server."
unexpectedServerResponse response
<> P.newline
<> P.newline
<> P.wrap "Here is the request."
<> P.wrap "Here is the request:"
<> P.newline
<> P.newline
<> P.indentN 2 (P.pshown request)
<> P.newline
<> P.newline
<> P.wrap "Here is the full response."
<> P.wrap "Here is the full response:"
<> P.newline
<> P.newline
<> P.indentN 2 (P.pshown response)
Expand Down Expand Up @@ -2362,21 +2362,24 @@ prettyTransportError = \case
Share.RateLimitExceeded -> "Rate limit exceeded, please try again later."
Share.Timeout -> "The code server timed-out when responding to your request. Please try again later or report an issue if the problem persists."
Share.UnexpectedResponse resp ->
(P.lines . catMaybes)
[ Just
( "The server sent a "
<> P.red (P.shown (Http.statusCode (Servant.responseStatusCode resp)))
<> " that we didn't expect."
),
let body = Text.decodeUtf8 (LazyByteString.toStrict (Servant.responseBody resp))
in if Text.null body then Nothing else Just (P.newline <> "Response body: " <> P.text body),
responseRequestId resp <&> \responseId -> P.newline <> "Request ID: " <> P.blue (P.text responseId)
]
where
-- Dig the request id out of a response header.
responseRequestId :: Servant.Response -> Maybe Text
responseRequestId =
fmap Text.decodeUtf8 . List.lookup "X-RequestId" . Foldable.toList @Seq . Servant.responseHeaders
unexpectedServerResponse resp

unexpectedServerResponse :: Servant.ResponseF LazyByteString.ByteString -> P.Pretty Unison.Util.ColorText.ColorText
unexpectedServerResponse resp =
(P.lines . catMaybes)
[ Just
( "I received an unexpected status code from the server: "
<> P.red (P.shown (Http.statusCode (Servant.responseStatusCode resp)))
),
let body = Text.decodeUtf8 (LazyByteString.toStrict (Servant.responseBody resp))
in if Text.null body then Nothing else Just (P.newline <> "Response body: " <> P.text body),
responseRequestId resp <&> \responseId -> P.newline <> "Request ID: " <> P.blue (P.text responseId)
]

-- | Dig the request id out of a response header.
responseRequestId :: Servant.Response -> Maybe Text
responseRequestId =
fmap Text.decodeUtf8 . List.lookup "X-RequestId" . Foldable.toList @Seq . Servant.responseHeaders

prettyEntityType :: Share.EntityType -> Pretty
prettyEntityType = \case
Expand Down

0 comments on commit b7b3439

Please sign in to comment.