Skip to content

request for comment: specialized schema parse errors#318

Closed
blackboxsw wants to merge 1 commit intocanonical:mainfrom
blackboxsw:specialized-parse-errors
Closed

request for comment: specialized schema parse errors#318
blackboxsw wants to merge 1 commit intocanonical:mainfrom
blackboxsw:specialized-parse-errors

Conversation

@blackboxsw
Copy link

@blackboxsw blackboxsw commented Jan 26, 2023

Description

@slyon thanks for the jira ping.
Request for comment as to whether this is the right approach to take to get more detailed schema errors (we could alternatively look to unique err.contents.code differences from the C library but we'd also still have to 'unpack' some encoded line/column and path values from the structured error messages in python.

Cloud-int would like to have specialized exception types from libnetplan for schema errors. The specialization needed would be attributes on the Exception raised which point to the offending config file path, the marked error line number and column number.

It seems that all libnetplan errors are generalized from the GERROR raised by the C code. If we can either specialize the err.contents.code for different types or errors, or allow for message format parsing in libnetplan:_checked_lib_call then cloud-init error handling and reporting from tooling can better know how to handle schema errors vs other errors raised by netplan.

mkdir /tmp/netplan
cat > /tmp/new.yaml <<EOF
network: bogus
EOF

cat > validate_bogus_netplan.py <<EOF
from netplan import libnetplan

ROOT_DIR="/tmp/netplan"
def doit():
    parser = libnetplan.Parser()
    parser.load_yaml_hierarchy(ROOT_DIR)
    try:
        parser.load_yaml("/tmp/new.yaml")
    except LibNetplanException as e: # cloud-init wants a LibNetplanSchemaException if possible
        print(dir(e))   # cloud-init doesn't want to parse the structured e.message to tease out line and column info

if __name__ == "__main__":
    doit()
EOF

PYTHONPATH=/usr/share/netplan python3 validate_bogus_netplan.py

Checklist

  • Runs make check successfully.
  • Retains 100% code coverage (make check-coverage).
  • New/changed keys in YAML format are documented.
  • (Optional) Adds example YAML for new feature.
  • (Optional) Closes an open bug in Launchpad.

@blackboxsw blackboxsw marked this pull request as draft January 26, 2023 19:25
raise LibNetplanException(err.contents.message.decode('utf-8'))
exception_cls = LibNetplanException
for error_format, error_type in GERROR_MESSAGE_FORMAT_EXCEPTION_MAP:
if re.match(error_format, err.contents.message.decode('utf-8')):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we could raise different err.contents.code values to represent different exception types because regex matching of err.contents.message format matching could be brittle if message format changes or with internationalization of error messages at some point.

@slyon slyon self-requested a review January 31, 2023 09:21
@slyon slyon added the RFC Request for comment (don't merge yet) label Jan 31, 2023
@slyon slyon mentioned this pull request Apr 12, 2023
5 tasks
Copy link
Contributor

@slyon slyon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I like the idea of this RFC and there's some attempt going on in improving the overall exception handling of Netplan, which this PR should be merged into, IMO (see #334).

Longer term, we should absolutely differentiate the exceptions by error code directly at the origin in libnetplan.so, instead of parsing the error message in libnetplan.py.

@slyon
Copy link
Contributor

slyon commented Apr 24, 2023

Closing this in favor of #334 (which merged most this PR's concepts).

@blackboxsw We'd be glad for your feedback/review on that follow-up PR, when you find some time.

@slyon slyon closed this Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

RFC Request for comment (don't merge yet)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants