Make path validation raise ValidationError instead of ValueError

This commit is contained in:
Gergely Polonkai 2022-05-05 08:33:03 +02:00
parent 00eb91e4cd
commit 476e6ad22d
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
2 changed files with 11 additions and 9 deletions

View File

@ -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:

View File

@ -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)