Skip to content

Commit

Permalink
Fix bug where dev server didn't perform some andThen http requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonkearns committed Jun 23, 2021
1 parent 0e53560 commit aa0381c
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 58 deletions.
3 changes: 3 additions & 0 deletions examples/docs/src/Page/Index.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ module Page.Index exposing (Data, Model, Msg, page)

import Css
import DataSource exposing (DataSource)
import DataSource.Http
import Head
import Head.Seo as Seo
import Html.Styled exposing (..)
import Html.Styled.Attributes as Attr exposing (css)
import Link
import OptimizedDecoder
import Page exposing (Page, StaticPayload)
import Pages.PageUrl exposing (PageUrl)
import Pages.Url
import Path
import Route exposing (Route)
import Secrets
import Shared
import SiteOld
import Svg.Styled exposing (path, svg)
Expand Down
4 changes: 1 addition & 3 deletions src/DataSource.elm
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ distill uniqueKey encode decode dataSource =
|> Result.map (Tuple.pair Dict.empty)

Nothing ->
("distill://" ++ uniqueKey)
|> Pages.StaticHttpRequest.MissingHttpResponse
|> Err
Err (Pages.StaticHttpRequest.MissingHttpResponse ("distill://" ++ uniqueKey) [])
)
|> toResult

Expand Down
33 changes: 17 additions & 16 deletions src/DataSource/Http.elm
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,11 @@ unoptimizedRequest requestWithSecrets expect =
)

Nothing ->
Secrets.maskedLookup requestWithSecrets
|> requestToString
|> Pages.StaticHttpRequest.MissingHttpResponse
|> Err
Err
(Pages.StaticHttpRequest.MissingHttpResponse
(requestToString (Secrets.maskedLookup requestWithSecrets))
[ requestWithSecrets ]
)
)
|> Result.andThen
(\( strippedResponses, rawResponse ) ->
Expand Down Expand Up @@ -331,10 +332,10 @@ unoptimizedRequest requestWithSecrets expect =
)

Nothing ->
Secrets.maskedLookup requestWithSecrets
|> requestToString
|> Pages.StaticHttpRequest.MissingHttpResponse
|> Err
Err
(Pages.StaticHttpRequest.MissingHttpResponse (requestToString (Secrets.maskedLookup requestWithSecrets))
[ requestWithSecrets ]
)
)
|> Result.andThen
(\( strippedResponses, rawResponse ) ->
Expand Down Expand Up @@ -382,10 +383,10 @@ unoptimizedRequest requestWithSecrets expect =
)

Nothing ->
Secrets.maskedLookup requestWithSecrets
|> requestToString
|> Pages.StaticHttpRequest.MissingHttpResponse
|> Err
Err
(Pages.StaticHttpRequest.MissingHttpResponse (requestToString (Secrets.maskedLookup requestWithSecrets))
[ requestWithSecrets ]
)
)
|> Result.andThen
(\( strippedResponses, rawResponse ) ->
Expand Down Expand Up @@ -432,10 +433,10 @@ unoptimizedRequest requestWithSecrets expect =
)

Nothing ->
Secrets.maskedLookup requestWithSecrets
|> requestToString
|> Pages.StaticHttpRequest.MissingHttpResponse
|> Err
Err
(Pages.StaticHttpRequest.MissingHttpResponse (requestToString (Secrets.maskedLookup requestWithSecrets))
[ requestWithSecrets ]
)
)
|> Result.andThen
(\( strippedResponses, rawResponse ) ->
Expand Down
40 changes: 27 additions & 13 deletions src/Pages/StaticHttpRequest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Internal.OptimizedDecoder
import Json.Decode.Exploration
import Json.Encode
import KeepOrDiscard exposing (KeepOrDiscard)
import List.Extra
import OptimizedDecoder
import Pages.Internal.ApplicationType exposing (ApplicationType)
import Pages.StaticHttp.Request
Expand Down Expand Up @@ -200,15 +201,15 @@ strippedResponsesHelp usedSoFar appType request rawResponses =


type Error
= MissingHttpResponse String
= MissingHttpResponse String (List (Secrets.Value Pages.StaticHttp.Request.Request))
| DecoderError String
| UserCalledStaticHttpFail String


toBuildError : String -> Error -> BuildError
toBuildError path error =
case error of
MissingHttpResponse missingKey ->
MissingHttpResponse missingKey _ ->
{ title = "Missing Http Response"
, message =
[ Terminal.text missingKey
Expand Down Expand Up @@ -259,15 +260,27 @@ resolveUrls appType request rawResponses =
resolveUrlsHelp : ApplicationType -> RawRequest value -> RequestsAndPending -> List (Secrets.Value Pages.StaticHttp.Request.Request) -> List (Secrets.Value Pages.StaticHttp.Request.Request)
resolveUrlsHelp appType request rawResponses soFar =
case request of
RequestError _ ->
(soFar
-- TODO do I need to preserve the URLs here? -- urlList
)
RequestError error ->
case error of
MissingHttpResponse _ next ->
let
thing =
next |> List.map Secrets.maskedLookup
in
(soFar ++ next)
|> List.Extra.uniqueBy (Secrets.maskedLookup >> Pages.StaticHttp.Request.hash)

_ ->
soFar

Request _ ( urlList, lookupFn ) ->
case lookupFn KeepOrDiscard.Keep appType rawResponses of
nextRequest ->
resolveUrlsHelp appType nextRequest rawResponses (soFar ++ urlList)
lookupFn KeepOrDiscard.Keep appType rawResponses
|> (\nextRequest ->
resolveUrlsHelp appType
nextRequest
rawResponses
(soFar ++ urlList)
)

Done _ _ ->
soFar
Expand Down Expand Up @@ -298,7 +311,7 @@ cacheRequestResolutionHelp foundUrls appType request rawResponses =
case request of
RequestError error ->
case error of
MissingHttpResponse _ ->
MissingHttpResponse key _ ->
-- TODO do I need to pass through continuation URLs here? -- Incomplete (urlList ++ foundUrls)
Incomplete foundUrls

Expand All @@ -309,9 +322,10 @@ cacheRequestResolutionHelp foundUrls appType request rawResponses =
HasPermanentError error

Request _ ( urlList, lookupFn ) ->
case lookupFn KeepOrDiscard.Keep appType rawResponses of
nextRequest ->
cacheRequestResolutionHelp urlList appType nextRequest rawResponses
lookupFn KeepOrDiscard.Keep appType rawResponses
|> (\nextRequest ->
cacheRequestResolutionHelp urlList appType nextRequest rawResponses
)

Done _ _ ->
Complete
Loading

0 comments on commit aa0381c

Please sign in to comment.