1#-----------------------------------------------------------------------------
2# Copyright (c) 2012 - 2021, Anaconda, Inc., and Bokeh Contributors.
3# All rights reserved.
4#
5# The full license is in the file LICENSE.txt, distributed with this software.
6#-----------------------------------------------------------------------------
7
8#-----------------------------------------------------------------------------
9# Boilerplate
10#-----------------------------------------------------------------------------
11import logging # isort:skip
12log = logging.getLogger(__name__)
13
14#-----------------------------------------------------------------------------
15# Imports
16#-----------------------------------------------------------------------------
17
18# Standard library imports
19import sys
20from traceback import format_exception
21
22# Bokeh imports
23from ..message import Message
24
25#-----------------------------------------------------------------------------
26# Globals and constants
27#-----------------------------------------------------------------------------
28
29__all__ = (
30    'error',
31)
32
33#-----------------------------------------------------------------------------
34# General API
35#-----------------------------------------------------------------------------
36
37#-----------------------------------------------------------------------------
38# Dev API
39#-----------------------------------------------------------------------------
40
41class error(Message):
42    ''' Define the ``ERROR`` message for reporting error conditions back to a
43    Bokeh server.
44
45    The ``content`` fragment of for this message is has the form:
46
47    .. code-block:: python
48
49        {
50            'text'      : <error message text>
51
52            # this is optional
53            'traceback' : <traceback text>
54        }
55
56    '''
57
58    msgtype  = 'ERROR'
59
60    def __repr__(self):
61        msg = super().__repr__()
62        msg += " --- "
63        msg += self.content['text']
64        if "traceback" in self.content:
65            msg += "\n"
66            msg += "".join(self.content['traceback'])
67        return msg
68
69    @classmethod
70    def create(cls, request_id, text, **metadata):
71        ''' Create an ``ERROR`` message
72
73        Args:
74            request_id (str) :
75                The message ID for the message the precipitated the error.
76
77            text (str) :
78                The text of any error message or traceback, etc.
79
80        Any additional keyword arguments will be put into the message
81        ``metadata`` fragment as-is.
82
83        '''
84        header = cls.create_header(request_id=request_id)
85        content = {
86            'text'  : text,
87        }
88        ex_type, ex, tb = sys.exc_info()
89        if ex_type:
90            content['traceback'] = format_exception(ex_type, ex, tb)
91        return cls(header, metadata, content)
92
93#-----------------------------------------------------------------------------
94# Private API
95#-----------------------------------------------------------------------------
96
97#-----------------------------------------------------------------------------
98# Code
99#-----------------------------------------------------------------------------
100