pytorch-lightning
101 строка · 3.3 Кб
1# Copyright The Lightning AI team.
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
15from json import JSONDecodeError, loads16from typing import Any17
18from click import ClickException, Context, Group19from lightning_cloud.openapi.rest import ApiException20
21
22class _ApiExceptionHandler(Group):23"""Attempts to convert ApiExceptions to ClickExceptions.24
25This process clarifies the error for the user by:
261. Showing the error message from the lightning.ai servers,
27instead of showing the entire HTTP response
282. Suppressing long tracebacks
29
30However, if the ApiException cannot be decoded, or is not
31a 4xx error, the original ApiException will be re-raised.
32
33"""
34
35def invoke(self, ctx: Context) -> Any:36try:37return super().invoke(ctx)38except ApiException as api:39exception_messages = []40if 400 <= api.status < 500:41try:42body = loads(api.body)43except JSONDecodeError:44raise api45exception_messages.append(body["message"])46exception_messages.extend(body["details"])47else:48raise api49raise ClickException("\n".join(exception_messages))50
51
52class MisconfigurationException(Exception):53"""Exception used to inform users of misuse with Lightning."""54
55
56class CacheMissException(Exception):57"""Exception used internally as a boundary to non-executed functions."""58
59
60class ExitAppException(Exception):61"""Exception used by components to signal that App should exit."""62
63
64class LightningComponentException(Exception):65"""Exception used to inform users of misuse with LightningComponent."""66
67
68class InvalidPathException(Exception):69"""Exception used to inform users they are accessing an invalid path."""70
71
72class LightningFlowException(Exception):73"""Exception used to inform users of misuse with LightningFlow."""74
75
76class LightningWorkException(Exception):77"""Exception used to inform users of misuse with LightningWork."""78
79
80class LightningPlatformException(Exception): # pragma: no cover81"""Exception used to inform users of issues related to platform the LightningApp is running on.82
83It gets raised by the Lightning Launcher on the platform side when the app is running in the cloud, and is useful
84when framework or user code needs to catch exceptions specific to the platform, e.g., when resources exceed quotas.
85
86"""
87
88
89class LightningAppStateException(Exception):90"""Exception to inform users of app state errors."""91
92
93class LightningSigtermStateException(Exception):94"""Exception to propagate exception in work proxy."""95
96def __init__(self, exit_code):97self.exit_code = exit_code98
99
100class LogLinesLimitExceeded(Exception):101"""Exception to inform the user that we've reached the maximum number of log lines."""102