Distinguish between permanent StaticHttp failures and temporary failures before all responses are available.

This commit is contained in:
Dillon Kearns 2019-11-02 07:16:38 -07:00
parent 9d0653a9aa
commit 3d4c487add
5 changed files with 139 additions and 42 deletions

View File

@ -133,9 +133,18 @@ pageViewOrError pathKey viewFn model cache =
Err error ->
{ title = "Parsing error"
, body =
Html.text <|
"Could not load static data - TODO better error here."
++ error
case error of
StaticHttpRequest.DecoderError decoderError ->
Html.div []
[ Html.text "Could not parse static data. I encountered this decoder problem."
, Html.pre [] [ Html.text decoderError ]
]
StaticHttpRequest.MissingHttpResponse missingKey ->
Html.div []
[ Html.text "I'm missing some StaticHttp data for this page:"
, Html.pre [] [ Html.text missingKey ]
]
}
Err error ->

View File

@ -705,6 +705,21 @@ sendStaticResponsesIfDone secrets allRawResponses errors staticResponses manifes
case entry of
NotFetched request rawResponses ->
let
usableRawResponses : Dict String String
usableRawResponses =
rawResponses
|> Dict.Extra.filterMap
(\key value ->
value
|> Result.map Just
|> Result.withDefault Nothing
)
hasPermanentError =
StaticHttpRequest.permanentError request usableRawResponses
|> Maybe.map (\_ -> True)
|> Maybe.withDefault False
( allUrlsKnown, knownUrlsToFetch ) =
StaticHttpRequest.resolveUrls request
(rawResponses |> Dict.map (\key value -> value |> Result.withDefault ""))
@ -713,7 +728,7 @@ sendStaticResponsesIfDone secrets allRawResponses errors staticResponses manifes
(knownUrlsToFetch |> List.map Pages.Internal.Secrets.useFakeSecrets |> Set.fromList |> Set.size)
== (rawResponses |> Dict.keys |> List.length)
in
if allUrlsKnown && fetchedAllKnownUrls then
if hasPermanentError || (allUrlsKnown && fetchedAllKnownUrls) then
False
else
@ -726,38 +741,66 @@ sendStaticResponsesIfDone secrets allRawResponses errors staticResponses manifes
|> List.concatMap
(\( path, NotFetched request rawResponses ) ->
let
usableRawResponses : Dict String String
usableRawResponses =
rawResponses
|> Dict.Extra.filterMap
(\key value ->
value
|> Result.map Just
|> Result.withDefault Nothing
)
maybePermanentError =
StaticHttpRequest.permanentError request
usableRawResponses
decoderErrors =
maybePermanentError
|> Maybe.map (StaticHttpRequest.toBuildError path)
|> Maybe.map FailedStaticHttpRequestError
|> Maybe.map List.singleton
|> Maybe.withDefault []
lookup =
case request of
StaticHttpRequest.Request ( urls, requestLookup ) ->
requestLookup
-- @@@@@@@@ TODO change this
-- StaticHttpRequest.resolve <|
-- StaticHttpRequest.Request ( urls, requestLookup )
StaticHttpRequest.Done value ->
\_ -> Ok (StaticHttpRequest.Done value)
in
case rawResponsesResult rawResponses of
Ok responses ->
case lookup responses of
Ok _ ->
[]
httpErrors =
case rawResponsesResult rawResponses of
Ok responses ->
case lookup responses of
Ok _ ->
[]
Err error ->
{ message =
[ Terminal.text path
, Terminal.text "\n\n"
, Terminal.text (StaticHttpRequest.errorToString error)
]
}
-- TODO should this be a decoding http error instead?
|> FailedStaticHttpRequestError
|> List.singleton
Err error ->
{ message =
[ Terminal.text path
, Terminal.text "\n\n"
, Terminal.text error
]
}
-- TODO should this be a decoding http error instead?
|> FailedStaticHttpRequestError
|> List.singleton
Err error ->
[ { message =
[ Terminal.text path
[ { message =
[ Terminal.text path
]
}
]
}
]
|> List.map FailedStaticHttpRequestError
|> List.map FailedStaticHttpRequestError
in
decoderErrors
-- ++ (httpErrors |> Debug.log "http errors")
)
in
if pendingRequests then

View File

@ -1,4 +1,4 @@
module Pages.Internal.Secrets exposing (Secrets(..), decoder, empty, useFakeSecrets)
module Pages.Internal.Secrets exposing (Secrets(..), UrlWithSecrets, decoder, empty, useFakeSecrets)
import BuildError exposing (BuildError)
import Dict exposing (Dict)

View File

@ -1,16 +1,32 @@
module Pages.StaticHttpRequest exposing (Request(..), resolve, resolveUrls, urls)
module Pages.StaticHttpRequest exposing (Error(..), Request(..), errorToString, permanentError, resolve, resolveUrls, toBuildError, urls)
import BuildError exposing (BuildError)
import Dict exposing (Dict)
import Pages.Internal.Secrets
import Secrets exposing (Secrets)
import TerminalText as Terminal
type Request value
= Request ( List (Secrets -> Result BuildError String), Dict String String -> Result String (Request value) )
= Request ( List (Secrets -> Result BuildError String), Dict String String -> Result Error (Request value) )
| Done value
errorToString : Error -> String
errorToString error =
case error of
MissingHttpResponse string ->
string
DecoderError string ->
string
type Error
= MissingHttpResponse String
| DecoderError String
urls : Request value -> List (Secrets -> Result BuildError String)
urls request =
case request of
@ -21,7 +37,37 @@ urls request =
[]
resolve : Request value -> Dict String String -> Result String value
toBuildError : String -> Error -> BuildError
toBuildError path error =
{ message =
[ Terminal.text path
, Terminal.text "\n\n"
, Terminal.text (errorToString error)
]
}
permanentError : Request value -> Dict String String -> Maybe Error
permanentError request rawResponses =
case request of
Request ( urlList, lookupFn ) ->
case lookupFn rawResponses of
Ok nextRequest ->
permanentError nextRequest rawResponses
Err error ->
case error of
MissingHttpResponse _ ->
Nothing
DecoderError _ ->
Just error
Done value ->
Nothing
resolve : Request value -> Dict String String -> Result Error value
resolve request rawResponses =
case request of
Request ( urlList, lookupFn ) ->
@ -30,13 +76,13 @@ resolve request rawResponses =
resolve nextRequest rawResponses
Err error ->
Err "TODO error message"
Err error
Done value ->
Ok value
resolveUrls : Request value -> Dict String String -> ( Bool, List (Secrets -> Result BuildError String) )
resolveUrls : Request value -> Dict String String -> ( Bool, List Pages.Internal.Secrets.UrlWithSecrets )
resolveUrls request rawResponses =
case request of
Request ( urlList, lookupFn ) ->

View File

@ -47,14 +47,11 @@ map2 fn request1 request2 =
case ( request1, request2 ) of
( Request ( urls1, lookupFn1 ), Request ( urls2, lookupFn2 ) ) ->
let
value : Dict String String -> Result String (Request c)
value rawResponses =
let
value1 : Result String (Request a)
value1 =
lookupFn1 rawResponses
value2 : Result String (Request b)
value2 =
lookupFn2 rawResponses
in
@ -70,7 +67,6 @@ map2 fn request1 request2 =
( urls1
, \rawResponses ->
let
value1 : Result String (Request a)
value1 =
lookupFn1 rawResponses
in
@ -82,7 +78,6 @@ map2 fn request1 request2 =
( urls1
, \rawResponses ->
let
value1 : Result String (Request b)
value1 =
lookupFn1 rawResponses
in
@ -93,7 +88,7 @@ map2 fn request1 request2 =
fn value1 value2 |> Done
lookup : Pages.StaticHttpRequest.Request value -> Dict String String -> Result String value
lookup : Pages.StaticHttpRequest.Request value -> Dict String String -> Result Pages.StaticHttpRequest.Error value
lookup request rawResponses =
case request of
Request ( urls, lookupFn ) ->
@ -176,14 +171,16 @@ jsonRequest url decoder =
Ok rawResponse
Nothing ->
Err <| "Couldn't find response for url `" ++ url ++ "`... available: \n[ " ++ (Dict.keys rawResponseDict |> String.join ", ") ++ " ]"
-- Err <| "Couldn't find response for url `" ++ url ++ "`... available: \n[ " ++ (Dict.keys rawResponseDict |> String.join ", ") ++ " ]"
Err <| Pages.StaticHttpRequest.MissingHttpResponse url
)
|> Result.andThen
(\rawResponse ->
rawResponse
|> Decode.decodeString decoder
|> Result.map Done
|> Result.mapError Decode.errorToString
|> Result.mapError Pages.StaticHttpRequest.DecoderError
|> Result.map Done
)
)
@ -201,14 +198,16 @@ jsonRequestWithSecrets urlWithSecrets decoder =
Ok rawResponse
Nothing ->
Err <| "Couldn't find response for url `" ++ Pages.Internal.Secrets.useFakeSecrets urlWithSecrets ++ "`"
-- Err <| "Couldn't find response for url `" ++ Pages.Internal.Secrets.useFakeSecrets urlWithSecrets ++ "`"
Err <| Pages.StaticHttpRequest.MissingHttpResponse <| Pages.Internal.Secrets.useFakeSecrets urlWithSecrets
)
|> Result.andThen
(\rawResponse ->
rawResponse
|> Decode.decodeString decoder
|> Result.map Done
|> Result.mapError Decode.errorToString
|> Result.mapError Pages.StaticHttpRequest.DecoderError
|> Result.map Done
)
)