Coverage for python/lsst/daf/butler/_exceptions.py: 82%
45 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-25 10:24 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-25 10:24 -0700
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/>.
28"""Specialized Butler exceptions."""
29__all__ = (
30 "ButlerUserError",
31 "CalibrationLookupError",
32 "CollectionCycleError",
33 "CollectionTypeError",
34 "DatasetNotFoundError",
35 "DimensionNameError",
36 "DatasetTypeNotSupportedError",
37 "EmptyQueryResultError",
38 "InvalidQueryError",
39 "MissingDatasetTypeError",
40 "MissingCollectionError",
41 "ValidationError",
42)
44from ._exceptions_legacy import CollectionError, DataIdError, DatasetTypeError
47class ButlerUserError(Exception):
48 """Base class for Butler exceptions that contain a user-facing error
49 message.
51 Parameters
52 ----------
53 detail : `str`
54 Details about the error that occurred.
55 """
57 # When used with Butler server, exceptions inheriting from
58 # this class will be sent to the client side and re-raised by RemoteButler
59 # there. Be careful that error messages do not contain security-sensitive
60 # information.
61 #
62 # This should only be used for "expected" errors that occur because of
63 # errors in user-supplied data passed to Butler methods. It should not be
64 # used for any issues caused by the Butler configuration file, errors in
65 # the library code itself or the underlying databases.
66 #
67 # When you create a new subclass of this type, add it to the list in
68 # _USER_ERROR_TYPES below.
70 error_type: str
71 """Unique name for this error type, used to identify it when sending
72 information about the error to the client.
73 """
75 def __init__(self, detail: str):
76 return super().__init__(detail)
79class CalibrationLookupError(LookupError, ButlerUserError):
80 """Exception raised for failures to look up a calibration dataset."""
82 error_type = "calibration_lookup"
85class CollectionCycleError(ValueError, ButlerUserError):
86 """Raised when an operation would cause a chained collection to be a child
87 of itself.
88 """
90 error_type = "collection_cycle"
93class CollectionTypeError(CollectionError, ButlerUserError):
94 """Exception raised when type of a collection is incorrect."""
96 error_type = "collection_type"
99class DatasetNotFoundError(LookupError, ButlerUserError):
100 """The requested dataset could not be found."""
102 error_type = "dataset_not_found"
105class DimensionNameError(KeyError, DataIdError, ButlerUserError):
106 """Exception raised when a dimension specified in a data ID does not exist
107 or required dimension is not provided.
108 """
110 error_type = "dimension_name"
113class DimensionValueError(ValueError, ButlerUserError):
114 """Exception raised for issues with dimension values in a data ID."""
116 error_type = "dimension_value"
119class InvalidQueryError(ButlerUserError):
120 """Exception raised when a query is not valid."""
122 error_type = "invalid_query"
125class MissingCollectionError(CollectionError, ButlerUserError):
126 """Exception raised when an operation attempts to use a collection that
127 does not exist.
128 """
130 error_type = "missing_collection"
133class MissingDatasetTypeError(DatasetTypeError, KeyError, ButlerUserError):
134 """Exception raised when a dataset type does not exist."""
136 error_type = "missing_dataset_type"
139class DatasetTypeNotSupportedError(RuntimeError):
140 """A `DatasetType` is not handled by this routine.
142 This can happen in a `Datastore` when a particular `DatasetType`
143 has no formatters associated with it.
144 """
146 pass
149class ValidationError(RuntimeError):
150 """Some sort of validation error has occurred."""
152 pass
155class EmptyQueryResultError(Exception):
156 """Exception raised when query methods return an empty result and `explain`
157 flag is set.
159 Parameters
160 ----------
161 reasons : `list` [`str`]
162 List of possible reasons for an empty query result.
163 """
165 def __init__(self, reasons: list[str]):
166 self.reasons = reasons
168 def __str__(self) -> str:
169 # There may be multiple reasons, format them into multiple lines.
170 return "Possible reasons for empty result:\n" + "\n".join(self.reasons)
173class UnknownButlerUserError(ButlerUserError):
174 """Raised when the server sends an ``error_type`` for which we don't know
175 the corresponding exception type. (This may happen if an old version of
176 the Butler client library connects to a new server).
177 """
179 error_type = "unknown"
182_USER_ERROR_TYPES: tuple[type[ButlerUserError], ...] = (
183 CalibrationLookupError,
184 CollectionCycleError,
185 CollectionTypeError,
186 DimensionNameError,
187 DimensionValueError,
188 DatasetNotFoundError,
189 InvalidQueryError,
190 MissingCollectionError,
191 MissingDatasetTypeError,
192 UnknownButlerUserError,
193)
194_USER_ERROR_MAPPING = {e.error_type: e for e in _USER_ERROR_TYPES}
195assert len(_USER_ERROR_MAPPING) == len(
196 _USER_ERROR_TYPES
197), "Subclasses of ButlerUserError must have unique 'error_type' property"
200def create_butler_user_error(error_type: str, message: str) -> ButlerUserError:
201 """Instantiate one of the subclasses of `ButlerUserError` based on its
202 ``error_type`` string.
204 Parameters
205 ----------
206 error_type : `str`
207 The value from the ``error_type`` class attribute on the exception
208 subclass you wish to instantiate.
209 message : `str`
210 Detailed error message passed to the exception constructor.
211 """
212 cls = _USER_ERROR_MAPPING.get(error_type)
213 if cls is None:
214 raise UnknownButlerUserError(f"Unknown exception type '{error_type}': {message}")
215 return cls(message)