Make FatalError opaque.

This commit is contained in:
Dillon Kearns 2023-01-16 21:43:59 -08:00
parent 95a49f6ec9
commit d61ddecca8
10 changed files with 47 additions and 34 deletions

File diff suppressed because one or more lines are too long

View File

@ -20,7 +20,7 @@ run =
(\stars -> (\stars ->
Script.log (String.fromInt stars) Script.log (String.fromInt stars)
) )
|> BackendTask.throw |> BackendTask.allowFatal
) )

View File

@ -75,7 +75,7 @@ expect envVariableName =
(\maybeValue -> (\maybeValue ->
maybeValue maybeValue
|> Result.fromMaybe |> Result.fromMaybe
{ fatal = (FatalError.recoverable
{ title = "Missing Env Variable" { title = "Missing Env Variable"
, body = , body =
[ TerminalText.text "BackendTask.Env.expect was expecting a variable `" [ TerminalText.text "BackendTask.Env.expect was expecting a variable `"
@ -84,7 +84,7 @@ expect envVariableName =
] ]
|> TerminalText.toString |> TerminalText.toString
} }
, recoverable = MissingEnvVariable envVariableName (MissingEnvVariable envVariableName)
} )
|> BackendTask.fromResult |> BackendTask.fromResult
) )

View File

@ -326,15 +326,14 @@ jsonFile jsonFileDecoder filePath =
|> Decode.decodeString jsonFileDecoder |> Decode.decodeString jsonFileDecoder
|> Result.mapError |> Result.mapError
(\jsonDecodeError -> (\jsonDecodeError ->
{ fatal = FatalError.recoverable
{ title = "JSON Decoding Error" { title = "JSON Decoding Error"
, body = , body =
[ TerminalText.text (Decode.errorToString jsonDecodeError) [ TerminalText.text (Decode.errorToString jsonDecodeError)
] ]
|> TerminalText.toString |> TerminalText.toString
} }
, recoverable = DecodingError jsonDecodeError (DecodingError jsonDecodeError)
}
) )
|> BackendTask.fromResult |> BackendTask.fromResult
) )
@ -372,7 +371,7 @@ errorDecoder :
} }
errorDecoder filePath = errorDecoder filePath =
Decode.succeed Decode.succeed
{ fatal = (FatalError.recoverable
{ title = "File Doesn't Exist" { title = "File Doesn't Exist"
, body = , body =
[ TerminalText.text "Couldn't find file at path `" [ TerminalText.text "Couldn't find file at path `"
@ -381,5 +380,5 @@ errorDecoder filePath =
] ]
|> TerminalText.toString |> TerminalText.toString
} }
, recoverable = FileDoesntExist FileDoesntExist
} )

View File

@ -1066,14 +1066,14 @@ expectUniqueMatch glob =
[] -> [] ->
BackendTask.fail <| BackendTask.fail <|
FatalError.fromStringWithValue FatalError.recoverable
("No files matched the pattern: " ++ toPatternString glob) { title = "Non-Unique Glob", body = "No files matched the pattern: " ++ toPatternString glob }
("No files matched the pattern: " ++ toPatternString glob) ("No files matched the pattern: " ++ toPatternString glob)
_ -> _ ->
BackendTask.fail <| BackendTask.fail <|
FatalError.fromStringWithValue FatalError.recoverable
"More than one file matched." { title = "Non-Unique Glob", body = "Expected a unique match, but more than one file matched." }
"More than one file matched." "More than one file matched."
) )

View File

@ -573,7 +573,7 @@ requestRaw request__ expect =
|> BackendTask.fromResult |> BackendTask.fromResult
|> BackendTask.mapError |> BackendTask.mapError
(\error -> (\error ->
FatalError.Recoverable (errorToString error) error FatalError.recoverable (errorToString error) error
) )
) )

View File

@ -105,7 +105,7 @@ get portName input decoder =
|> Decode.andThen |> Decode.andThen
(\errorKind -> (\errorKind ->
if errorKind == "PortNotDefined" then if errorKind == "PortNotDefined" then
{ fatal = FatalError.recoverable
{ title = "Port Error" { title = "Port Error"
, body = , body =
[ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I expected to find a port named `" [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I expected to find a port named `"
@ -114,8 +114,7 @@ get portName input decoder =
] ]
|> TerminalText.toString |> TerminalText.toString
} }
, recoverable = PortNotDefined { name = portName } (PortNotDefined { name = portName })
}
|> Decode.succeed |> Decode.succeed
else if errorKind == "ExportIsNotFunction" then else if errorKind == "ExportIsNotFunction" then
@ -124,7 +123,7 @@ get portName input decoder =
|> Decode.map (Maybe.withDefault "") |> Decode.map (Maybe.withDefault "")
|> Decode.map |> Decode.map
(\incorrectPortType -> (\incorrectPortType ->
FatalError.Recoverable FatalError.recoverable
{ title = "Port Error" { title = "Port Error"
, body = , body =
[ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I found an export called `" [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I found an export called `"
@ -138,7 +137,7 @@ get portName input decoder =
) )
else if errorKind == "MissingPortsFile" then else if errorKind == "MissingPortsFile" then
FatalError.Recoverable FatalError.recoverable
{ title = "Port Error" { title = "Port Error"
, body = , body =
[ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I couldn't find your port-data-source file. Be sure to create a 'port-data-source.ts' or 'port-data-source.js' file." [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I couldn't find your port-data-source file. Be sure to create a 'port-data-source.ts' or 'port-data-source.js' file."
@ -154,7 +153,7 @@ get portName input decoder =
|> Decode.map (Maybe.withDefault "") |> Decode.map (Maybe.withDefault "")
|> Decode.map |> Decode.map
(\errorMessage -> (\errorMessage ->
FatalError.Recoverable FatalError.recoverable
{ title = "Port Error" { title = "Port Error"
, body = , body =
[ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I couldn't import the port definitions file, because of this exception:\n\n" [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I couldn't import the port definitions file, because of this exception:\n\n"
@ -172,7 +171,7 @@ get portName input decoder =
|> Decode.map (Maybe.withDefault Encode.null) |> Decode.map (Maybe.withDefault Encode.null)
|> Decode.map |> Decode.map
(\portCallError -> (\portCallError ->
FatalError.Recoverable FatalError.recoverable
{ title = "Port Error" { title = "Port Error"
, body = , body =
[ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I was able to import the port definitions file, but when running it I encountered this exception:\n\n" [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I was able to import the port definitions file, but when running it I encountered this exception:\n\n"
@ -185,7 +184,7 @@ get portName input decoder =
) )
else else
FatalError.Recoverable FatalError.recoverable
{ title = "Port Error" { title = "Port Error"
, body = , body =
[ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I expected to find a port named `" [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I expected to find a port named `"

View File

@ -1,5 +1,5 @@
module FatalError exposing module FatalError exposing
( FatalError, fromString, fromStringWithValue ( FatalError, fromString, recoverable
, Recoverable , Recoverable
) )
@ -57,12 +57,14 @@ issue.
In the case of server-rendered Routes (`RouteBuilder.serverRender`), `elm-pages` will show your 500 error page In the case of server-rendered Routes (`RouteBuilder.serverRender`), `elm-pages` will show your 500 error page
when these errors occur. when these errors occur.
@docs FatalError, fromString, fromStringWithValue @docs FatalError, fromString, recoverable
@docs Recoverable @docs Recoverable
-} -}
import Pages.Internal.FatalError
{-| -} {-| -}
type alias Recoverable error = type alias Recoverable error =
@ -73,20 +75,27 @@ type alias Recoverable error =
{-| -} {-| -}
type alias FatalError = type alias FatalError =
{ title : String, body : String } Pages.Internal.FatalError.FatalError
{-| -}
build : { title : String, body : String } -> FatalError
build info =
Pages.Internal.FatalError.FatalError info
{-| -} {-| -}
fromString : String -> FatalError fromString : String -> FatalError
fromString string = fromString string =
{ title = "Custom Error" build
, body = string { title = "Custom Error"
} , body = string
}
{-| -} {-| -}
fromStringWithValue : String -> value -> Recoverable value recoverable : { title : String, body : String } -> value -> Recoverable value
fromStringWithValue string value = recoverable info value =
{ fatal = { title = "Custom Error", body = string } { fatal = build info
, recoverable = value , recoverable = value
} }

View File

@ -0,0 +1,5 @@
module Pages.Internal.FatalError exposing (FatalError(..))
type FatalError
= FatalError { title : String, body : String }

View File

@ -4,6 +4,7 @@ import BackendTask exposing (BackendTask)
import BuildError exposing (BuildError) import BuildError exposing (BuildError)
import FatalError exposing (FatalError, Recoverable) import FatalError exposing (FatalError, Recoverable)
import List.Extra import List.Extra
import Pages.Internal.FatalError
import Pages.StaticHttp.Request as HashRequest import Pages.StaticHttp.Request as HashRequest
import Pages.StaticHttpRequest as StaticHttpRequest import Pages.StaticHttpRequest as StaticHttpRequest
import RequestsAndPending exposing (RequestsAndPending) import RequestsAndPending exposing (RequestsAndPending)
@ -111,7 +112,7 @@ nextStep allRawResponses staticResponses { errors } =
Just (Ok completed) -> Just (Ok completed) ->
Finish completed Finish completed
Just (Err buildError) -> Just (Err (Pages.Internal.FatalError.FatalError buildError)) ->
FinishedWithErrors FinishedWithErrors
[ { title = buildError.title |> String.toUpper [ { title = buildError.title |> String.toUpper
, path = "" -- TODO include path here , path = "" -- TODO include path here