xref: /dpdk/dts/framework/exception.py (revision ad80f550dbc5896c7ce4025c3fa344527fa4cea5)
1# SPDX-License-Identifier: BSD-3-Clause
2# Copyright(c) 2010-2014 Intel Corporation
3# Copyright(c) 2022-2023 PANTHEON.tech s.r.o.
4# Copyright(c) 2022-2023 University of New Hampshire
5
6"""
7User-defined exceptions used across the framework.
8"""
9
10from enum import IntEnum, unique
11from typing import ClassVar
12
13
14@unique
15class ErrorSeverity(IntEnum):
16    """
17    The severity of errors that occur during DTS execution.
18    All exceptions are caught and the most severe error is used as return code.
19    """
20
21    NO_ERR = 0
22    GENERIC_ERR = 1
23    CONFIG_ERR = 2
24    REMOTE_CMD_EXEC_ERR = 3
25    SSH_ERR = 4
26
27
28class DTSError(Exception):
29    """
30    The base exception from which all DTS exceptions are derived.
31    Stores error severity.
32    """
33
34    severity: ClassVar[ErrorSeverity] = ErrorSeverity.GENERIC_ERR
35
36
37class SSHTimeoutError(DTSError):
38    """
39    Command execution timeout.
40    """
41
42    command: str
43    output: str
44    severity: ClassVar[ErrorSeverity] = ErrorSeverity.SSH_ERR
45
46    def __init__(self, command: str, output: str):
47        self.command = command
48        self.output = output
49
50    def __str__(self) -> str:
51        return f"TIMEOUT on {self.command}"
52
53    def get_output(self) -> str:
54        return self.output
55
56
57class SSHConnectionError(DTSError):
58    """
59    SSH connection error.
60    """
61
62    host: str
63    severity: ClassVar[ErrorSeverity] = ErrorSeverity.SSH_ERR
64
65    def __init__(self, host: str):
66        self.host = host
67
68    def __str__(self) -> str:
69        return f"Error trying to connect with {self.host}"
70
71
72class SSHSessionDeadError(DTSError):
73    """
74    SSH session is not alive.
75    It can no longer be used.
76    """
77
78    host: str
79    severity: ClassVar[ErrorSeverity] = ErrorSeverity.SSH_ERR
80
81    def __init__(self, host: str):
82        self.host = host
83
84    def __str__(self) -> str:
85        return f"SSH session with {self.host} has died"
86
87
88class ConfigurationError(DTSError):
89    """
90    Raised when an invalid configuration is encountered.
91    """
92
93    severity: ClassVar[ErrorSeverity] = ErrorSeverity.CONFIG_ERR
94
95
96class RemoteCommandExecutionError(DTSError):
97    """
98    Raised when a command executed on a Node returns a non-zero exit status.
99    """
100
101    command: str
102    command_return_code: int
103    severity: ClassVar[ErrorSeverity] = ErrorSeverity.REMOTE_CMD_EXEC_ERR
104
105    def __init__(self, command: str, command_return_code: int):
106        self.command = command
107        self.command_return_code = command_return_code
108
109    def __str__(self) -> str:
110        return (
111            f"Command {self.command} returned a non-zero exit code: "
112            f"{self.command_return_code}"
113        )
114