#!/usr/bin/env python3
# Author(s): Toni Sissala
# Copyright 2025 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, unknown, expected, *expecteds):
msg = "Unknown XML root '%s'." % (unknown,)
expected = (expected,) + expecteds
len_expected = len(expected)
if len_expected == 1:
msg += " Expecting '%s'." % (expected[0],)
else:
msg += " Expecting %s." % (' or '.join(["'%s'" % (exp,) for exp in 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:
msgpaths = ', '.join(xpaths)
msg += ' from path ' + msgpaths if len(xpaths) == 1 else ' from paths ' + msgpaths
else:
msg = msg % xpaths
super().__init__(msg)
[docs]
class InvalidContent(MappingError):
"""Attribute found but contains invalid data."""