elm / error-message-catalog

A catalog of broken Elm programs / data to improve error messages
BSD 3-Clause "New" or "Revised" License
173 stars 17 forks source link

Undefined Maybe type field in inbound port gives incorrect error message #232

Open janwirth opened 7 years ago

janwirth commented 7 years ago

Summary I have two ports: One outbound and one inbound. They accept the same type essentially. The inbound port takes an array of that type.

Then inbound port was choking on the incoming data.

The fix was to provide null for any undefined field in geolocation or set geolocation to null if itself was undefined.

The error message:

Uncaught Error: Trying to send an unexpected type of value through port `items`:
I ran into the following problems at _[0].geolocation:

Expecting null but instead got: {"coords":{"accuracy":24,"latitude":47.5324616,"longitude":12.2500232},"timestamp":1503248041607}
Expecting an object with a field named `altitude` at _.coords but instead got: {"accuracy":24,"latitude":47.5324616,"longitude":12.2500232}

   (stacktrace from obfuscated code. I use the firebase SDK)
    at send (main.js:13268)
    at main.js:32923
    at main.js:26332
    at fc (main.js:26196)
    at bf (main.js:26261)
    at cf (main.js:26260)
    at Qg.g.Gb (main.js:26351)
    at Ag.g.wd (main.js:26315)
    at og.wd (main.js:26305)
    at Yf.Xf (main.js:26303)

The Type signatures:

type alias JsonItem = {
    description : String,
    image : String,
    price : Int,
    title : String,
    id: String,
    geolocation: Maybe Position
  }
-- JS-API morphic
type alias Position = {
    coords: Coordinates
  , timestamp: Int
}

type alias Coordinates =
  {
    accuracy : Float
  , altitude : Maybe Float
  , altitudeAccuracy : Maybe Float
  , heading : Maybe Float
  , latitude : Float
  , longitude : Float
  , speed : Maybe Float
  }

The ports:

port submit : JsonItem -> Cmd msg

port items : (List JsonItem -> msg) -> Sub msg

Outbound data:

{
  "description": "oaerisneioanrstieoarnst",
  "image": "",
  "price": 0,
  "title": "",
  "id": "",
  "geolocation": {
    "coords": {
      "accuracy": 24,
      "altitude": null,
      "altitudeAccuracy": null,
      "heading": null,
      "latitude": 47.5324616,
      "longitude": 12.2500232,
      "speed": null
    },
    "timestamp": 1503248041607
  }
}

Inbound data

[
  {
    "description": "oaerisneioanrstieoarnst",
    "geolocation": {
      "coords": {
        "accuracy": 24,
        "latitude": 47.5324616,
        "longitude": 12.2500232
      },
      "timestamp": 1503248041607
    },
    "id": "-Ks-ZCuJbk1Alu3EKKCk",
    "image": "",
    "price": 0,
    "title": ""
  }
]