ErrorsΒΆ
The package has two main exceptions: paxb.exceptions.SerializationError
and
paxb.exceptions.DeserializationError
.
paxb.exceptions.DeserializationError
is raised when any deserialization error occurs.
The most common case it is raised is a required element is not found in an xml tree. Look at the example:
>>> import paxb as pb
>>>
>>> @pb.model
... class User:
... name = pb.attribute()
...
>>> xml_str = '<User/>'
>>> pb.from_xml(User, xml_str)
Traceback (most recent call last):
...
paxb.exceptions.DeserializationError: required attribute '/User[1]/name' not found
This error is raised when either of paxb.field()
, paxb.attr()
, paxb.nested()
or paxb.wrapper()
element not found. This behaviour can be altered by passing a default value to an element:
>>> import paxb as pb
>>>
>>> @pb.model
... class User:
... name = pb.attr(default='Alex')
...
>>> xml_str = '<User/>'
>>> pb.from_xml(User, xml_str)
User(name='Alex')
The same applies to paxb.field()
, paxb.nested()
and paxb.wrapper()
.
paxb.exceptions.SerializationError
is raised when any serialization error occurs.
The most common case it is raised is a required element is not set. Look at the example:
>>> import paxb as pb
>>>
>>> @pb.model
... class User:
... name = pb.attr()
...
>>> obj = User(name=None)
>>> pb.to_xml(obj)
Traceback (most recent call last):
...
paxb.exceptions.SerializationError: required attribute 'name' is not set
This error is raised when either of paxb.field()
, paxb.attr()
, paxb.nested()
or paxb.wrapper()
element is not set. This behaviour can be altered by passing a default value to an element:
>>> import paxb as pb
>>>
>>> @pb.model
... class User:
... name = pb.attr(default='Alex')
...
>>> obj = User()
>>> pb.to_xml(obj)
b'<User name="Alex" />'