Browse Source

Move warnings to their own module

Raising warnings from an exception module can result in a rather
confusing message:

  Running command: '/usr/bin/nova-scheduler '
  /.../sqlalchemy/enginefacade.py:332: NotSupportedWarning: Configuration option(s) ['use_tpool'] not supported
    exception.NotSupportedWarning

For someone not used to Python's 'warning' infrastructure, this can look
worse than it is, as seen at [1].

While this particular warning was resolved in change I4a5f8958c3e, it's
possible that a similar warning could be raised for other options. Best
to move this out of the 'exception' module and into something more
sensible.

[1] https://bugzilla.redhat.com/show_bug.cgi?id=1639423

Change-Id: I8e0480fb11123067a111ed6aeda6b47614615645
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Stephen Finucane 6 months ago
parent
commit
1f0f5654a7

+ 9
- 24
oslo_db/exception.py View File

@@ -43,10 +43,12 @@ with `try/except` statement. This is required for consistent handling of
43 43
 database errors.
44 44
 """
45 45
 
46
+from debtcollector import moves
47
+from oslo_utils.excutils import CausedByException
46 48
 import six
47 49
 
48 50
 from oslo_db._i18n import _
49
-from oslo_utils.excutils import CausedByException
51
+from oslo_db import warning
50 52
 
51 53
 
52 54
 class DBError(CausedByException):
@@ -299,27 +301,10 @@ class CantStartEngineError(Exception):
299 301
     """Error raised when the enginefacade cannot start up correctly."""
300 302
 
301 303
 
302
-class NotSupportedWarning(Warning):
303
-    """Warn that an argument or call that was passed is not supported.
304
-
305
-    This subclasses Warning so that it can be filtered as a distinct
306
-    category.
307
-
308
-    .. seealso::
309
-
310
-        https://docs.python.org/2/library/warnings.html
311
-
312
-    """
304
+moves.moved_class(warning.NotSupportedWarning,
305
+                  'NotSupportedWarning',
306
+                  __name__, version='Stein')
313 307
 
314
-
315
-class OsloDBDeprecationWarning(DeprecationWarning):
316
-    """Issued per usage of a deprecated API.
317
-
318
-    This subclasses DeprecationWarning so that it can be filtered as a distinct
319
-    category.
320
-
321
-    .. seealso::
322
-
323
-        https://docs.python.org/2/library/warnings.html
324
-
325
-    """
308
+moves.moved_class(warning.OsloDBDeprecationWarning,
309
+                  'OsloDBDeprecationWarning',
310
+                  __name__, version='Stein')

+ 3
- 3
oslo_db/sqlalchemy/enginefacade.py View File

@@ -10,7 +10,6 @@
10 10
 #    License for the specific language governing permissions and limitations
11 11
 #    under the License.
12 12
 
13
-
14 13
 import contextlib
15 14
 import functools
16 15
 import operator
@@ -28,6 +27,7 @@ from oslo_db import options
28 27
 from oslo_db.sqlalchemy import engines
29 28
 from oslo_db.sqlalchemy import orm
30 29
 from oslo_db.sqlalchemy import utils
30
+from oslo_db import warning
31 31
 
32 32
 
33 33
 class _symbol(object):
@@ -334,7 +334,7 @@ class _TransactionFactory(object):
334 334
             warnings.warn(
335 335
                 "Configuration option(s) %r not supported" %
336 336
                 sorted(not_supported),
337
-                exception.NotSupportedWarning
337
+                warning.NotSupportedWarning
338 338
             )
339 339
 
340 340
     def get_legacy_facade(self):
@@ -1253,7 +1253,7 @@ class LegacyEngineFacade(object):
1253 1253
         warnings.warn(
1254 1254
             "EngineFacade is deprecated; please use "
1255 1255
             "oslo_db.sqlalchemy.enginefacade",
1256
-            exception.OsloDBDeprecationWarning,
1256
+            warning.OsloDBDeprecationWarning,
1257 1257
             stacklevel=2)
1258 1258
         if _factory:
1259 1259
             self._factory = _factory

+ 2
- 1
oslo_db/tests/sqlalchemy/test_enginefacade.py View File

@@ -37,6 +37,7 @@ from oslo_db.sqlalchemy import enginefacade
37 37
 from oslo_db.sqlalchemy import engines as oslo_engines
38 38
 from oslo_db.sqlalchemy import orm
39 39
 from oslo_db.tests.sqlalchemy import base as test_base
40
+from oslo_db import warning
40 41
 
41 42
 
42 43
 enginefacade.transaction_context_provider(oslo_context.RequestContext)
@@ -2182,7 +2183,7 @@ class ConfigOptionsTest(oslo_test_base.BaseTestCase):
2182 2183
 
2183 2184
         self.assertEqual(1, len(w))
2184 2185
         self.assertTrue(
2185
-            issubclass(w[-1].category, exception.NotSupportedWarning))
2186
+            issubclass(w[-1].category, warning.NotSupportedWarning))
2186 2187
         self.assertEqual(
2187 2188
             "Configuration option(s) ['fake1', 'wrong2'] not supported",
2188 2189
             str(w[-1].message)

+ 41
- 0
oslo_db/warning.py View File

@@ -0,0 +1,41 @@
1
+# Copyright 2018 Red Hat, Inc.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+# not use this file except in compliance with the License. You may obtain
5
+# 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, WITHOUT
11
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+# License for the specific language governing permissions and limitations
13
+# under the License.
14
+
15
+"""Custom warnings."""
16
+
17
+
18
+class NotSupportedWarning(Warning):
19
+    """Warn that an argument or call that was passed is not supported.
20
+
21
+    This subclasses Warning so that it can be filtered as a distinct
22
+    category.
23
+
24
+    .. seealso::
25
+
26
+        https://docs.python.org/2/library/warnings.html
27
+
28
+    """
29
+
30
+
31
+class OsloDBDeprecationWarning(DeprecationWarning):
32
+    """Issued per usage of a deprecated API.
33
+
34
+    This subclasses DeprecationWarning so that it can be filtered as a distinct
35
+    category.
36
+
37
+    .. seealso::
38
+
39
+        https://docs.python.org/2/library/warnings.html
40
+
41
+    """

Loading…
Cancel
Save