]> git.ipfire.org Git - people/stevee/pypdns.git/commitdiff
Add exceptions for database handling and invalid SOA records.
authorStefan Schantl <stefan.schantl@ipfire.org>
Wed, 3 Oct 2012 18:32:13 +0000 (20:32 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Wed, 3 Oct 2012 18:32:13 +0000 (20:32 +0200)
Fixes #10232.

backend.py
database.py
errors.py [new file with mode: 0644]

index 7095eca8e3de12f338a738f2a31c841c6c1cc6d1..3911bef87557018deabba485ba9c7b73b2fcfc9b 100644 (file)
 ###############################################################################
 
 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
-
index 6af90d6c058e6ebd6c6fe70922cf45e674c2fc81..67adea7e5b0dccad26e2a2b6d682ba014e8ef08a 100644 (file)
@@ -18,12 +18,8 @@ class Database(object):
        def __init__(self, filename):\r
                self.filename = filename\r
 \r
-               self._db = None\r
-\r
-               try:\r
-                       self.reconnect()\r
-               except:\r
-                       logging.error("Cannot connect to database on %s", self.filename, exc_info=True)\r
+               self._db = None \r
+               self.reconnect()\r
 \r
        def __del__(self):\r
                self.close()\r
diff --git a/errors.py b/errors.py
new file mode 100644 (file)
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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+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