pytorch-lightning

Форк
0
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

15
from json import JSONDecodeError, loads
16
from typing import Any
17

18
from click import ClickException, Context, Group
19
from lightning_cloud.openapi.rest import ApiException
20

21

22
class _ApiExceptionHandler(Group):
23
    """Attempts to convert ApiExceptions to ClickExceptions.
24

25
    This process clarifies the error for the user by:
26
    1. Showing the error message from the lightning.ai servers,
27
       instead of showing the entire HTTP response
28
    2. Suppressing long tracebacks
29

30
    However, if the ApiException cannot be decoded, or is not
31
    a 4xx error, the original ApiException will be re-raised.
32

33
    """
34

35
    def invoke(self, ctx: Context) -> Any:
36
        try:
37
            return super().invoke(ctx)
38
        except ApiException as api:
39
            exception_messages = []
40
            if 400 <= api.status < 500:
41
                try:
42
                    body = loads(api.body)
43
                except JSONDecodeError:
44
                    raise api
45
                exception_messages.append(body["message"])
46
                exception_messages.extend(body["details"])
47
            else:
48
                raise api
49
            raise ClickException("\n".join(exception_messages))
50

51

52
class MisconfigurationException(Exception):
53
    """Exception used to inform users of misuse with Lightning."""
54

55

56
class CacheMissException(Exception):
57
    """Exception used internally as a boundary to non-executed functions."""
58

59

60
class ExitAppException(Exception):
61
    """Exception used by components to signal that App should exit."""
62

63

64
class LightningComponentException(Exception):
65
    """Exception used to inform users of misuse with LightningComponent."""
66

67

68
class InvalidPathException(Exception):
69
    """Exception used to inform users they are accessing an invalid path."""
70

71

72
class LightningFlowException(Exception):
73
    """Exception used to inform users of misuse with LightningFlow."""
74

75

76
class LightningWorkException(Exception):
77
    """Exception used to inform users of misuse with LightningWork."""
78

79

80
class LightningPlatformException(Exception):  # pragma: no cover
81
    """Exception used to inform users of issues related to platform the LightningApp is running on.
82

83
    It gets raised by the Lightning Launcher on the platform side when the app is running in the cloud, and is useful
84
    when framework or user code needs to catch exceptions specific to the platform, e.g., when resources exceed quotas.
85

86
    """
87

88

89
class LightningAppStateException(Exception):
90
    """Exception to inform users of app state errors."""
91

92

93
class LightningSigtermStateException(Exception):
94
    """Exception to propagate exception in work proxy."""
95

96
    def __init__(self, exit_code):
97
        self.exit_code = exit_code
98

99

100
class LogLinesLimitExceeded(Exception):
101
    """Exception to inform the user that we've reached the maximum number of log lines."""
102

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.