1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/errors.py Tue Aug 30 16:51:10 2016 +0200
1.3 @@ -0,0 +1,87 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Error classes.
1.8 +
1.9 +Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
1.10 + 2014, 2016 Paul Boddie <paul@boddie.org.uk>
1.11 +
1.12 +This program is free software; you can redistribute it and/or modify it under
1.13 +the terms of the GNU General Public License as published by the Free Software
1.14 +Foundation; either version 3 of the License, or (at your option) any later
1.15 +version.
1.16 +
1.17 +This program is distributed in the hope that it will be useful, but WITHOUT
1.18 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.19 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.20 +details.
1.21 +
1.22 +You should have received a copy of the GNU General Public License along with
1.23 +this program. If not, see <http://www.gnu.org/licenses/>.
1.24 +"""
1.25 +
1.26 +class ProcessingError(Exception):
1.27 +
1.28 + "A processing error."
1.29 +
1.30 + pass
1.31 +
1.32 +class ProgramError(ProcessingError):
1.33 +
1.34 + "A general program processing error."
1.35 +
1.36 + def __init__(self, message):
1.37 + self.message = message
1.38 +
1.39 + def __repr__(self):
1.40 + return "%s(%r)" % (self.__class__.__name__, self.message)
1.41 +
1.42 + def __str__(self):
1.43 + return self.message
1.44 +
1.45 +class NodeProcessingError(ProcessingError):
1.46 +
1.47 + "A processing error associated with a particular program node."
1.48 +
1.49 + def __init__(self, message, unit_name=None, astnode=None):
1.50 + self.message = message
1.51 + self.unit_name = unit_name
1.52 + self.astnode = astnode
1.53 +
1.54 + def get_lineno(self, node):
1.55 +
1.56 + "Search for line number information associated with 'node'."
1.57 +
1.58 + if node is None:
1.59 + return None
1.60 +
1.61 + lineno = node.lineno
1.62 + if lineno is not None:
1.63 + return lineno
1.64 + else:
1.65 + for child in node.getChildNodes():
1.66 + lineno = self.get_lineno(child)
1.67 + if lineno is not None:
1.68 + return lineno
1.69 + return None
1.70 +
1.71 + def __repr__(self):
1.72 + return "%s(%r, %r, %r)" % (self.__class__.__name__, self.message, self.unit_name, self.astnode)
1.73 +
1.74 + def __str__(self):
1.75 + lineno = self.get_lineno(self.astnode)
1.76 + return "Error in %s%s: %s" % (self.unit_name, lineno and (" at line %s" % lineno) or "", self.message)
1.77 +
1.78 +class InspectError(NodeProcessingError):
1.79 +
1.80 + "An error during the module inspection process."
1.81 +
1.82 + pass
1.83 +
1.84 +class TranslateError(NodeProcessingError):
1.85 +
1.86 + "An error during the module translation process."
1.87 +
1.88 + pass
1.89 +
1.90 +# vim: tabstop=4 expandtab shiftwidth=4