# Make bugs into type errors

**URL:** <https://discourse.elm-lang.org/t/make-bugs-into-type-errors/6216>\
**Category:** Request Feedback\
**Created:** [August 27, 2020, 11:06am UTC](https://discourse.elm-lang.org/t/make-bugs-into-type-errors/6216 "2020-08-27T11:06:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![choonkeat](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/choonkeat/32/1555_2.png) [@choonkeat](https://discourse.elm-lang.org/u/choonkeat)\
**Post date:** [August 27, 2020, 11:06am UTC](https://discourse.elm-lang.org/t/make-bugs-into-type-errors/6216/1 "2020-08-27T11:06:29Z")

</div>

A few months ago, I asked this question (which I can’t find in Slack anymore):

> > Background: Lifecycle of a RemoteData.WebData
> > 
> > 1. Set value as NotAsked initially
> > 2. When making API call, set value as Loading
> > 3. When API result comes back  
> > a. If error, set value as Failure  
> > b. If success, set value as Success
> 
> When firing off an API call, we need to anticipate which model fields are going to be affected and preemptively set them as `RemoteData.Loading`.
> 
> I find this wiring quite fragile. e.g. if I forget to set `thing = RemoteData.Loading` here, everything compiles. Or if update changes and now this API affects more/fewer fields, how do I remember to make corresponding updates in other places?
> 
> Is there a way to make this more robust?

My coworker [jsegura](https://github.com/jsegura) and I recently stumbled upon this approach while recalling this dissatisfaction. Looking for feedback or better approaches 🙇‍♂️

> **[Make bugs into type errors](https://blog.choonkeat.com/weblog/2020/08/make-bugs-into-type-errors.html)**
>
> It’s very common for frontend apps to fetch remote data to update sections of the UI. For example

---

<div class="post-metadata">

**Author:** ![berend](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/berend/32/2250_2.png) [@berend](https://discourse.elm-lang.org/u/berend)\
**Post date:** [August 28, 2020, 1:21am UTC](https://discourse.elm-lang.org/t/make-bugs-into-type-errors/6216/2 "2020-08-28T01:21:28Z")

</div>

This example is extremely artificial. The `update` for most people is simply:

```auto
 ResponseReceived response -> 
    ( { model | response = response }, Cmd.none )

```

so your artificial situation can’t even occur.

---

<div class="post-metadata">

**Author:** ![choonkeat](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/choonkeat/32/1555_2.png) [@choonkeat](https://discourse.elm-lang.org/u/choonkeat)\
**Post date:** [August 28, 2020, 1:58am UTC](https://discourse.elm-lang.org/t/make-bugs-into-type-errors/6216/3 "2020-08-28T01:58:31Z")

</div>

We use the [remote data pattern](http://blog.jenkster.com/2016/06/how-elm-slays-a-ui-antipattern.html) a lot and sets up the loading indicator when firing off the request

```auto
+ RequestSent param -> 
+ ( {model | response = Loading }, sendRequest param )
  ResponseReceived response -> 
      ( { model | response = response }, Cmd.none )

```

We use [GraphQL](https://github.com/harmboschloo/graphql-to-elm#graphql-to-elm), and it’s not uncommon for 1 http request to be multiple queries; each updating different parts of the model

```auto
  ResponseReceived response -> 
      ( { model | thing = response.thing, categories = response.categories }, Cmd.none )

```

---

<div class="post-metadata">

**Author:** ![berend](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/berend/32/2250_2.png) [@berend](https://discourse.elm-lang.org/u/berend)\
**Post date:** [August 28, 2020, 2:04am UTC](https://discourse.elm-lang.org/t/make-bugs-into-type-errors/6216/4 "2020-08-28T02:04:28Z")

</div>

Didn’t know about that GraphQL library, that’s brilliant. After using [dillonkearns/elm-graphql](https://github.com/dillonkearns/elm-graphql) I was looking for a way to write GraphQL in GraphQL.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [September 7, 2020, 2:04am UTC](https://discourse.elm-lang.org/t/make-bugs-into-type-errors/6216/5 "2020-09-07T02:04:31Z")

</div>

Closed on September 7, 2020, 2:04am UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
