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 ->
Script.log (String.fromInt stars)
)
|> BackendTask.throw
|> BackendTask.allowFatal
)

View File

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

View File

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

View File

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

View File

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

View File

@ -105,7 +105,7 @@ get portName input decoder =
|> Decode.andThen
(\errorKind ->
if errorKind == "PortNotDefined" then
{ fatal =
FatalError.recoverable
{ title = "Port Error"
, body =
[ 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
}
, recoverable = PortNotDefined { name = portName }
}
(PortNotDefined { name = portName })
|> Decode.succeed
else if errorKind == "ExportIsNotFunction" then
@ -124,7 +123,7 @@ get portName input decoder =
|> Decode.map (Maybe.withDefault "")
|> Decode.map
(\incorrectPortType ->
FatalError.Recoverable
FatalError.recoverable
{ title = "Port Error"
, body =
[ 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
FatalError.Recoverable
FatalError.recoverable
{ title = "Port Error"
, 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."
@ -154,7 +153,7 @@ get portName input decoder =
|> Decode.map (Maybe.withDefault "")
|> Decode.map
(\errorMessage ->
FatalError.Recoverable
FatalError.recoverable
{ title = "Port Error"
, 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"
@ -172,7 +171,7 @@ get portName input decoder =
|> Decode.map (Maybe.withDefault Encode.null)
|> Decode.map
(\portCallError ->
FatalError.Recoverable
FatalError.recoverable
{ title = "Port Error"
, 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"
@ -185,7 +184,7 @@ get portName input decoder =
)
else
FatalError.Recoverable
FatalError.recoverable
{ title = "Port Error"
, body =
[ 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
( FatalError, fromString, fromStringWithValue
( FatalError, fromString, recoverable
, Recoverable
)
@ -57,12 +57,14 @@ issue.
In the case of server-rendered Routes (`RouteBuilder.serverRender`), `elm-pages` will show your 500 error page
when these errors occur.
@docs FatalError, fromString, fromStringWithValue
@docs FatalError, fromString, recoverable
@docs Recoverable
-}
import Pages.Internal.FatalError
{-| -}
type alias Recoverable error =
@ -73,20 +75,27 @@ type alias Recoverable error =
{-| -}
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 =
{ title = "Custom Error"
, body = string
}
build
{ title = "Custom Error"
, body = string
}
{-| -}
fromStringWithValue : String -> value -> Recoverable value
fromStringWithValue string value =
{ fatal = { title = "Custom Error", body = string }
recoverable : { title : String, body : String } -> value -> Recoverable value
recoverable info value =
{ fatal = build info
, 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 FatalError exposing (FatalError, Recoverable)
import List.Extra
import Pages.Internal.FatalError
import Pages.StaticHttp.Request as HashRequest
import Pages.StaticHttpRequest as StaticHttpRequest
import RequestsAndPending exposing (RequestsAndPending)
@ -111,7 +112,7 @@ nextStep allRawResponses staticResponses { errors } =
Just (Ok completed) ->
Finish completed
Just (Err buildError) ->
Just (Err (Pages.Internal.FatalError.FatalError buildError)) ->
FinishedWithErrors
[ { title = buildError.title |> String.toUpper
, path = "" -- TODO include path here