Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

refactor(ddns): typed errors replace the token-prose sniff

Juan Denisfbc36831b1a5
diff --git a/backend/app/api/ddns.py b/backend/app/api/ddns.py
index 73ee7c81..926cd0db 100644
--- a/backend/app/api/ddns.py
+++ b/backend/app/api/ddns.py
@@ -74,11 +74,5 @@ def update():
return jsonify({'error': 'token required'}), 401
ip = request.args.get('ip') or body.get('ip') or _client_ip()
- try:
- status, host = DdnsService.update_ip(token, ip)
- except ValueError as e:
- message = str(e)
- code = 401 if 'token' in message.lower() else 400
- return jsonify({'error': message}), code
-
+ status, host = DdnsService.update_ip(token, ip)
return jsonify({'status': status, 'hostname': host.hostname, 'ip': ip})
diff --git a/backend/app/services/ddns_service.py b/backend/app/services/ddns_service.py
index b4fa374f..5df98d68 100644
--- a/backend/app/services/ddns_service.py
+++ b/backend/app/services/ddns_service.py
@@ -2,6 +2,7 @@ import ipaddress
import logging
from datetime import datetime
from app import db
+from app.exceptions import AuthenticationError, ValidationError
from app.models.ddns_host import DdnsHost
from app.models.dns_zone import DNSZone, DNSRecord
from app.services.dns_zone_service import DNSZoneService
@@ -26,7 +27,7 @@ class DdnsService:
zone_id = data.get('zone_id')
zone = DNSZone.query.get(zone_id) if zone_id else None
if not zone:
- raise ValueError('Valid zone_id required')
+ raise ValidationError('Valid zone_id required')
record_name = (data.get('record_name') or '@').strip() or '@'
host = DdnsHost(
@@ -70,10 +71,13 @@ class DdnsService:
configured provider (e.g. Cloudflare) is synced automatically."""
host = DdnsHost.query.filter_by(token=token, enabled=True).first()
if not host:
- raise ValueError('Invalid or disabled token')
+ raise AuthenticationError('Invalid or disabled token')
ip = (ip or '').strip()
- record_type = DdnsService._record_type_for(ip) # validates the IP
+ try:
+ record_type = DdnsService._record_type_for(ip)
+ except ValueError as e:
+ raise ValidationError(f'Invalid IP address: {ip}') from e
record = DNSRecord.query.filter_by(
zone_id=host.zone_id, record_type=record_type, name=host.record_name
diff --git a/backend/tests/STATUS_SNIFFING_CEILING b/backend/tests/STATUS_SNIFFING_CEILING
index 45a4fb75..7f8f011e 100644
--- a/backend/tests/STATUS_SNIFFING_CEILING
+++ b/backend/tests/STATUS_SNIFFING_CEILING
@@ -1 +1 @@
-8
+7
diff --git a/backend/tests/test_ddns.py b/backend/tests/test_ddns.py
index db6efb03..7d2c224c 100644
--- a/backend/tests/test_ddns.py
+++ b/backend/tests/test_ddns.py
@@ -1,6 +1,8 @@
"""Tests for Dynamic DNS — token-authenticated A/AAAA record updates."""
import pytest
+from app.exceptions import AuthenticationError, ValidationError
+
from app import db
from app.models.dns_zone import DNSZone, DNSRecord
from app.services.ddns_service import DdnsService
@@ -42,19 +44,19 @@ class TestDdnsService:
assert DNSRecord.query.filter_by(zone_id=zone.id, record_type='AAAA', name='@').first()
def test_invalid_token_rejected(self, app):
- with pytest.raises(ValueError):
+ with pytest.raises(AuthenticationError):
DdnsService.update_ip('nope', '203.0.113.1')
def test_invalid_ip_rejected(self, app):
zone = _manual_zone('bad.example.com')
host = DdnsService.create_host({'zone_id': zone.id})
- with pytest.raises(ValueError):
+ with pytest.raises(ValidationError):
DdnsService.update_ip(host.token, 'not-an-ip')
def test_disabled_host_rejected(self, app):
zone = _manual_zone('off.example.com')
host = DdnsService.create_host({'zone_id': zone.id, 'enabled': False})
- with pytest.raises(ValueError):
+ with pytest.raises(AuthenticationError):
DdnsService.update_ip(host.token, '203.0.113.1')