From: Stefan Schantl Date: Wed, 3 Oct 2012 18:32:13 +0000 (+0200) Subject: Add exceptions for database handling and invalid SOA records. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7ceb5debeefdab5772a9ffdf7d67dd9816e438d;p=people%2Fstevee%2Fpypdns.git Add exceptions for database handling and invalid SOA records. Fixes #10232. --- diff --git a/backend.py b/backend.py index 7095eca..3911bef 100644 --- a/backend.py +++ b/backend.py @@ -27,19 +27,28 @@ ############################################################################### import database +import errors +import sqlite3 DB = "/var/lib/pdns/pdns.db" # Create the primary DNS class. class DNS(object): -""" -Primary DNS class. + """ + Primary DNS class. -Uses the database class from imported database module. -Connects to the PDNS sqlite database. -""" + Uses the database class from imported database module. + Connects to the PDNS sqlite database. + """ def __init__(self, db): - self.db = database.Database(db) + # Try to connect to database or raise an exception. + try: + self.db = database.Database(db) + + except sqlite3.OperationalError, e: + raise errors.DatabaseException, "Could not open database: %s" % e + + # Get all configured domains. def get_domains(self): @@ -71,12 +80,12 @@ Connects to the PDNS sqlite database. # Create Domain class. class Domain(object): -""" -Domain class. + """ + Domain class. -Uses query method from database module to get requested information from domain. -The domain is specified by it's unique database id. -""" + Uses query method from database module to get requested information from domain. + The domain is specified by it's unique database id. + """ def __init__(self, dns, domain_id): self.dns = dns self.id = domain_id @@ -161,12 +170,12 @@ The domain is specified by it's unique database id. # Create class for domain records. class Record(object): -""" -Record class + """ + Record class -It is used to get details about configured records. -The domain and record is's are specified by their unique database id's. -""" + It is used to get details about configured records. + The domain and record is's are specified by their unique database id's. + """ def __init__(self, domain, record_id): self.domain = domain self.id = record_id @@ -228,13 +237,13 @@ The domain and record is's are specified by their unique database id's. # Create an own class to deal with "SOA" records. class SOARecord(Record): -""" -SOA Record class. -This is an own class to deal with "SOA" records. + """ + SOA Record class. + This is an own class to deal with "SOA" records. -Uses splitt() to generate a list of the content string from the database. -Returns the requested entries. -""" + Uses splitt() to generate a list of the content string from the database. + Returns the requested entries. + """ def __init__(self, domain, record_id): Record.__init__(self, domain, record_id) @@ -242,8 +251,8 @@ Returns the requested entries. # Check if the content from database is valid (It contains all 7 required information). if not len(self.soa_attrs) == 7: - #XXX Add something like an error message or log output. - pass + raise InvalidRecordDataException, "Your SOA record \ + doesn't contain all required seven elements." # Primary NS - the domain name of the name server that was the original source of the data. @property @@ -284,4 +293,3 @@ Returns the requested entries. class ARecord(Record): pass - diff --git a/database.py b/database.py index 6af90d6..67adea7 100644 --- a/database.py +++ b/database.py @@ -18,12 +18,8 @@ class Database(object): def __init__(self, filename): self.filename = filename - self._db = None - - try: - self.reconnect() - except: - logging.error("Cannot connect to database on %s", self.filename, exc_info=True) + self._db = None + self.reconnect() def __del__(self): self.close() diff --git a/errors.py b/errors.py new file mode 100644 index 0000000..1107707 --- /dev/null +++ b/errors.py @@ -0,0 +1,46 @@ +############################################################################### +# # +# pyPDNS - A PDNS administration tool, written in pure python. # +# Copyright (C) 2012 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +class DatabaseException(Exception): + """ + Exception if the database doesn't exist. + """ + pass + + +class DomainNotFoundException(Exception): + """ + Exception if an invalid domain has been specified. + """ + pass + + +class RecordNotFoundException(Exception): + """ + Exception if an invalid record has been specified. + """ + pass + + +class InvalidRecordDataException(Exception): + """ + Exception if a record contains invalid data. + """ + pass