Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding an object with circular dependencies throws an Error. #5

Open
ChristophP opened this issue Sep 17, 2018 · 0 comments
Open

Comments

@ChristophP
Copy link

ChristophP commented Sep 17, 2018

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 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant