1 #!/usr/bin/env python 2 3 """ 4 Error classes. 5 6 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 7 2014, 2016 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 """ 22 23 class ProcessingError(Exception): 24 25 "A processing error." 26 27 pass 28 29 class ProgramError(ProcessingError): 30 31 "A general program processing error." 32 33 def __init__(self, message): 34 self.message = message 35 36 def __repr__(self): 37 return "%s(%r)" % (self.__class__.__name__, self.message) 38 39 def __str__(self): 40 return self.message 41 42 class NodeProcessingError(ProcessingError): 43 44 "A processing error associated with a particular program node." 45 46 def __init__(self, message, unit_name=None, astnode=None): 47 self.message = message 48 self.unit_name = unit_name 49 self.astnode = astnode 50 51 def get_lineno(self, node): 52 53 "Search for line number information associated with 'node'." 54 55 if node is None: 56 return None 57 58 lineno = node.lineno 59 if lineno is not None: 60 return lineno 61 else: 62 for child in node.getChildNodes(): 63 lineno = self.get_lineno(child) 64 if lineno is not None: 65 return lineno 66 return None 67 68 def __repr__(self): 69 return "%s(%r, %r, %r)" % (self.__class__.__name__, self.message, self.unit_name, self.astnode) 70 71 def __str__(self): 72 lineno = self.get_lineno(self.astnode) 73 return "Error in %s%s: %s" % (self.unit_name, lineno and (" at line %s" % lineno) or "", self.message) 74 75 class InspectError(NodeProcessingError): 76 77 "An error during the module inspection process." 78 79 pass 80 81 class TranslateError(NodeProcessingError): 82 83 "An error during the module translation process." 84 85 pass 86 87 # vim: tabstop=4 expandtab shiftwidth=4