Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/sigstore/errors.py: 63%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

35 statements  

1# Copyright 2023 The Sigstore Authors 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15""" 

16Exceptions. 

17""" 

18 

19import sys 

20from collections.abc import Mapping 

21from logging import Logger 

22from typing import Any 

23 

24 

25class Error(Exception): 

26 """Base sigstore exception type. Defines helpers for diagnostics.""" 

27 

28 def diagnostics(self) -> str: 

29 """Returns human-friendly error information.""" 

30 

31 return str(self) 

32 

33 def log_and_exit(self, logger: Logger, raise_error: bool = False) -> None: 

34 """Prints all relevant error information to stderr and exits.""" 

35 

36 remind_verbose = ( 

37 "Raising original exception:" 

38 if raise_error 

39 else "For detailed error information, run sigstore with the `--verbose` flag." 

40 ) 

41 

42 logger.error(f"{self.diagnostics()}\n{remind_verbose}") 

43 

44 if raise_error: 

45 # don't want "during handling another exception" 

46 self.__suppress_context__ = True 

47 raise self 

48 

49 sys.exit(1) 

50 

51 

52class NetworkError(Error): 

53 """Raised when a connectivity-related issue occurs.""" 

54 

55 def diagnostics(self) -> str: 

56 """Returns diagnostics for the error.""" 

57 

58 cause_ctx = ( 

59 f""" 

60 Additional context: 

61 

62 {self.__cause__} 

63 """ 

64 if self.__cause__ 

65 else "" 

66 ) 

67 

68 return ( 

69 """\ 

70 A network issue occurred. 

71 

72 Check your internet connection and try again. 

73 """ 

74 + cause_ctx 

75 ) 

76 

77 

78class TUFError(Error): 

79 """Raised when a TUF error occurs.""" 

80 

81 def __init__(self, message: str): 

82 """Constructs a `TUFError`.""" 

83 self.message = message 

84 

85 from tuf.api import exceptions 

86 

87 _details: Mapping[Any, str] = { 

88 exceptions.DownloadError: NetworkError().diagnostics() 

89 } 

90 

91 def diagnostics(self) -> str: 

92 """Returns diagnostics specialized to the wrapped TUF error.""" 

93 details = TUFError._details.get( 

94 type(self.__context__), 

95 "Please report this issue at <https://github.com/sigstore/sigstore-python/issues/new>.", 

96 ) 

97 

98 return f"""\ 

99 {self.message}. 

100 

101 {details} 

102 """ 

103 

104 

105class MetadataError(Error): 

106 """Raised when TUF metadata does not conform to the expected structure.""" 

107 

108 def diagnostics(self) -> str: 

109 """Returns diagnostics for the error.""" 

110 return f"""{self}.""" 

111 

112 

113class RootError(Error): 

114 """Raised when TUF cannot establish its root of trust.""" 

115 

116 def diagnostics(self) -> str: 

117 """Returns diagnostics for the error.""" 

118 return """\ 

119 Unable to establish root of trust. 

120 

121 This error may occur when the resources embedded in this distribution of sigstore-python are out of date.""" 

122 

123 

124class VerificationError(Error): 

125 """ 

126 Raised whenever any phase or subcomponent of Sigstore verification fails. 

127 """