Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
elm decoder very helpful to debug decoders used in event handlers (thanks @JoelQ on the elm slack)
module Main exposing (main)
import Html exposing (Html, text)
import Json.Decode as Decode exposing (Decoder)
loggingDecoder : Decoder a -> Decoder a
loggingDecoder realDecoder =
Decode.value
|> Decode.andThen
(\value ->
case Decode.decodeValue realDecoder value of
Ok decoded ->
Decode.succeed decoded
Err error ->
Decode.fail <| Debug.log "decode error" error
)
nameDecoder : Decoder String
nameDecoder =
Decode.field "name" Decode.string
json : String
json =
"""
{ "age": 42 }
"""
main : Html msg
main =
case Decode.decodeString (loggingDecoder nameDecoder) json of
Ok name ->
text name
Err _ ->
text "check the logs for the error"
@atomkirk
Copy link

atomkirk commented Aug 12, 2020

loggingDecoder realDecoder =
    D.value
        |> D.andThen
            (\value ->
                case D.decodeValue realDecoder value of
                    Ok decoded ->
                        D.succeed decoded

                    Err error ->
                        D.fail <| D.errorToString <| Debug.log "decode error" error
            )

D.fail <| D.errorToString <| Debug.log "decode error" error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment