Skip to content

Commit 8327ea1

Browse files
committed
Make (De)SerializationError a RepositoryError
SerializationError and DeserializationError are both errors coming from the repository side looking from the clients point of view. That's why it makes sense to make them repository errors. Signed-off-by: Martin Vrachev <[email protected]>
1 parent 8fbe24b commit 8327ea1

File tree

2 files changed

+9
-24
lines changed

2 files changed

+9
-24
lines changed

tuf/api/serialization/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
import abc
1818
from typing import TYPE_CHECKING
1919

20+
from tuf.api.exceptions import RepositoryError
21+
2022
if TYPE_CHECKING:
2123
# pylint: disable=cyclic-import
2224
from tuf.api.metadata import Metadata, Signed
2325

2426

25-
# TODO: Should these be in tuf.exceptions or inherit from tuf.exceptions.Error?
26-
class SerializationError(Exception):
27+
class SerializationError(RepositoryError):
2728
"""Error during serialization."""
2829

2930

30-
class DeserializationError(Exception):
31+
class DeserializationError(RepositoryError):
3132
"""Error during deserialization."""
3233

3334

tuf/ngclient/_internal/trusted_metadata_set.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474

7575
from tuf.api import exceptions
7676
from tuf.api.metadata import Metadata, Root, Snapshot, Targets, Timestamp
77-
from tuf.api.serialization import DeserializationError
7877

7978
logger = logging.getLogger(__name__)
8079

@@ -161,10 +160,7 @@ def update_root(self, data: bytes) -> Metadata[Root]:
161160
raise RuntimeError("Cannot update root after timestamp")
162161
logger.debug("Updating root")
163162

164-
try:
165-
new_root = Metadata[Root].from_bytes(data)
166-
except DeserializationError as e:
167-
raise exceptions.RepositoryError("Failed to load root") from e
163+
new_root = Metadata[Root].from_bytes(data)
168164

169165
if new_root.signed.type != Root.type:
170166
raise exceptions.RepositoryError(
@@ -218,10 +214,7 @@ def update_timestamp(self, data: bytes) -> Metadata[Timestamp]:
218214
# No need to check for 5.3.11 (fast forward attack recovery):
219215
# timestamp/snapshot can not yet be loaded at this point
220216

221-
try:
222-
new_timestamp = Metadata[Timestamp].from_bytes(data)
223-
except DeserializationError as e:
224-
raise exceptions.RepositoryError("Failed to load timestamp") from e
217+
new_timestamp = Metadata[Timestamp].from_bytes(data)
225218

226219
if new_timestamp.signed.type != Timestamp.type:
227220
raise exceptions.RepositoryError(
@@ -311,10 +304,7 @@ def update_snapshot(
311304
if not trusted:
312305
snapshot_meta.verify_length_and_hashes(data)
313306

314-
try:
315-
new_snapshot = Metadata[Snapshot].from_bytes(data)
316-
except DeserializationError as e:
317-
raise exceptions.RepositoryError("Failed to load snapshot") from e
307+
new_snapshot = Metadata[Snapshot].from_bytes(data)
318308

319309
if new_snapshot.signed.type != Snapshot.type:
320310
raise exceptions.RepositoryError(
@@ -423,10 +413,7 @@ def update_delegated_targets(
423413

424414
meta.verify_length_and_hashes(data)
425415

426-
try:
427-
new_delegate = Metadata[Targets].from_bytes(data)
428-
except DeserializationError as e:
429-
raise exceptions.RepositoryError("Failed to load snapshot") from e
416+
new_delegate = Metadata[Targets].from_bytes(data)
430417

431418
if new_delegate.signed.type != Targets.type:
432419
raise exceptions.RepositoryError(
@@ -455,10 +442,7 @@ def _load_trusted_root(self, data: bytes) -> None:
455442
Note that an expired initial root is considered valid: expiry is
456443
only checked for the final root in update_timestamp().
457444
"""
458-
try:
459-
new_root = Metadata[Root].from_bytes(data)
460-
except DeserializationError as e:
461-
raise exceptions.RepositoryError("Failed to load root") from e
445+
new_root = Metadata[Root].from_bytes(data)
462446

463447
if new_root.signed.type != Root.type:
464448
raise exceptions.RepositoryError(

0 commit comments

Comments
 (0)