Beginner: how can I debug http get / json decode?

495 views
Skip to first unread message

Ivan Uemlianin

unread,
Sep 1, 2015, 8:53:59 AM9/1/15
to Elm Discuss
Dear All

I am starting to explore Elm.  I have read the tutorials and played with the demos.  Now I am trying to write a http/json client to consume an API.  I thought I'd start with a simple GET, and to base a first module on the random cat gif demo.

Result, and problem, so far: the overall flow seems to work properly, but the get json part seems to be returning an error.  Please can anybody help with:

  (a) Why is it returning an error?

  (b) How can I see what the error is, as it is hidden behind layers of Effect, Task, Maybe, etc.?  For example, is there a convenient way to work with http requests in the repl?

The relevant Elm module is at this gist:

    https://gist.github.com/llaisdy/200c2e770621b42d214f

The module is a copy of RandomCatGif.elm, in example 5 of the Elm architecture tutorial.

Instead of GETting a cat gif, I am GETting a "topic", a json object like this:

  {"id": 5, "name": "Mars", "subtitle": "Fourth Planet", description: "bla bla bla ..."}

The symptom when running the module (with example 5's Main.elm) is that the errorTopic is being shown in the view, leading me to believe that the json decoder in decodeUrl is not working properly.

Any pointers gratefully received.

With thanks and best wishes

Ivan


Peter Damoc

unread,
Sep 1, 2015, 10:19:33 AM9/1/15
to Elm Discuss
I know this is silly but have you tried verifying that the JSON server is indeed giving the JSON you need at the proper address?  
Also, have you tried using Http.getString instead of Http.get for temporary debugging purposes? 

P.S.  the code in the gist is has an error. when you create the emptyTopic/errorTopic you add an extra "False" that's not part of the Topic definition. 

 

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

Ivan Uemlianin

unread,
Sep 1, 2015, 10:52:19 AM9/1/15
to elm-d...@googlegroups.com
Dear Peter

Thanks for you comments.

(a) not silly --- first thing I tested.  Yes, I have the server running while I'm working on the elm.  e.g. curl returns this:

$ curl http://localhost:8080/api/topic/mars
{"id": 5, "name": "Mars", "subtitle": "Fourth Planet", description: "bla bla bla ..."}

(p.s.)  thanks, I've corrected the gist (removed the Falses).

(b)
have you tried using Http.getString instead of Http.get for temporary debugging purposes?

Wouldn't the difficulty be the same?  Both return Task Error Something:

    getString : String -> Task Error String
    get : Decoder value -> String -> Task Error value

If there's an error, how can I see what the error is?  Either --- preferably --- in the repl, or in the module?

Best wishes

Ivan
You received this message because you are subscribed to a topic in the Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elm-discuss/o8r-gvjZjBo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elm-discuss...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

-- 
============================================================
Ivan A. Uemlianin PhD
Llaisdy
Speech Technology Research and Development

                    iv...@llaisdy.com
                        @llaisdy
                         llaisdy.wordpress.com
              github.com/llaisdy
                     www.linkedin.com/in/ivanuemlianin

                        festina lente
============================================================ 

Peter Damoc

unread,
Sep 1, 2015, 11:29:45 AM9/1/15
to Elm Discuss
Hi Ivan

Regarding the .getString, you'll be able to rule out JSON decoding problems.

As for Errors, instead of Task.toMaybe  you can use Task.toResult and unwrap that in the update function. If you haven't played with Results yet, you can take a look at the zip-codes example for unwrapping (starting on line 25)

You will, of course, need to change the signature of the Action type too. :) 

The possible values for Http.Error are here:

Ivan Uemlianin

unread,
Sep 1, 2015, 11:36:09 AM9/1/15
to elm-d...@googlegroups.com
Dear Peter

Thanks, that's very helpful.
Will report back.

Ivan

Amitai Burstein

unread,
Sep 1, 2015, 4:55:41 PM9/1/15
to Elm Discuss
I'm also trying to debug the 5th example but I'm still unable to get Debug to work, so I can go ver each line and see the result it spits out. 

When I "Http.getString (randomUrl topic)" I just see topic: "<internal structure>"  in the console.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages