zig icon indicating copy to clipboard operation
zig copied to clipboard

Better error logging for std.json panics at runtime

Open VisenDev opened this issue 2 years ago • 1 comments

Zig Version

0.11.0

Steps to Reproduce and Observed Output

Usually when std.json fails to parse some json data, it panics and produces an error log something like this:

error: UnknownField
/usr/local/lib/zig/std/json/static.zig:371:25: 0x1061e741c in innerParse__anon_11603 (dev)
                        return error.UnknownField;
                        ^
/usr/local/lib/zig/std/json/static.zig:467:64: 0x1061d3744 in innerParse__anon_11227 (dev)
                                arraylist.appendAssumeCapacity(try innerParse(ptrInfo.child, allocator, source, options));
                                                               ^
/usr/local/lib/zig/std/json/static.zig:140:19: 0x1061b5a35 in parseFromTokenSourceLeaky__anon_10125 (dev)
    const value = try innerParse(T, allocator, scanner_or_reader, resolved_options);
                  ^
/usr/local/lib/zig/std/json/static.zig:107:20: 0x10618e04d in parseFromTokenSource__anon_7661 (dev)
    parsed.value = try parseFromTokenSourceLeaky(T, parsed.arena.allocator(), scanner_or_reader, options);
                   ^
/usr/local/lib/zig/std/json/static.zig:73:5: 0x106187a2d in parseFromSlice__anon_6948 (dev)
    return parseFromTokenSource(T, allocator, &scanner, options);
    ^
/Users/Robert/zig/dev/src/tiles.zig:35:18: 0x106186e3e in loadTiles (dev)
    const data = try json.parseFromSlice([]TileRecord, a, string, .{});
                 ^
/Users/Robert/zig/dev/src/state.zig:21:22: 0x10618815e in init (dev)
            .tiles = try tile.loadTiles(a, textures),
                     ^

or

error: UnexpectedToken
etc...

This is not helpful at all in debugging which part of the json failed to parse and why.

Expected Output

It would be great if std.json would log the line of json it failed to parse, and why it couldn't be parsed into the given struct

For example

ERROR: failed to parse json string
   {"amount": 1.10, "name": "foo"}
into 
  struct {amount: u32, name: []u8,};
  
Expected identifier "amount" to hold u32, found f32.

#etc...

VisenDev avatar Aug 23 '23 18:08 VisenDev

Hi, for anyone stumbling across this, there might be some work already started for this in branch json_diagnostics.

Jiboo avatar May 19 '24 16:05 Jiboo

cc @thejoshwolfe here is the issue to track what you have been working on

to be clear, the OP is incorrect in the sense that this is an error, not a panic, and the suggestion of logging is bad, but this issue can be used to track the need for the ability to pass a diagnostics struct.

andrewrk avatar Jun 07 '24 18:06 andrewrk

~I opened a PR with a checklist~. I'll reopen when it's further along.

thejoshwolfe avatar Jun 07 '24 22:06 thejoshwolfe

to be clear, the OP is incorrect in the sense that this is an error, not a panic,

Yes, I made this issue while I was learning zig and didn't understand the difference

VisenDev avatar Jun 12 '24 01:06 VisenDev