mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-25 12:52:27 +03:00
Include content type in StringBody. Include more context in bad http requests.
This commit is contained in:
parent
3ce0df4526
commit
3cdbe8b0a3
31
src/Pages/Http.elm
Normal file
31
src/Pages/Http.elm
Normal file
@ -0,0 +1,31 @@
|
||||
module Pages.Http exposing (..)
|
||||
|
||||
import Http
|
||||
|
||||
|
||||
type Error
|
||||
= BadUrl String
|
||||
| Timeout
|
||||
| NetworkError
|
||||
| BadStatus Http.Metadata String
|
||||
|
||||
|
||||
expectString : (Result Error String -> msg) -> Http.Expect msg
|
||||
expectString toMsg =
|
||||
Http.expectStringResponse toMsg <|
|
||||
\response ->
|
||||
case response of
|
||||
Http.BadUrl_ url ->
|
||||
Err (BadUrl url)
|
||||
|
||||
Http.Timeout_ ->
|
||||
Err Timeout
|
||||
|
||||
Http.NetworkError_ ->
|
||||
Err NetworkError
|
||||
|
||||
Http.BadStatus_ metadata body ->
|
||||
Err (BadStatus metadata body)
|
||||
|
||||
Http.GoodStatus_ metadata body ->
|
||||
Ok body
|
@ -24,6 +24,7 @@ import Json.Decode as Decode
|
||||
import Json.Encode
|
||||
import Pages.ContentCache as ContentCache exposing (ContentCache)
|
||||
import Pages.Document
|
||||
import Pages.Http
|
||||
import Pages.ImagePath as ImagePath
|
||||
import Pages.Internal.StaticHttpBody as StaticHttpBody
|
||||
import Pages.Manifest as Manifest
|
||||
@ -150,7 +151,7 @@ type alias Model =
|
||||
|
||||
|
||||
type Msg
|
||||
= GotStaticHttpResponse { request : { masked : RequestDetails, unmasked : RequestDetails }, response : Result Http.Error String }
|
||||
= GotStaticHttpResponse { request : { masked : RequestDetails, unmasked : RequestDetails }, response : Result Pages.Http.Error String }
|
||||
|
||||
|
||||
type alias Config pathKey userMsg userModel metadata view =
|
||||
@ -287,13 +288,13 @@ perform cliMsgConstructor toJsPort effect =
|
||||
StaticHttpBody.EmptyBody ->
|
||||
Http.emptyBody
|
||||
|
||||
StaticHttpBody.StringBody string ->
|
||||
Http.stringBody string
|
||||
StaticHttpBody.StringBody contentType string ->
|
||||
Http.stringBody contentType string
|
||||
|
||||
StaticHttpBody.JsonBody value ->
|
||||
Http.jsonBody value
|
||||
, expect =
|
||||
Http.expectString
|
||||
Pages.Http.expectString
|
||||
(\response ->
|
||||
(GotStaticHttpResponse >> cliMsgConstructor)
|
||||
{ request = requests
|
||||
@ -473,21 +474,23 @@ update siteMetadata config msg model =
|
||||
, Terminal.yellow <| Terminal.text request.masked.url
|
||||
, Terminal.text "\n\n"
|
||||
, case error of
|
||||
Http.BadStatus code ->
|
||||
Terminal.text <| "Bad status: " ++ String.fromInt code
|
||||
Pages.Http.BadStatus metadata body ->
|
||||
Terminal.text <|
|
||||
String.join "\n"
|
||||
[ "Bad status: " ++ String.fromInt metadata.statusCode
|
||||
, "Status message: " ++ metadata.statusText
|
||||
, "Body: " ++ body
|
||||
]
|
||||
|
||||
Http.BadUrl _ ->
|
||||
Pages.Http.BadUrl _ ->
|
||||
-- TODO include HTTP method, headers, and body
|
||||
Terminal.text <| "Invalid url: " ++ request.masked.url
|
||||
|
||||
Http.Timeout ->
|
||||
Pages.Http.Timeout ->
|
||||
Terminal.text "Timeout"
|
||||
|
||||
Http.NetworkError ->
|
||||
Pages.Http.NetworkError ->
|
||||
Terminal.text "Network error"
|
||||
|
||||
Http.BadBody string ->
|
||||
Terminal.text "Unable to parse HTTP response body"
|
||||
]
|
||||
, fatal = True
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import Json.Encode as Encode
|
||||
|
||||
type Body
|
||||
= EmptyBody
|
||||
| StringBody String
|
||||
| StringBody String String
|
||||
| JsonBody Encode.Value
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ encode body =
|
||||
EmptyBody ->
|
||||
encodeWithType "empty" []
|
||||
|
||||
StringBody content ->
|
||||
StringBody contentType content ->
|
||||
encodeWithType "string"
|
||||
[ ( "content", Encode.string content )
|
||||
]
|
||||
|
@ -95,9 +95,9 @@ emptyBody =
|
||||
|
||||
{-| Builds a string body for a StaticHttp request. See [elm/http's `Http.stringBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#stringBody).
|
||||
-}
|
||||
stringBody : String -> Body
|
||||
stringBody content =
|
||||
Body.StringBody content
|
||||
stringBody : String -> String -> Body
|
||||
stringBody contentType content =
|
||||
Body.StringBody contentType content
|
||||
|
||||
|
||||
{-| Builds a JSON body for a StaticHttp request. See [elm/http's `Http.jsonBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#jsonBody).
|
||||
|
26
tests/PagesHttp.elm
Normal file
26
tests/PagesHttp.elm
Normal file
@ -0,0 +1,26 @@
|
||||
module PagesHttp exposing (..)
|
||||
|
||||
import Http as H exposing (Response(..))
|
||||
import Pages.Http exposing (..)
|
||||
import SimulatedEffect.Http as Http
|
||||
|
||||
|
||||
expectString : (Result Pages.Http.Error String -> msg) -> Http.Expect msg
|
||||
expectString toMsg =
|
||||
Http.expectStringResponse toMsg <|
|
||||
\response ->
|
||||
case response of
|
||||
BadUrl_ url ->
|
||||
Err (BadUrl url)
|
||||
|
||||
Timeout_ ->
|
||||
Err Timeout
|
||||
|
||||
NetworkError_ ->
|
||||
Err NetworkError
|
||||
|
||||
BadStatus_ metadata body ->
|
||||
Err (BadStatus metadata body)
|
||||
|
||||
GoodStatus_ metadata body ->
|
||||
Ok body
|
@ -8,12 +8,15 @@ import Json.Decode as JD
|
||||
import Json.Decode.Exploration as Decode exposing (Decoder)
|
||||
import Pages.ContentCache as ContentCache
|
||||
import Pages.Document as Document
|
||||
import Pages.Http
|
||||
import Pages.ImagePath as ImagePath
|
||||
import Pages.Internal.Platform.Cli as Main exposing (..)
|
||||
import Pages.Internal.StaticHttpBody as StaticHttpBody
|
||||
import Pages.Manifest as Manifest
|
||||
import Pages.PagePath as PagePath
|
||||
import Pages.StaticHttp as StaticHttp
|
||||
import Pages.StaticHttp.Request as Request
|
||||
import PagesHttp
|
||||
import ProgramTest exposing (ProgramTest)
|
||||
import Regex
|
||||
import Secrets
|
||||
@ -550,7 +553,9 @@ So maybe MISSING should be API_KEY"""
|
||||
|
||||
I got an error making an HTTP request to this URL: https://api.github.com/repos/dillonkearns/elm-pages
|
||||
|
||||
Bad status: 404""")
|
||||
Bad status: 404
|
||||
Status message: TODO: if you need this, please report to https://github.com/avh4/elm-program-test/issues
|
||||
Body: """)
|
||||
, test "uses real secrets to perform request and masked secrets to store and lookup response" <|
|
||||
\() ->
|
||||
start
|
||||
@ -713,11 +718,20 @@ simulateEffects effect =
|
||||
FetchHttp ({ unmasked, masked } as requests) ->
|
||||
Http.request
|
||||
{ method = unmasked.method
|
||||
, url = unmasked.url -- |> Debug.log "FETCHING"
|
||||
, url = unmasked.url
|
||||
, headers = unmasked.headers |> List.map (\( key, value ) -> Http.header key value)
|
||||
, body = Http.emptyBody
|
||||
, body =
|
||||
case unmasked.body of
|
||||
StaticHttpBody.EmptyBody ->
|
||||
Http.emptyBody
|
||||
|
||||
StaticHttpBody.StringBody contentType string ->
|
||||
Http.stringBody contentType string
|
||||
|
||||
StaticHttpBody.JsonBody value ->
|
||||
Http.jsonBody value
|
||||
, expect =
|
||||
Http.expectString
|
||||
PagesHttp.expectString
|
||||
(\response ->
|
||||
GotStaticHttpResponse
|
||||
{ request = requests
|
||||
|
Loading…
Reference in New Issue
Block a user