json
json copied to clipboard
Encoding an object with circular dependencies throws an Error.
When one is passing an object with circular dependencies from JS to Elm and then calls Json.Encode.encode
on the Value
you get a runtime Error from within Elm.
Of course the source of the error is from JS land but it would be nice if Elm gave a better Error message and log something like "<Json with circular references>"
rather than a stack trace. Maybe the JSON.stringify
call could be put in a try/catch or something. It's not a huge issue but I have encountered this error multiple times when people were printing stuff for debugging and it would be helpful if Elm told them the source of their error right away.
SSCCE:
-- Elm
module Main exposing (..)
import Browser
import Html exposing (text)
import Json.Encode as JE
type alias Model =
{}
type Msg
= NoOp
init : JE.Value -> ( Model, Cmd Msg )
init flags =
let _ = JE.encode 2 flags -- will cause an infinite recursion
in
( {}, Cmd.none )
main : Program JE.Value Model Msg
main =
Browser.document
{ view = \_ -> { title="SSCCE", body = [ text "this will crash" ] }
, init = init
, update = \msg model -> (model, Cmd.none)
, subscriptions = always Sub.none
}
// JS
Elm.Main.init({ flags: window });