Coverage for watcher/notifications/exception.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-17 12:22 +0000

1# Licensed under the Apache License, Version 2.0 (the "License"); you may 

2# not use this file except in compliance with the License. You may obtain 

3# a copy of the License at 

4# 

5# http://www.apache.org/licenses/LICENSE-2.0 

6# 

7# Unless required by applicable law or agreed to in writing, software 

8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

10# License for the specific language governing permissions and limitations 

11# under the License. 

12 

13import inspect 

14import sys 

15 

16from watcher.notifications import base as notificationbase 

17from watcher.objects import base 

18from watcher.objects import fields as wfields 

19 

20 

21@base.WatcherObjectRegistry.register_notification 

22class ExceptionPayload(notificationbase.NotificationPayloadBase): 

23 # Version 1.0: Initial version 

24 VERSION = '1.0' 

25 fields = { 

26 'module_name': wfields.StringField(), 

27 'function_name': wfields.StringField(), 

28 'exception': wfields.StringField(), 

29 'exception_message': wfields.StringField() 

30 } 

31 

32 @classmethod 

33 def from_exception(cls, fault=None): 

34 fault = fault or sys.exc_info()[1] 

35 trace = inspect.trace()[-1] 

36 # TODO(gibi): apply strutils.mask_password on exception_message and 

37 # consider emitting the exception_message only if the safe flag is 

38 # true in the exception like in the REST API 

39 return cls( 

40 function_name=trace[3], 

41 module_name=inspect.getmodule(trace[0]).__name__, 

42 exception=fault.__class__.__name__, 

43 exception_message=str(fault)) 

44 

45 

46@notificationbase.notification_sample('infra-optim-exception.json') 

47@base.WatcherObjectRegistry.register_notification 

48class ExceptionNotification(notificationbase.NotificationBase): 

49 # Version 1.0: Initial version 

50 VERSION = '1.0' 

51 fields = { 

52 'payload': wfields.ObjectField('ExceptionPayload') 

53 }