From 476e6ad22d2776379137320e3576657c7d12923a Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 5 May 2022 08:33:03 +0200 Subject: [PATCH] Make path validation raise ValidationError instead of ValueError --- earthsnake/path.py | 15 ++++++++------- tests/test_path.py | 5 +++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/earthsnake/path.py b/earthsnake/path.py index ba2d608..d01236c 100644 --- a/earthsnake/path.py +++ b/earthsnake/path.py @@ -1,5 +1,6 @@ """Path handling""" +from .exc import ValidationError from .identity import Identity from .types import ALPHA_LOWER, ALPHA_UPPER, DIGIT @@ -22,25 +23,25 @@ class Path: """Validate a path""" if not 2 <= len(path) <= 512: - raise ValueError('Path length must be between 2 and 512') + raise ValidationError('Path length must be between 2 and 512') if not path.startswith('/'): - raise ValueError('Paths must start with a /') + raise ValidationError('Paths must start with a /') if path.endswith('/'): - raise ValueError('Paths must not end with a /') + raise ValidationError('Paths must not end with a /') if path.startswith('/@'): - raise ValueError('Paths must not start with /@') + raise ValidationError('Paths must not start with /@') if '//' in path: - raise ValueError('Paths must not contain //') + raise ValidationError('Paths must not contain //') if path.count('!') > 1: - raise ValueError('Only one ! is allowed in paths') + raise ValidationError('Only one ! is allowed in paths') if '!' in path and not allow_ephemeral: - raise ValueError('Only ephemeral paths may contain !') + raise ValidationError('Only ephemeral paths may contain !') @property def is_shared(self) -> bool: diff --git a/tests/test_path.py b/tests/test_path.py index 89f2531..20ed47b 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -3,6 +3,7 @@ import pytest +from earthsnake.exc import ValidationError from earthsnake.identity import Identity from earthsnake.path import Path @@ -22,7 +23,7 @@ from earthsnake.path import Path def test_invalid(path: str, error_msg: str) -> None: """Test if validation fails for invalid paths""" - with pytest.raises(ValueError) as ctx: + with pytest.raises(ValidationError) as ctx: Path.validate(path) assert error_msg in str(ctx.value) @@ -31,7 +32,7 @@ def test_invalid(path: str, error_msg: str) -> None: def test_invalid_non_ephemeral() -> None: """Test validating ephemeral paths when ephemeral is not allowed""" - with pytest.raises(ValueError) as ctx: + with pytest.raises(ValidationError) as ctx: Path.validate('/ephemeral!') assert 'Only ephemeral paths may contain !' in str(ctx.value)