Skip to content

Error handling simple

Brian Marick edited this page Jan 22, 2018 · 3 revisions

Problem 1

FlowSolution
FlowAltSolution

I don't really have a preference for one over the other. whenOk_do is simpler in the Flow version, but always_do is simpler in the FlowAlt version.

As we'll see later in the chapter, our final app will need more data, which it will store in a record with a model field. So the FlowAlt solution upgrades more easily, at least along one evolutionary path.

On the other hand, the ok field isn't very clear about what's ok. It might be easy for someone new to the code to think it's about whether the model is OK (which it isn't). Maybe errorsEncountered?

Problem 2

My two solutions:

finishWith : Cmd msg -> ModelState model -> (model, Cmd msg)
finishWith cmd soFar = 
  case soFar of
    Ok model -> (model, cmd)
    Err model -> (model, Cmd.none)

finishWith : Cmd msg -> ModelState model -> (model, Cmd msg)
finishWith cmd soFar = 
  case soFar.ok of
    True ->  (soFar.model, cmd)
    False -> (soFar.model, Cmd.none)

I don't think the two implementations give us any reason to prefer one ModelState over the other.

Problem 3

I don't think it's a good idea. We've found no need for an always try case, but separating the two responsibilities would force us to implement it.

That's the same argument I used in chapter 8 for preferring this:

type AnimalId
= ByName String
| ByNumber Int
| ByBoth String Int

to this:

(Just "Betsy", Nothing) 
(Nothing, Just 12) 
(Just "Betsy", Just 12)

The latter representation allows an invalid value; the former makes it impossible.

Clone this wiki locally