Skip to content
This repository has been archived by the owner on Mar 15, 2020. It is now read-only.

Adding headers to HTTPError #335

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bocadillo/error_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ async def error_to_html(req: Request, res: Response, exc: HTTPError):
html = f"<h1>{exc.title}</h1>"
if exc.detail:
html += f"\n<p>{exc.detail}</p>"
if exc.headers:
html += f"\n<p>{exc.headers}</p>"
res.html = html


Expand All @@ -46,6 +48,7 @@ async def error_to_json(req: Request, res: Response, exc: HTTPError):
```
"""
res.status_code = exc.status_code
res.headers = exc.headers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some of the tests failing in CI come from this line. We shouldn’t set response headers if exc.headers is None. :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh duh its optional, ill change that. well what about adding it to slots?

Copy link
Author

@teaglebuilt teaglebuilt Oct 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__slots__ = (status, detail, headers ) 

Since headers is optional I figured it would not be included

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can certainly add it, yes.

res.json = exc.as_json()


Expand All @@ -65,4 +68,6 @@ async def error_to_text(req: Request, res: Response, exc: HTTPError):
text = exc.title
if exc.detail:
text += f"\n{exc.detail}"
if exc.headers:
text += f"\n{exc.headers}"
res.text = text
8 changes: 7 additions & 1 deletion bocadillo/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ class HTTPError(Exception):
__slots__ = ("_status", "detail")

def __init__(
self, status: typing.Union[int, HTTPStatus], detail: typing.Any = ""
self,
status: typing.Union[int, HTTPStatus],
detail: typing.Any = "",
headers: typing.Dict = None,
):
if isinstance(status, int):
status = HTTPStatus( # pylint: disable=no-value-for-parameter
Expand All @@ -34,6 +37,7 @@ def __init__(
), f"Expected int or HTTPStatus, got {type(status)}"
self._status = status
self.detail = detail
self.headers = headers

@property
def status_code(self) -> int:
Expand All @@ -54,6 +58,8 @@ def as_json(self) -> dict:
data = {"error": self.title, "status": self.status_code}
if self.detail:
data["detail"] = self.detail
if self.headers:
data["headers"] = self.headers
return data

def __str__(self):
Expand Down