feat: custom exception classes

This commit is contained in:
Daniel Langbein 2023-04-01 10:07:43 +02:00
parent 74b7747a94
commit 76f19a960a
2 changed files with 13 additions and 3 deletions

View File

@ -0,0 +1,6 @@
class UnknownIPException(Exception):
pass
class MultipleRecordsException(Exception):
pass

View File

@ -10,6 +10,8 @@ from typing import Type
import requests import requests
from nc_dnsapi import Client, DNSRecord from nc_dnsapi import Client, DNSRecord
from netcup_dns.exception import UnknownIPException, MultipleRecordsException
def main(): def main():
""" """
@ -49,7 +51,7 @@ def main():
# Lazy: Only determine external IPv6 if an AAAA record shall be updated. # Lazy: Only determine external IPv6 if an AAAA record shall be updated.
destination = external_ipv6() destination = external_ipv6()
else: else:
raise Exception(f'DNS record type {type_} is not supported.') raise ValueError(f'DNS record type {type_} is not supported.')
if update_record_destination(api, domain, hostname, type_, destination=destination): if update_record_destination(api, domain, hostname, type_, destination=destination):
print(f'Set {hostname}.{domain} {type_} record to {destination}') print(f'Set {hostname}.{domain} {type_} record to {destination}')
@ -86,13 +88,14 @@ def get_record(api: Client, domain: str, hostname: str, type_: str) -> DNSRecord
:param hostname: :param hostname:
:param type_: :param type_:
:return: The DNS record identified by `domain`, `hostname` and `type`. :return: The DNS record identified by `domain`, `hostname` and `type`.
:raises MultipleRecordsException:
""" """
records: list[DNSRecord] = api.dns_records(domain) records: list[DNSRecord] = api.dns_records(domain)
record: DNSRecord record: DNSRecord
matches = [record for record in records if record.hostname == hostname and record.type == type_] matches = [record for record in records if record.hostname == hostname and record.type == type_]
if len(matches) != 1: if len(matches) != 1:
raise Exception(f'Expected one DNSRecord for {hostname}.{domain}, but got {len(matches)}') raise MultipleRecordsException(f'Expected one DNSRecord for {hostname}.{domain}, but got {len(matches)}')
return matches[0] return matches[0]
@ -127,6 +130,7 @@ def external_ip(version: Type[ipaddress.IPv4Address | ipaddress.IPv6Address], ti
:param endpoints: List of webservices that return ones public IP IPv4/IPv6 address. :param endpoints: List of webservices that return ones public IP IPv4/IPv6 address.
:argument timeout: Timeout for each webservice in seconds. :argument timeout: Timeout for each webservice in seconds.
:return: Public IPv4/IPv6 address. :return: Public IPv4/IPv6 address.
:raises UnknownIPException:
""" """
if endpoints is None: if endpoints is None:
if version == ipaddress.IPv4Address: if version == ipaddress.IPv4Address:
@ -159,7 +163,7 @@ def external_ip(version: Type[ipaddress.IPv4Address | ipaddress.IPv6Address], ti
# Return IP address as string. # Return IP address as string.
return parsed_ip.exploded return parsed_ip.exploded
raise Exception('Could not determine public IP address.') raise UnknownIPException('Could not determine public IP address.')
@lru_cache(maxsize=None) @lru_cache(maxsize=None)