#!/usr/bin/env python3
# Author(s): Toni Sissala
# Copyright 2019 Finnish Social Science Data Archive FSD / University of Tampere
# Licensed under the EUPL. See LICENSE.txt for full license.
"""Exceptions for mapping package.
"""
[docs]class InvalidMapperParameters(Exception):
"""Raise for invalid configuration of a mapper - Coding error.
For example trying to add attributes to a mapper which does not
support attributes. These errors are coding errors and should be
treated as such.
"""
[docs]class MappingError(Exception):
"""Raise for errors while mapping input - User error.
Subclass to create more precise error classes.
"""
[docs]class ParseError(MappingError):
"""Unable to parse source XML.
:note: Mask ElementTree ParseError so caller
may use MappingError to catch all user-errors
when mapping.
"""
[docs]class UnknownXMLRoot(MappingError):
"""Unknown root element.
"""
def __init__(self, expected=None, unknown=None):
msg = "Unknown XML root"
if unknown:
msg += " %s" % (unknown,)
if expected:
msg += " expecting %s" % (expected,)
super().__init__(msg)
[docs]class MissingRequiredAttribute(MappingError):
"""Source does not contain a required attribute.
"""
def __init__(self, *xpaths, msg=None):
self.xpaths = xpaths
if msg is None:
msg = 'Required element cannot be found'
if xpaths:
if len(xpaths) > 1:
msg += ' from paths %s' % ', '.join(xpaths)
else:
msg += ' from path %s' % xpaths
else:
msg = msg % xpaths
super().__init__(msg)
[docs]class InvalidContent(MappingError):
"""Attribute found but contains invalid data.
"""