1# coding=utf-8
2# Copyright (c) 2015, 2019 Intel Corporation
3
4# Permission is hereby granted, free of charge, to any person obtaining a copy
5# of this software and associated documentation files (the "Software"), to deal
6# in the Software without restriction, including without limitation the rights
7# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8# copies of the Software, and to permit persons to whom the Software is
9# furnished to do so, subject to the following conditions:
10
11# The above copyright notice and this permission notice shall be included in
12# all copies or substantial portions of the Software.
13
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20# SOFTWARE.
21
22"""Exception and error classes for piglit, and exception handlers."""
23
24import sys
25import functools
26
27__all__ = [
28    'PiglitInternalError',
29    'PiglitFatalError',
30    'PiglitException',
31    'PiglitAbort',
32    'handler',
33]
34
35
36
37def handler(func):
38    """Decorator function for handling errors in an entry point.
39
40    This will handle expected errors (PiglitFatalError), and unexpected errors,
41    either PiglitInternalErrors or PiglitExceptions, as well as handling
42    generic Exceptions
43
44    """
45
46    @functools.wraps(func)
47    def _inner(*args, **kwargs):
48        try:
49            func(*args, **kwargs)
50        except PiglitFatalError as e:
51            print('Fatal Error: {}'.format(str(e)), file=sys.stderr)
52            sys.exit(e.exitcode)
53        except PiglitAbort as e:
54            print('Aborting Piglit execution: {}'.format(str(e)),
55                  file=sys.stderr)
56            sys.exit(e.exitcode)
57        except PiglitUserError as e:
58            print('User error: {}'.format(str(e)), file=sys.stderr)
59            sys.exit(e.exitcode)
60
61    return _inner
62
63
64class PiglitBaseException(Exception):
65
66    """Base class for all piglit exceptions."""
67
68    def __init__(self, *args, exitcode=1, **kwargs):
69        super().__init__(*args, **kwargs)
70        self.exitcode = exitcode
71
72
73class PiglitException(PiglitBaseException):
74    """Class for non-error exceptions.
75
76    These should *always* be caught. If this class (or any subclass) is
77    uncaught that is a bug in piglit.
78
79    """
80
81    def __str__(self):
82        return ('An internal exception that should have been handled was not:'
83                '\n{}'.format(super(PiglitException, self).__str__()))
84
85
86class PiglitInternalError(PiglitBaseException):
87    """Class for errors in piglit.
88
89    These should always be handled.
90
91    """
92
93    def __str__(self):
94        return 'An internal error occurred:\n{}'.format(
95            super(PiglitInternalError, self).__str__())
96
97
98class PiglitFatalError(PiglitBaseException):
99    """Class for errors in piglit that cannot be recovered from.
100
101    When this class (or a subclass) is raised it should be raised all the way
102    to the top of the program where it exits.
103
104    """
105
106
107class PiglitUserError(PiglitBaseException):
108    """Class for user configuration errors.
109
110    When this class (or a subclass) is raised it should be raised all the way
111    to the top of the program where it exits.
112    """
113
114
115class PiglitAbort(PiglitBaseException):
116    """Class for non-errors that require piglit aborting.
117
118    When this class (or a subclass) is raised it should be raised all the way
119    to the top of the program where it exits.
120
121    """
122    def __init__(self, *args, exitcode=3, **kwargs):
123        super().__init__(*args, exitcode=exitcode, **kwargs)
124