Reference Guide  2.5.0
errors.py
1 # -----------------------------------------------------------------------------
2 # BSD 3-Clause License
3 #
4 # Copyright (c) 2020-2024, Science and Technology Facilities Council.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 #
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
12 #
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 #
17 # * Neither the name of the copyright holder nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 # -----------------------------------------------------------------------------
34 # Authors R. W. Ford, A. R. Porter, S. Siso and N. Nobre, STFC Daresbury Lab
35 # I. Kavcic and A. J. Voysey, Met Office
36 # J. Henrichs, Bureau of Meteorology
37 # -----------------------------------------------------------------------------
38 
39 ''' This module provides various error classes used in PSyclone'''
40 
41 
42 class LazyString:
43  '''Utility that defers any computation associated with computing a
44  string until the string is required. This is particularly useful
45  for exceptions, where the string will typically not need to be
46  computed unless the program is about to stop.
47 
48  :param function func: a function that computes a string.
49 
50  :raises TypeError: if the func argument is not a function.
51 
52  '''
53  def __init__(self, func):
54  if not hasattr(func, '__call__'):
55  raise TypeError(
56  f"The func argument for the LazyString class should be a "
57  f"function, but found '{type(func).__name__}'.")
58  self._func_func = func
59 
60  def __str__(self):
61  '''
62  :raises TypeError: if the function stored in self._func does \
63  not return a string.
64  '''
65  result = self._func_func()
66  if not isinstance(result, str):
67  raise TypeError(
68  f"The function supplied to the LazyString class should return "
69  f"a string, but found '{type(result).__name__}'.")
70  return result
71 
72 
73 class PSycloneError(Exception):
74  ''' Provides a PSyclone specific error class as a generic parent class for
75  all PSyclone exceptions.
76 
77  :param str value: the message associated with the error.
78 
79  '''
80  def __init__(self, value):
81  Exception.__init__(self, value)
82  self.valuevalue = LazyString(lambda: f"PSyclone Error: {value}")
83 
84  def __repr__(self):
85  return type(self).__name__ + "()"
86 
87  def __str__(self):
88  return str(self.valuevalue)
89 
90 
92  ''' Provides a PSyclone specific error class for errors detected when
93  resolving dependencies in the code.
94 
95  :param str value: the message associated with the error.
96  '''
97  def __init__(self, value):
98  PSycloneError.__init__(self, value)
99  self.valuevaluevalue = "UnresolvedDependencyError: "+str(value)
100 
101 
103  ''' Provides a PSyclone specific error class for errors found during PSy
104  code generation.
105 
106  :param str value: the message associated with the error.
107  '''
108  def __init__(self, value):
109  PSycloneError.__init__(self, value)
110  self.valuevaluevalue = "Generation Error: "+str(value)
111 
112 
114  ''' Provides a PSyclone-specific error class when a field with the
115  requested property/ies is not found.
116 
117  :param str value: the message associated with the error.
118  '''
119  def __init__(self, value):
120  PSycloneError.__init__(self, value)
121  self.valuevaluevalue = "Field not found error: "+str(value)
122 
123 
125  '''
126  PSyclone-specific exception for use when an internal error occurs (i.e.
127  something that 'should not happen').
128 
129  :param str value: the message associated with the error.
130  '''
131  def __init__(self, value):
132  PSycloneError.__init__(self, value)
133  self.valuevaluevalue = "PSyclone internal error: "+str(value)
134 
135 
136 # For Sphinx AutoAPI documentation generation
137 __all__ = ["LazyString", "PSycloneError", "GenerationError",
138  "FieldNotFoundError", "InternalError"]