a [f1#@sdZddlmmZzddlmZWney<eZYn0ddl m Z m Z Gddde Z GdddeZGd d d eZdS) zHTTPError exception class.N) OrderedDict)jsonuric@sNeZdZdZdZdddZddZedd Ze fd d Z d d Z ddZ dS) HTTPErrora Represents a generic HTTP error. Raise an instance or subclass of ``HTTPError`` to have Falcon return a formatted error response and an appropriate HTTP status code to the client when something goes wrong. JSON and XML media types are supported by default. To customize the error presentation, implement a custom error serializer and set it on the :class:`~.API` instance via :meth:`~.API.set_error_serializer`. To customize what data is passed to the serializer, subclass ``HTTPError`` and override the ``to_dict()`` method (``to_json()`` is implemented via ``to_dict()``). To also support XML, override the ``to_xml()`` method. Attributes: status (str): HTTP status line, e.g. '748 Confounded by Ponies'. has_representation (bool): Read-only property that determines whether error details will be serialized when composing the HTTP response. In ``HTTPError`` this property always returns ``True``, but child classes may override it in order to return ``False`` when an empty HTTP body is desired. (See also: :class:`falcon.http_error.NoRepresentation`) title (str): Error title to send to the client. description (str): Description of the error to send to the client. headers (dict): Extra headers to add to the response. link (str): An href that the client can provide to the user for getting help. code (int): An internal application code that a user can reference when requesting support for the error. Args: status (str): HTTP status code and text, such as "400 Bad Request" Keyword Args: title (str): Human-friendly error title. If not provided, defaults to the HTTP status line as determined by the ``status`` argument. description (str): Human-friendly description of the error, along with a helpful suggestion or two (default ``None``). headers (dict or list): A ``dict`` of header names and values to set, or a ``list`` of (*name*, *value*) tuples. Both *name* and *value* must be of type ``str`` or ``StringType``, and only character values 0x00 through 0xFF may be used on platforms that use wide characters. Note: The Content-Type header, if present, will be overridden. If you wish to return custom error messages, you can create your own HTTP error class, and install an error handler to convert it into an appropriate HTTP response for the client Note: Falcon can process a list of ``tuple`` slightly faster than a ``dict``. href (str): A URL someone can visit to find out more information (default ``None``). Unicode characters are percent-encoded. href_text (str): If href is given, use this as the friendly title/description for the link (default 'API documentation for this error'). code (int): An internal code that customers can reference in their support request or to help them when searching for knowledge base articles related to this error (default ``None``). )statustitle descriptionheaderslinkcodeNc Cs`||_|p ||_||_||_||_|rVt}|_|p8d|d<t||d<d|d<nd|_dS)Nz#Documentation related to this errortexthrefhelprel) rrrr r rr rencode) selfrrrr r href_textr r ru/sparta/input/_build_configuration/image_build+validate/lib/bmcenv/lib64/python3.9/site-packages/falcon/http_error.py__init__js    zHTTPError.__init__cCsd|jj|jfS)Nz<%s: %s>) __class____name__rrrrr__repr__szHTTPError.__repr__cCsdS)NTrrrrrhas_representationszHTTPError.has_representationcCsP|}|j|d<|jdur$|j|d<|jdur8|j|d<|jdurL|j|d<|S)aReturn a basic dictionary representing the error. This method can be useful when serializing the error to hash-like media types, such as YAML, JSON, and MessagePack. Args: obj_type: A dict-like type that will be used to store the error information (default ``dict``). Returns: dict: A dictionary populated with the error's title, description, etc. rNrr r )rrr r )robj_typeobjrrrto_dicts       zHTTPError.to_dictcCs|t}tj|ddS)zReturn a pretty-printed JSON representation of the error. Returns: str: A JSON document for the error. F) ensure_ascii)rrrdumps)rrrrrto_jsons zHTTPError.to_jsoncCstd}|jt|d_|jdur4|jt|d_|jdurRt|jt|d_|jdurt|d}dD]}|j|t||_qldtj |d d S) z~Return an XML-encoded representation of the error. Returns: str: An XML document for the error. errorrNrr r )r r rs&zutf-8)encoding) etElementr SubElementr rr strr tostring)r error_element link_elementkeyrrrto_xmls      zHTTPError.to_xml)NNNNNN) r __module__ __qualname____doc__ __slots__rrpropertyrdictrr r+rrrrrsE     rc@seZdZdZeddZdS)NoRepresentationaMixin for ``HTTPError`` child classes that have no representation. This class can be mixed in when inheriting from ``HTTPError``, in order to override the `has_representation` property such that it always returns ``False``. This, in turn, will cause Falcon to return an empty response body to the client. You can use this mixin when defining errors that either should not have a body (as dictated by HTTP standards or common practice), or in the case that a detailed error response may leak information to an attacker. Note: This mixin class must appear before ``HTTPError`` in the base class list when defining the child; otherwise, it will not override the `has_representation` property as expected. cCsdS)NFrrrrrrsz#NoRepresentation.has_representationN)rr,r-r.r0rrrrrr2sr2cs$eZdZdZefddZZS)OptionalRepresentationaMixin for ``HTTPError`` child classes that may have a representation. This class can be mixed in when inheriting from ``HTTPError`` in order to override the `has_representation` property, such that it will return ``False`` when the error instance has no description (i.e., the `description` kwarg was not set). You can use this mixin when defining errors that do not include a body in the HTTP response by default, serializing details only when the web developer provides a description of the error. Note: This mixin class must appear before ``HTTPError`` in the base class list when defining the child; otherwise, it will not override the `has_representation` property as expected. cstt|jduS)N)superr3rrrrrrsz)OptionalRepresentation.has_representation)rr,r-r.r0r __classcell__rrr5rr3sr3)r.xml.etree.ElementTreeetree ElementTreer# collectionsr ImportErrorr1 falcon.utilrr Exceptionrobjectr2r3rrrrs  3