waisbrot / yaml2jsonnet

Convert YAML into Jsonnet
GNU Affero General Public License v3.0
28 stars 2 forks source link

Traceback on yaml anchor #5

Open MatthewScholefield opened 2 years ago

MatthewScholefield commented 2 years ago

When parsing a yaml file with an anchor, yaml2jsonnet generates a traceback:

$ cat anchor-example.yaml
foo: &foo-ref
  key-1: "val-1"
  key-2: "val-2"
var: *foo-ref
$ yaml2jsonnet anchor-example.yaml
Traceback (most recent call last):
  File "/home/user/.local/bin/yaml2jsonnet", line 8, in <module>
    sys.exit(main())
  File "/home/user/.local/lib/python3.9/site-packages/yaml2jsonnet/cli.py", line 58, in main
    run(args)
  File "/home/user/.local/lib/python3.9/site-packages/yaml2jsonnet/cli.py", line 47, in run
    convert_yaml(yaml_data, args.out, args.document_comments)
  File "/home/user/.local/lib/python3.9/site-packages/yaml2jsonnet/yaml2jsonnet.py", line 14, in convert_yaml
    JsonnetRenderer(events, output, array, inject_comments).render()
  File "/home/user/.local/lib/python3.9/site-packages/yaml2jsonnet/jsonnet_renderer.py", line 210, in render
    self.state.send(event)
  File "/home/user/.local/lib/python3.9/site-packages/yaml2jsonnet/jsonnet_renderer.py", line 383, in _mapping_value
    raise UnhandledEventError(self, event)
yaml2jsonnet.jsonnet_renderer.UnhandledEventError: Event was not handled by the current state: Last event was AliasEvent(anchor='foo-ref'), the state was _mapping_value, the queue was [(StreamStartEvent,_start), (DocumentStartEvent,_stream), (MappingStartEvent,_document)]

Instead it'd be nice to show an error like "Yaml anchors are not supported. Please remove them and try again."

waisbrot commented 2 years ago

The UnhandledEventError is for unknown unknowns, so to speak. I didn't remember that anchors were a thing and never tested them. If you've got an idea for how to handle generic errors in a friendlier way, though, I'm interested.

The YAML parsing library that I'm using seems to handle anchors (it's not giving an error, it's giving me an event that I'm not handling). Presumably, it'd be pretty easy to add anchors. If I get some time, I'll try adding it.