xyncro / chiron

JSON for F#
https://xyncro.tech/chiron
MIT License
175 stars 41 forks source link

Better error message on Json.read failure #10

Closed haf closed 8 years ago

haf commented 9 years ago
        let inline read key =
                fromJson >> Json.ofResult
            =<< Json.getLensPartial (objectKeyPLens key) 

// snip

        let getLensPartial l : Json<_> =
            fun json ->
                match Lens.getPartial l json with
                | Some x -> Value x, json
                | _ -> Error "", json

returns an empty error message; it could possibly re-use they key to provide a nice error message "The property 'someKey' did not exist in the JSON structure".

kolektiv commented 9 years ago

It should definitely return something non-empty! Giving a key might be tricky, as lenses are effectively opaque, so we don't know what element of the object was intended, but I'll come up with something...

haf commented 9 years ago

Perhaps if you throw a typed exception instead of using 'failwith'? Or perhaps expose the error better?

kolektiv commented 9 years ago

Yeah that's a good suggestion, it's hard to give much more useful detail, due to the opacity, but at least a non-generic exception would help. (Saying that, if the error could even be thrown here, tryGetLensPartial is probably a better choice!) But I'll change this, as it's not good enough :)

haf commented 9 years ago

Ping?

kolektiv commented 9 years ago

I'll pick this up and see what we can get in for 5.0 :)

kolektiv commented 8 years ago

There's some excellent new stuff on this in 6.0.0-rc4 onwards (courtesy of @neoeinstein - i'm not taking any credit for that), so I'll close this off.