-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Decode.fromString can throw exceptions #42
Comments
Hello @kentcb,
For example, if you write a custom decoder and don't handle the exception at the decoder level, it will bubble to the top and it will be the user responsibility to handle it. That's why most of the decoder use Lines 303 to 310 in 2a70134
Like that they can handle the error path and provide a good/better error message. The only exception that is handled by Lines 97 to 106 in 2a70134
In theory, it should catch all the exception when passing an invalid JSON. Can you please provide a reproduction code? |
Thanks @MangelMaxime. Here is a repro: module Repro
open Xunit
open Thoth.Json.Net
type SomeUnion = SomeUnion of string
let someUnionDecoder = Decode.Auto.generateDecoder<SomeUnion> ()
[<Fact>]
let ``repro`` () =
let good = """["SomeUnion","foo"]"""
let goodDecode = Decode.fromString someUnionDecoder good
let expectedGood = "foo" |> SomeUnion |> Ok
Assert.Equal(goodDecode, expectedGood)
let bad = """"SomeUnion","foo"]"""
let badDecode = Decode.fromString someUnionDecoder bad
let expectedBad = Error "Some kind of message"
Assert.Equal(badDecode, expectedBad) Instead of getting
|
Hello @kentcb, sorry for the delay I was on vacation and wanted to wrap things up on another OSS project of mine. But got sidetracked a bit 😅 So, the error you are having is because Newtonsoft seems to consider your JSON as valid... When it is not: And because of that, the reflection is trying to call the reflection API but with the wrong number of arguments as it can't extract the arguments from the JSON because it is invalid... So for me the error is not in Thoth.Json.Net but in Newtonsoft. I opened an issue about that on their repo: JamesNK/Newtonsoft.Json#2585 I don't know how to capture the error in a clear way to give back a better message. Also, in future version, we will remove Newtonsoft as a dependency so this kind of cryptic error should not happen anymore. |
Hello @kentcb, it happen that this was due to how we consume Newtonsoft. The way we used it, was we were asking it to consomme the JSON "token" per "token" and not as a whole. It has been fixed in version 7.1.0 |
Hi,
I may be misunderstanding
Decode.fromString
, but the implementation and its unsafe counterpart seems to suggest thatfromString
should not throw, ever. However, I am able to get it to throw by giving it some bad data:All I did to reproduce this is leave off a leading
[
from my input string, which is being decoded into a single case DU. Is this a bug in Thoth, or something I'm doing wrong? Obviously I can simply catch this or all exceptions and assume the decoding failed, but I'm wondering why I need to do that whenfromString
already returns aResult<>
.The text was updated successfully, but these errors were encountered: