Skip to content

Commit eeaddbf

Browse files
committed
#43 - add documentation for sending exception messages to Clients
Signed-off-by: Lance-Drane <Lance-Drane@users.noreply.github.com>
1 parent 37d1bd0 commit eeaddbf

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

docs/core_concepts.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,51 @@ Please see the :doc:`pydantic` page for more information about valid types.
3535

3636
Arguments to the ``@intersect_message()`` decorator can be used to specify specific details about your function; for example, the Content-Types of both the request and response parameter, the data provider for the response data, and whether you want to allow type coercion in the request.
3737

38+
Exception Handling
39+
^^^^^^^^^^^^^^^^^^
40+
41+
By default, if a Capability raises an Exception, unhandled or otherwise, it will be logged server-side but the information will NOT be sent to the Client. To send a more detailed message to the Client, catch error messages yourself and raise ``IntersectCapabilityError`` with your error string as the argument:
42+
43+
Note that you should be very careful about what Exception information you include when you raise ``IntersectCapabilityError``.
44+
45+
Here is an example
46+
47+
.. code-block:: python
48+
49+
from pydantic import BaseModel
50+
from intersect_sdk import IntersectBaseCapabilityImplementation, IntersectCapabilityError
51+
52+
class InputType(BaseModel):
53+
# ...
54+
pass
55+
56+
class OutputType(BaseModel):
57+
# ...
58+
pass
59+
60+
class YourCapability(IntersectBaseCapabilityImplementation):
61+
62+
# assume that the "debug" parameter is a configuration value
63+
def __init__(self, debug: bool):
64+
super().__init__()
65+
self.debug = debug
66+
67+
def _internal_function(self, request: InputType) -> OutputType:
68+
# this function may raise an exception
69+
pass
70+
71+
@intersect_message()
72+
def my_message(self, request: InputType) -> OutputType:
73+
# if "request" is not an integer,
74+
try:
75+
self._internal_function(request)
76+
except (FileNotFoundError, ValueError) as e: # only raising specific kinds of exceptions, let other exceptions propagate (they will be caught by the INTERSECT-SDK)
77+
if self.debug:
78+
# propagate the exception message to the client
79+
raise IntersectCapabilityError(f'An error occurred in my_message: {str(e)}')
80+
# do NOT propagate the exception message to the client
81+
raise
82+
3883
CapabilityImplementation - Events
3984
---------------------------------
4085

0 commit comments

Comments
 (0)