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

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

36 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, NoReturn 

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) -> NoReturn: 

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 check any Sigstore instance related arguments and consider " 

96 "reporting the issue at <https://github.com/sigstore/sigstore-python/issues/new>.", 

97 ) 

98 

99 return f"""\ 

100 {self.message}. 

101 

102 {details} 

103 """ 

104 

105 

106class MetadataError(Error): 

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

108 

109 def diagnostics(self) -> str: 

110 """Returns diagnostics for the error.""" 

111 return f"""{self}.""" 

112 

113 

114class RootError(Error): 

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

116 

117 def diagnostics(self) -> str: 

118 """Returns diagnostics for the error.""" 

119 return """\ 

120 Unable to establish root of trust. 

121 

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

123 

124 

125class VerificationError(Error): 

126 """ 

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

128 """ 

129 

130 

131class CertValidationError(VerificationError): 

132 """ 

133 Raised when a TSA certificate chain fails to validate during Sigstore verification. 

134 

135 This is used by CLI to hint that an incorrect Sigstore instance may have been used 

136 """