mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-29 09:32:57 +03:00
Improve error messages in CLI.
This commit is contained in:
parent
179527d071
commit
d014dbf687
@ -15,6 +15,7 @@ module Pages.ContentCache exposing
|
||||
, update
|
||||
)
|
||||
|
||||
import BuildError exposing (BuildError)
|
||||
import Dict exposing (Dict)
|
||||
import Html exposing (Html)
|
||||
import Html.Attributes as Attr
|
||||
@ -26,6 +27,7 @@ import Pages.Document as Document exposing (Document)
|
||||
import Pages.PagePath as PagePath exposing (PagePath)
|
||||
import Result.Extra
|
||||
import Task exposing (Task)
|
||||
import TerminalText as Terminal
|
||||
import Url exposing (Url)
|
||||
import Url.Builder
|
||||
|
||||
@ -39,7 +41,7 @@ type alias ContentCache metadata view =
|
||||
|
||||
|
||||
type alias Errors =
|
||||
Dict Path String
|
||||
List BuildError
|
||||
|
||||
|
||||
type alias ContentCacheInner metadata view =
|
||||
@ -81,7 +83,7 @@ getMetadata entry =
|
||||
metadata
|
||||
|
||||
|
||||
pagesWithErrors : ContentCache metadata view -> Maybe (Dict (List String) String)
|
||||
pagesWithErrors : ContentCache metadata view -> List BuildError
|
||||
pagesWithErrors cache =
|
||||
cache
|
||||
|> Result.map
|
||||
@ -94,7 +96,7 @@ pagesWithErrors cache =
|
||||
Parsed metadata { body } ->
|
||||
case body of
|
||||
Err parseError ->
|
||||
Just ( path, parseError )
|
||||
createBuildError path parseError |> Just
|
||||
|
||||
_ ->
|
||||
Nothing
|
||||
@ -103,18 +105,7 @@ pagesWithErrors cache =
|
||||
Nothing
|
||||
)
|
||||
)
|
||||
|> Result.map
|
||||
(\errors ->
|
||||
case errors of
|
||||
[] ->
|
||||
Nothing
|
||||
|
||||
_ ->
|
||||
errors
|
||||
|> Dict.fromList
|
||||
|> Just
|
||||
)
|
||||
|> Result.withDefault Nothing
|
||||
|> Result.withDefault []
|
||||
|
||||
|
||||
init :
|
||||
@ -128,15 +119,30 @@ init document content =
|
||||
Tuple.mapSecond
|
||||
(\result ->
|
||||
result
|
||||
|> Result.mapError (\error -> ( Tuple.first tuple, error ))
|
||||
|> Result.mapError
|
||||
(\error ->
|
||||
-- ( Tuple.first tuple, error )
|
||||
createBuildError (Tuple.first tuple) error
|
||||
)
|
||||
)
|
||||
tuple
|
||||
)
|
||||
|> combineTupleResults
|
||||
|> Result.mapError Dict.fromList
|
||||
-- |> Result.mapError Dict.fromList
|
||||
|> Result.map Dict.fromList
|
||||
|
||||
|
||||
createBuildError : List String -> String -> BuildError
|
||||
createBuildError path decodeError =
|
||||
{ message =
|
||||
[ Terminal.text "I ran into a problem when parsing the metadata for the page with this path: "
|
||||
, Terminal.text ("/" ++ (path |> String.join "/"))
|
||||
, Terminal.text "\n\n"
|
||||
, Terminal.text decodeError
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
parseMetadata :
|
||||
Document metadata view
|
||||
-> List ( List String, { extension : String, frontMatter : String, body : Maybe String } )
|
||||
@ -195,15 +201,22 @@ parseContent extension body document =
|
||||
errorView : Errors -> Html msg
|
||||
errorView errors =
|
||||
errors
|
||||
|> Dict.toList
|
||||
-- |> Dict.toList
|
||||
|> List.map errorEntryView
|
||||
|> Html.div
|
||||
[ Attr.style "padding" "20px 100px"
|
||||
]
|
||||
|
||||
|
||||
errorEntryView : ( Path, String ) -> Html msg
|
||||
errorEntryView ( path, error ) =
|
||||
errorEntryView : BuildError -> Html msg
|
||||
errorEntryView buildError =
|
||||
let
|
||||
path =
|
||||
[ "TODO" ]
|
||||
|
||||
error =
|
||||
"TODO"
|
||||
in
|
||||
Html.div []
|
||||
[ Html.h2 []
|
||||
[ Html.text ("/" ++ (path |> String.join "/"))
|
||||
|
@ -123,8 +123,8 @@ type alias Model =
|
||||
|
||||
type Error
|
||||
= MissingSecret BuildError
|
||||
| MetadataDecodeError ErrorContext String
|
||||
| InternalError String
|
||||
| MetadataDecodeError BuildError
|
||||
| InternalError BuildError
|
||||
|
||||
|
||||
type alias ErrorContext =
|
||||
@ -190,12 +190,8 @@ cliApplication cliMsgConstructor narrowMsg toModel fromModel config =
|
||||
contentCache
|
||||
|> Result.map
|
||||
(\cache -> cache |> ContentCache.extractMetadata config.pathKey)
|
||||
|> Result.mapError
|
||||
(\error ->
|
||||
error
|
||||
|> Dict.toList
|
||||
|> List.map (\( path, errorString ) -> errorString)
|
||||
)
|
||||
|
||||
-- |> Result.mapError MetadataDecodeError
|
||||
in
|
||||
Platform.worker
|
||||
{ init =
|
||||
@ -251,7 +247,7 @@ perform cliMsgConstructor toJsPort effect =
|
||||
init :
|
||||
(Model -> model)
|
||||
-> ContentCache.ContentCache metadata view
|
||||
-> Result (List String) (List ( PagePath pathKey, metadata ))
|
||||
-> Result (List BuildError) (List ( PagePath pathKey, metadata ))
|
||||
->
|
||||
{ config
|
||||
| view :
|
||||
@ -276,40 +272,7 @@ init toModel contentCache siteMetadata config cliMsgConstructor flags =
|
||||
case contentCache of
|
||||
Ok _ ->
|
||||
case contentCache |> ContentCache.pagesWithErrors of
|
||||
Just pageErrors ->
|
||||
let
|
||||
requests =
|
||||
siteMetadata
|
||||
|> Result.andThen
|
||||
(\metadata ->
|
||||
staticResponseForPage metadata config.view
|
||||
)
|
||||
|
||||
staticResponses : StaticResponses
|
||||
staticResponses =
|
||||
case requests of
|
||||
Ok okRequests ->
|
||||
staticResponsesInit okRequests
|
||||
|
||||
Err errors ->
|
||||
Dict.empty
|
||||
in
|
||||
case Decode.decodeValue (Decode.field "secrets" Decode.string) flags of
|
||||
Ok _ ->
|
||||
( Model staticResponses secrets (pageErrorsToErrors pageErrors) |> toModel
|
||||
, NoEffect
|
||||
)
|
||||
|
||||
Err error ->
|
||||
( Model staticResponses
|
||||
Secrets.empty
|
||||
[ InternalError <| "Failed to parse flags: " ++ Decode.errorToString error
|
||||
]
|
||||
|> toModel
|
||||
, NoEffect
|
||||
)
|
||||
|
||||
Nothing ->
|
||||
[] ->
|
||||
let
|
||||
requests =
|
||||
siteMetadata
|
||||
@ -354,28 +317,60 @@ init toModel contentCache siteMetadata config cliMsgConstructor flags =
|
||||
, NoEffect
|
||||
)
|
||||
|
||||
pageErrors ->
|
||||
let
|
||||
requests =
|
||||
siteMetadata
|
||||
|> Result.andThen
|
||||
(\metadata ->
|
||||
staticResponseForPage metadata config.view
|
||||
)
|
||||
|
||||
staticResponses : StaticResponses
|
||||
staticResponses =
|
||||
case requests of
|
||||
Ok okRequests ->
|
||||
staticResponsesInit okRequests
|
||||
|
||||
Err errors ->
|
||||
Dict.empty
|
||||
in
|
||||
case Decode.decodeValue (Decode.field "secrets" Decode.string) flags of
|
||||
Ok _ ->
|
||||
( Model staticResponses secrets (pageErrors |> List.map MetadataDecodeError) |> toModel
|
||||
, sendStaticResponsesIfDone
|
||||
(pageErrors |> List.map MetadataDecodeError)
|
||||
Dict.empty
|
||||
config.manifest
|
||||
)
|
||||
|
||||
Err error ->
|
||||
( Model staticResponses
|
||||
Secrets.empty
|
||||
[ InternalError <| { message = [ Terminal.text <| "Failed to parse flags: " ++ Decode.errorToString error ] } ]
|
||||
|> toModel
|
||||
, NoEffect
|
||||
)
|
||||
|
||||
Err metadataParserErrors ->
|
||||
( Model Dict.empty
|
||||
secrets
|
||||
(metadataParserErrors
|
||||
|> Dict.toList
|
||||
|> List.map
|
||||
(\( path, parserError ) ->
|
||||
MetadataDecodeError { path = path } parserError
|
||||
)
|
||||
)
|
||||
(metadataParserErrors |> List.map MetadataDecodeError)
|
||||
|> toModel
|
||||
, NoEffect
|
||||
, sendStaticResponsesIfDone
|
||||
(metadataParserErrors |> List.map MetadataDecodeError)
|
||||
Dict.empty
|
||||
config.manifest
|
||||
)
|
||||
|
||||
Err error ->
|
||||
( Model Dict.empty
|
||||
Secrets.empty
|
||||
[ InternalError <| "Failed to parse flags: " ++ Decode.errorToString error
|
||||
[ InternalError <| { message = [ Terminal.text <| "Failed to parse flags: " ++ Decode.errorToString error ] }
|
||||
]
|
||||
|> toModel
|
||||
, sendStaticResponsesIfDone
|
||||
[ InternalError <| "Failed to parse flags: " ++ Decode.errorToString error
|
||||
[ InternalError <| { message = [ Terminal.text <| "Failed to parse flags: " ++ Decode.errorToString error ] }
|
||||
]
|
||||
Dict.empty
|
||||
config.manifest
|
||||
@ -386,16 +381,6 @@ type alias PageErrors =
|
||||
Dict String String
|
||||
|
||||
|
||||
pageErrorsToErrors : Dict (List String) String -> List Error
|
||||
pageErrorsToErrors pageErrors =
|
||||
pageErrors
|
||||
|> Dict.toList
|
||||
|> List.map
|
||||
(\( pagePath, errorString ) ->
|
||||
MetadataDecodeError { path = pagePath } errorString
|
||||
)
|
||||
|
||||
|
||||
pageErrorsToString : PageErrors -> String
|
||||
pageErrorsToString pageErrors =
|
||||
pageErrors
|
||||
@ -410,7 +395,7 @@ pageErrorsToString pageErrors =
|
||||
|
||||
|
||||
update :
|
||||
Result (List String) (List ( PagePath pathKey, metadata ))
|
||||
Result (List BuildError) (List ( PagePath pathKey, metadata ))
|
||||
->
|
||||
{ config
|
||||
| -- update : userMsg -> userModel -> ( userModel, Cmd userMsg )
|
||||
@ -629,11 +614,11 @@ errorToString error =
|
||||
MissingSecret buildError ->
|
||||
buildError.message |> Terminal.toString
|
||||
|
||||
MetadataDecodeError errorContext string ->
|
||||
"Error decoding metadata"
|
||||
MetadataDecodeError buildError ->
|
||||
buildError.message |> Terminal.toString
|
||||
|
||||
InternalError errorMessage ->
|
||||
errorMessage
|
||||
InternalError buildError ->
|
||||
buildError.message |> Terminal.toString
|
||||
|
||||
|
||||
encodeStaticResponses : StaticResponses -> Dict String (Dict String String)
|
||||
@ -670,7 +655,7 @@ staticResponseForPage :
|
||||
}
|
||||
)
|
||||
->
|
||||
Result (List String)
|
||||
Result (List BuildError)
|
||||
(List
|
||||
( PagePath pathKey
|
||||
, StaticHttp.Request
|
||||
|
Loading…
Reference in New Issue
Block a user