notificationMessage =
case foo ofOk () ->
"Success!"Err errorMessage ->
"Failed to make this work because " ++ errorMessage
As shown in the following example, the rule will only check whether a wildcard (_) is used for Err. I recommend enabling the
NoUnused.Patterns rule
to help get you to the state where the noise is removed.
notificationMessage =
case foo ofOk () ->
"Success!"Err errorMessage ->
-- `errorMessage` is not used, but the rule only checks whether a wildcard is being used."Failure!"
In some cases, you will genuinely only care about whether something has failed or not. In those cases, you can transform the result
in a way where you explicitly show that you don't care about the error.
To do the above, I recommend using Result.mapError or Result.toMaybe,
and to use them at the earliest possible convenience, such as when you are creating the Task, so that your the associated constructor shows you will ignore the error.
typeMsg
= UserClickedOnSend
| GotServerResponse (Result () Data)
update : Msg -> Model -> ( Model, CmdMsg )
update msg model =
case msg ofUserClickedOnSend ->
( model
, Http.get
{ url = "https://elm-lang.org/assets/public-opinion.txt"
, expect =
Http.expectString
(Result.mapError (\_ -> ()) >> GotServerResponse)
}
)
GotServerResponse (Ok data) ->
( { model | data = data }, Cmd.none )
GotServerResponse (Err ()) ->
( { model | errorWasReceived = True }, Cmd.none )
When (not) to enable this rule
This rule is still experimental. I am trying to figure out if this error is always useful or how to tweak it to remove
false positives and discover more cases where errors are ignored.
Try it out
You can try this rule out by running the following command:
Reports when error details are not being used.
config = [ NoIgnoringErrors.rule ]Fail
notificationMessage = case foo of Ok () -> "Success!" Err _ -> "Failure!"Success
notificationMessage = case foo of Ok () -> "Success!" Err errorMessage -> "Failed to make this work because " ++ errorMessageAs shown in the following example, the rule will only check whether a wildcard (
_) is used forErr. I recommend enabling theNoUnused.Patternsrule to help get you to the state where the noise is removed.notificationMessage = case foo of Ok () -> "Success!" Err errorMessage -> -- `errorMessage` is not used, but the rule only checks whether a wildcard is being used. "Failure!"In some cases, you will genuinely only care about whether something has failed or not. In those cases, you can transform the result in a way where you explicitly show that you don't care about the error.
hasFailed = case Result.mapError (\_ -> ()) foo of Ok data -> ... Err () -> ...To do the above, I recommend using
Result.mapErrororResult.toMaybe, and to use them at the earliest possible convenience, such as when you are creating theTask, so that your the associated constructor shows you will ignore the error.type Msg = UserClickedOnSend | GotServerResponse (Result () Data) update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of UserClickedOnSend -> ( model , Http.get { url = "https://elm-lang.org/assets/public-opinion.txt" , expect = Http.expectString (Result.mapError (\_ -> ()) >> GotServerResponse) } ) GotServerResponse (Ok data) -> ( { model | data = data }, Cmd.none ) GotServerResponse (Err ()) -> ( { model | errorWasReceived = True }, Cmd.none )When (not) to enable this rule
This rule is still experimental. I am trying to figure out if this error is always useful or how to tweak it to remove false positives and discover more cases where errors are ignored.
Try it out
You can try this rule out by running the following command: