#-----------------------------------------------------------------------------# Copyright (c) Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsimportsysfromtracebackimportformat_exceptionfromtypingimportAny,TypedDict# Bokeh importsfrom...core.typesimportIDfrom..messageimportMessage#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('error',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------classError(TypedDict):text:strtraceback:str|None
[docs]classerror(Message[Error]):''' Define the ``ERROR`` message for reporting error conditions back to a Bokeh server. The ``content`` fragment of for this message is has the form: .. code-block:: python { 'text' : <error message text> # this is optional 'traceback' : <traceback text> } '''msgtype='ERROR'def__repr__(self)->str:msg=super().__repr__()msg+=" --- "msg+=self.content['text']ifself.content["traceback"]isnotNone:msg+="\n"+self.content['traceback']returnmsg
[docs]@classmethoddefcreate(cls,request_id:ID,text:str,**metadata:Any)->error:''' Create an ``ERROR`` message Args: request_id (str) : The message ID for the message the precipitated the error. text (str) : The text of any error message or traceback, etc. Any additional keyword arguments will be put into the message ``metadata`` fragment as-is. '''header=cls.create_header(request_id=request_id)ex_type,ex,tb=sys.exc_info()traceback="".join(format_exception(ex_type,ex,tb))ifex_typeelseNonecontent=Error(text=text,traceback=traceback)returncls(header,metadata,content)