Coverage for mcpgateway / plugins / framework / errors.py: 100%

12 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-02-11 07:10 +0000

1# -*- coding: utf-8 -*- 

2"""Location: ./mcpgateway/plugins/framework/errors.py 

3Copyright 2025 

4SPDX-License-Identifier: Apache-2.0 

5Authors: Teryl Taylor 

6 

7Pydantic models for plugins. 

8This module implements the pydantic models associated with 

9the base plugin layer including configurations, and contexts. 

10""" 

11 

12# First-Party 

13from mcpgateway.plugins.framework.models import PluginErrorModel, PluginViolation 

14 

15 

16class PluginViolationError(Exception): 

17 """A plugin violation error. 

18 

19 Attributes: 

20 violation (PluginViolation): the plugin violation. 

21 message (str): the plugin violation reason. 

22 """ 

23 

24 def __init__(self, message: str, violation: PluginViolation | None = None): 

25 """Initialize a plugin violation error. 

26 

27 Args: 

28 message: the reason for the violation error. 

29 violation: the plugin violation object details. 

30 

31 Examples: 

32 >>> from mcpgateway.plugins.framework.errors import PluginViolationError 

33 >>> from mcpgateway.plugins.framework.models import PluginViolation 

34 >>> v = PluginViolation(reason="r", description="d", code="c") 

35 >>> err = PluginViolationError("blocked", violation=v) 

36 >>> (str(err), err.violation.code) 

37 ('blocked', 'c') 

38 """ 

39 self.message = message 

40 self.violation = violation 

41 super().__init__(self.message) 

42 

43 

44class PluginError(Exception): 

45 """A plugin error object for errors internal to the plugin. 

46 

47 Attributes: 

48 error (PluginErrorModel): the plugin error object. 

49 """ 

50 

51 def __init__(self, error: PluginErrorModel): 

52 """Initialize a plugin violation error. 

53 

54 Args: 

55 error: the plugin error details. 

56 

57 Examples: 

58 >>> from mcpgateway.plugins.framework.errors import PluginError 

59 >>> from mcpgateway.plugins.framework.models import PluginErrorModel 

60 >>> pe = PluginError(PluginErrorModel(message="boom", plugin_name="p1")) 

61 >>> (str(pe), pe.error.plugin_name) 

62 ('boom', 'p1') 

63 """ 

64 self.error = error 

65 super().__init__(self.error.message) 

66 

67 

68def convert_exception_to_error(exception: Exception, plugin_name: str) -> PluginErrorModel: 

69 """Converts an exception object into a PluginErrorModel. Primarily used for external plugin error handling. 

70 

71 Args: 

72 exception: The exception to be converted. 

73 plugin_name: The name of the plugin on which the exception occurred. 

74 

75 Returns: 

76 A plugin error pydantic object that can be sent over HTTP. 

77 

78 Examples: 

79 >>> from mcpgateway.plugins.framework.errors import convert_exception_to_error 

80 >>> err = convert_exception_to_error(ValueError("nope"), plugin_name="p1") 

81 >>> (err.plugin_name, "ValueError('nope')" in err.message) 

82 ('p1', True) 

83 """ 

84 return PluginErrorModel(message=repr(exception), plugin_name=plugin_name)