Source code for harp.http.errors
import traceback
from functools import cached_property
from typing import Optional
import harp
from multidict import MultiDict, MultiDictProxy
from .typing import BaseHttpMessage
[docs]
class HttpError(BaseHttpMessage):
kind = "error"
[docs]
def __init__(
self,
message: str,
/,
*,
exception: Optional[Exception] = None,
status=500,
verbose_message=None,
):
super().__init__()
self.message = message
self.exception = exception
self.status = status
self.verbose_message = verbose_message or message
[docs]
@cached_property
def body(self) -> bytes:
if self.exception:
msg = str(self.exception)
out = type(self.exception).__name__
if msg:
out += f": {msg}"
if harp.DEBUG:
out += "\n\n" + "".join(traceback.format_exception(self.exception))
return out.encode()
return self.message.encode()