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(image-updates): typed errors replace the not-found prose sniff

Juan Denis05c61dd271fa
diff --git a/backend/app/api/image_updates.py b/backend/app/api/image_updates.py
index fa6fa157..a0c86a1c 100644
--- a/backend/app/api/image_updates.py
+++ b/backend/app/api/image_updates.py
@@ -21,11 +21,7 @@ def check(app_id):
"""Run a registry-digest comparison for the application's image now."""
if not _admin():
return jsonify({'error': 'Admin access required'}), 403
- result = ImageUpdateService.check_application(app_id)
- if not result['success']:
- code = 404 if 'not found' in result['error'].lower() else 400
- return jsonify({'error': result['error']}), code
- return jsonify(result['check'])
+ return jsonify(ImageUpdateService.check_application(app_id))
@image_updates_bp.route('/applications/<int:app_id>', methods=['GET'])
diff --git a/backend/app/services/image_update_service.py b/backend/app/services/image_update_service.py
index e3a39232..1b491891 100644
--- a/backend/app/services/image_update_service.py
+++ b/backend/app/services/image_update_service.py
@@ -5,6 +5,7 @@ import logging
from datetime import datetime
from app import db
+from app.exceptions import NotFoundError, ValidationError
from app.models import Application
from app.models.image_update import ImageUpdateCheck
from app.services.docker_service import DockerService
@@ -54,10 +55,10 @@ class ImageUpdateService:
# (and a `docker login` when a private registry is bound).
app = Application.query_active().filter_by(id=application_id).first()
if not app:
- return {'success': False, 'error': 'Application not found'}
+ raise NotFoundError('Application not found')
image_ref = app.docker_image
if not image_ref:
- return {'success': False, 'error': 'Application has no Docker image'}
+ raise ValidationError('Application has no Docker image')
check = ImageUpdateCheck(application_id=application_id, image_ref=image_ref, status='pending')
db.session.add(check)
@@ -82,7 +83,7 @@ class ImageUpdateService:
db.session.commit()
logger.info('Image-update check %s: %s (local=%s remote=%s)',
image_ref, check.status, local, remote)
- return {'success': True, 'check': check.to_dict()}
+ return check.to_dict()
@classmethod
def latest_check(cls, application_id):
diff --git a/backend/tests/STATUS_SNIFFING_CEILING b/backend/tests/STATUS_SNIFFING_CEILING
index f599e28b..ec635144 100644
--- a/backend/tests/STATUS_SNIFFING_CEILING
+++ b/backend/tests/STATUS_SNIFFING_CEILING
@@ -1 +1 @@
-10
+9
diff --git a/backend/tests/test_image_update.py b/backend/tests/test_image_update.py
index 907667b3..d1eefd30 100644
--- a/backend/tests/test_image_update.py
+++ b/backend/tests/test_image_update.py
@@ -1,4 +1,5 @@
"""Tests for image-digest update detection."""
+import pytest
import uuid
from app import db
@@ -26,9 +27,7 @@ class TestImageUpdateService:
a = _seed_app()
monkeypatch.setattr(ImageUpdateService, '_local_digest', lambda ref: 'sha256:aaa')
monkeypatch.setattr(ImageUpdateService, '_registry_digest', lambda ref: 'sha256:bbb')
- result = ImageUpdateService.check_application(a.id)
- assert result['success']
- chk = result['check']
+ chk = ImageUpdateService.check_application(a.id)
assert chk['status'] == 'completed'
assert chk['update_available'] is True
assert chk['current_digest'] == 'sha256:aaa'
@@ -38,7 +37,7 @@ class TestImageUpdateService:
a = _seed_app()
monkeypatch.setattr(ImageUpdateService, '_local_digest', lambda ref: 'sha256:same')
monkeypatch.setattr(ImageUpdateService, '_registry_digest', lambda ref: 'sha256:same')
- chk = ImageUpdateService.check_application(a.id)['check']
+ chk = ImageUpdateService.check_application(a.id)
assert chk['status'] == 'completed'
assert chk['update_available'] is False
@@ -46,14 +45,15 @@ class TestImageUpdateService:
a = _seed_app()
monkeypatch.setattr(ImageUpdateService, '_local_digest', lambda ref: None)
monkeypatch.setattr(ImageUpdateService, '_registry_digest', lambda ref: 'sha256:bbb')
- chk = ImageUpdateService.check_application(a.id)['check']
+ chk = ImageUpdateService.check_application(a.id)
assert chk['status'] == 'failed'
assert chk['update_available'] is False
- def test_no_image_returns_error(self, app):
+ def test_no_image_raises_typed_error(self, app):
+ from app.exceptions import ValidationError
a = _seed_app(image=None)
- result = ImageUpdateService.check_application(a.id)
- assert result['success'] is False
+ with pytest.raises(ValidationError):
+ ImageUpdateService.check_application(a.id)
def test_badge_present_in_application_to_dict(self, app, monkeypatch):
a = _seed_app()