Coverage for src / links / exceptions.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-10 22:03 +0200

1from fastapi import HTTPException, status 

2from typing import Optional 

3 

4 

5class LinkException(HTTPException): 

6 def __init__(self, status_code: int, detail: str): 

7 super().__init__(status_code=status_code, detail=detail) 

8 

9 

10class NotUniqueAliasError(LinkException): 

11 """Ошибка при попытке использовать уже занятый alias""" 

12 def __init__(self, alias: str): 

13 super().__init__( 

14 status_code=status.HTTP_400_BAD_REQUEST, 

15 detail=f"Custom alias '{alias}' already exists. Please choose another one." 

16 ) 

17 

18 

19class AliasLengthError(LinkException): 

20 """Ошибка при недопустимой длине alias""" 

21 def __init__(self, alias: str, min_len: int = 5, max_len: int = 15): 

22 super().__init__( 

23 status_code=status.HTTP_400_BAD_REQUEST, 

24 detail=f"Alias '{alias}' must be between {min_len} and {max_len} characters long." 

25 ) 

26 

27 

28class LinkExpiredError(LinkException): 

29 """Ошибка при истечении срока действия ссылки""" 

30 def __init__(self, short_code: str): 

31 super().__init__( 

32 status_code=status.HTTP_410_GONE, 

33 detail=f"Link '{short_code}' has expired and is no longer available." 

34 ) 

35 

36 

37class PermissionDeniedError(LinkException): 

38 """Ошибка при отсутствии прав доступа""" 

39 def __init__(self, action: str = "perform this action"): 

40 super().__init__( 

41 status_code=status.HTTP_403_FORBIDDEN, 

42 detail=f"You don't have permission to {action}." 

43 ) 

44 

45 

46class InvalidURLFormatError(LinkException): 

47 """Ошибка при невалидном формате URL""" 

48 def __init__(self, url: str): 

49 super().__init__( 

50 status_code=status.HTTP_400_BAD_REQUEST, 

51 detail=f"Invalid URL format: '{url}'. Please provide a valid URL starting with http:// or https://" 

52 )