Commit d5c535da by Guba Sándor Committed by Őry Máté

dashboard: add exception handling

parent 842b98c9
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from lxml import etree as ET from lxml import etree as ET
import logging
rng_file = "/usr/share/libvirt/schemas/domain.rng" rng_file = "/usr/share/libvirt/schemas/domain.rng"
header = """<domain type='kvm'> # Mandatory xml elements dor parsing
<name>validator</name> header = "<domain type='kvm'><name>validator</name>\
<memory unit='KiB'>1024</memory> <memory unit='KiB'>1024</memory>\
<os> <os><type>hvm</type></os>"
<type>hvm</type> footer = "</domain>"
</os>"""
footer = """</domain>""" logger = logging.getLogger()
def domain_validator(value): def domain_validator(value):
xml = header + value + footer xml = header + value + footer
relaxng = ET.RelaxNG(file=rng_file) try:
if not relaxng.validate(ET.fromstring(xml)): parsed_xml = ET.fromstring(xml)
raise ValidationError("%s is not valid libvirt Domain xml." % value) except Exception as e:
raise ValidationError(e.message)
try:
relaxng = ET.RelaxNG(file=rng_file)
except:
logger.critical("%s RelaxNG libvirt domain schema file "
"is missing for validation.", rng_file)
else:
try:
relaxng.assertValid(parsed_xml)
except Exception as e:
raise ValidationError(e.message)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment