Coverage for python/lsst/daf/butler/_exceptions.py: 81%

43 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-05 10:00 +0000

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28"""Specialized Butler exceptions.""" 

29__all__ = ( 

30 "CalibrationLookupError", 

31 "CollectionCycleError", 

32 "CollectionTypeError", 

33 "DatasetNotFoundError", 

34 "DimensionNameError", 

35 "ButlerUserError", 

36 "DatasetTypeNotSupportedError", 

37 "EmptyQueryResultError", 

38 "MissingDatasetTypeError", 

39 "MissingCollectionError", 

40 "ValidationError", 

41) 

42 

43from ._exceptions_legacy import CollectionError, DataIdError, DatasetTypeError 

44 

45 

46class ButlerUserError(Exception): 

47 """Base class for Butler exceptions that contain a user-facing error 

48 message. 

49 

50 Parameters 

51 ---------- 

52 detail : `str` 

53 Details about the error that occurred. 

54 """ 

55 

56 # When used with Butler server, exceptions inheriting from 

57 # this class will be sent to the client side and re-raised by RemoteButler 

58 # there. Be careful that error messages do not contain security-sensitive 

59 # information. 

60 # 

61 # This should only be used for "expected" errors that occur because of 

62 # errors in user-supplied data passed to Butler methods. It should not be 

63 # used for any issues caused by the Butler configuration file, errors in 

64 # the library code itself or the underlying databases. 

65 # 

66 # When you create a new subclass of this type, add it to the list in 

67 # _USER_ERROR_TYPES below. 

68 

69 error_type: str 

70 """Unique name for this error type, used to identify it when sending 

71 information about the error to the client. 

72 """ 

73 

74 def __init__(self, detail: str): 

75 return super().__init__(detail) 

76 

77 

78class CalibrationLookupError(LookupError, ButlerUserError): 

79 """Exception raised for failures to look up a calibration dataset.""" 

80 

81 error_type = "calibration_lookup" 

82 

83 

84class CollectionCycleError(ValueError, ButlerUserError): 

85 """Raised when an operation would cause a chained collection to be a child 

86 of itself. 

87 """ 

88 

89 error_type = "collection_cycle" 

90 

91 

92class CollectionTypeError(CollectionError, ButlerUserError): 

93 """Exception raised when type of a collection is incorrect.""" 

94 

95 error_type = "collection_type" 

96 

97 

98class DatasetNotFoundError(LookupError, ButlerUserError): 

99 """The requested dataset could not be found.""" 

100 

101 error_type = "dataset_not_found" 

102 

103 

104class DimensionNameError(KeyError, DataIdError, ButlerUserError): 

105 """Exception raised when a dimension specified in a data ID does not exist 

106 or required dimension is not provided. 

107 """ 

108 

109 error_type = "dimension_name" 

110 

111 

112class DimensionValueError(ValueError, ButlerUserError): 

113 """Exception raised for issues with dimension values in a data ID.""" 

114 

115 error_type = "dimension_value" 

116 

117 

118class MissingCollectionError(CollectionError, ButlerUserError): 

119 """Exception raised when an operation attempts to use a collection that 

120 does not exist. 

121 """ 

122 

123 error_type = "missing_collection" 

124 

125 

126class MissingDatasetTypeError(DatasetTypeError, KeyError, ButlerUserError): 

127 """Exception raised when a dataset type does not exist.""" 

128 

129 error_type = "missing_dataset_type" 

130 

131 

132class DatasetTypeNotSupportedError(RuntimeError): 

133 """A `DatasetType` is not handled by this routine. 

134 

135 This can happen in a `Datastore` when a particular `DatasetType` 

136 has no formatters associated with it. 

137 """ 

138 

139 pass 

140 

141 

142class ValidationError(RuntimeError): 

143 """Some sort of validation error has occurred.""" 

144 

145 pass 

146 

147 

148class EmptyQueryResultError(Exception): 

149 """Exception raised when query methods return an empty result and `explain` 

150 flag is set. 

151 

152 Parameters 

153 ---------- 

154 reasons : `list` [`str`] 

155 List of possible reasons for an empty query result. 

156 """ 

157 

158 def __init__(self, reasons: list[str]): 

159 self.reasons = reasons 

160 

161 def __str__(self) -> str: 

162 # There may be multiple reasons, format them into multiple lines. 

163 return "Possible reasons for empty result:\n" + "\n".join(self.reasons) 

164 

165 

166class UnknownButlerUserError(ButlerUserError): 

167 """Raised when the server sends an ``error_type`` for which we don't know 

168 the corresponding exception type. (This may happen if an old version of 

169 the Butler client library connects to a new server). 

170 """ 

171 

172 error_type = "unknown" 

173 

174 

175_USER_ERROR_TYPES: tuple[type[ButlerUserError], ...] = ( 

176 CalibrationLookupError, 

177 CollectionCycleError, 

178 CollectionTypeError, 

179 DimensionNameError, 

180 DimensionValueError, 

181 DatasetNotFoundError, 

182 MissingCollectionError, 

183 MissingDatasetTypeError, 

184 UnknownButlerUserError, 

185) 

186_USER_ERROR_MAPPING = {e.error_type: e for e in _USER_ERROR_TYPES} 

187assert len(_USER_ERROR_MAPPING) == len( 

188 _USER_ERROR_TYPES 

189), "Subclasses of ButlerUserError must have unique 'error_type' property" 

190 

191 

192def create_butler_user_error(error_type: str, message: str) -> ButlerUserError: 

193 """Instantiate one of the subclasses of `ButlerUserError` based on its 

194 ``error_type`` string. 

195 

196 Parameters 

197 ---------- 

198 error_type : `str` 

199 The value from the ``error_type`` class attribute on the exception 

200 subclass you wish to instantiate. 

201 message : `str` 

202 Detailed error message passed to the exception constructor. 

203 """ 

204 cls = _USER_ERROR_MAPPING.get(error_type) 

205 if cls is None: 

206 raise UnknownButlerUserError(f"Unknown exception type '{error_type}': {message}") 

207 return cls(message)