Skip to content

Commit

Permalink
Error handlers and http_state handlers documentation fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
ondratu committed Oct 12, 2024
1 parent 9bebf5e commit a7c6464
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
10 changes: 6 additions & 4 deletions doc/documentation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ You can use headers instead of `content_type` argument.
.. code:: python
@app.http_state(NOT_FOUND)
def not_found(req):
def not_found(req, *_):
return make_response(b'Page not Found',
headers={"Content-Type": "text/plain"},
status_code=NOT_FOUND)
Expand Down Expand Up @@ -522,18 +522,20 @@ or METHOD_HEAD.

HTTP state handlers
~~~~~~~~~~~~~~~~~~~
There are some predefined HTTP state handlers, which is use when other
There are some predefined HTTP state handlers, which are use when other
HTTP state are raised via HTTPException or any other exception which ends with
HTTP_INTERNAL_SERVER_ERROR status code.

You can redefined your own handlers for any combination of status code and
method type like routes handlers. Responsing from these handlers are same as in
method type like routes handlers. Response from these handlers are same as in
route handlers.

Be sure, that some http_state handlers can add other keyword arguments.

.. code:: python
@app.http_state(state.HTTP_NOT_FOUND)
def page_not_found(req):
def page_not_found(req, *_):
return "Your request %s not found." % req.path, "text/plain"
If your http state (error) handler was crashed with error, internal server
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def test_upload(req):


@app.http_state(state.HTTP_NOT_FOUND)
def not_found(req):
def not_found(req, *_):
"""Not found example response."""
buff = (
"<html>",
Expand Down
9 changes: 5 additions & 4 deletions poorwsgi/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ def http_state(self, status_code: int,
.. code:: python
@app.http_state(state.HTTP_NOT_FOUND)
def page_not_found(req):
def page_not_found(req, *_):
return "Your page %s was not found." % req.path, "text/plain"
"""
def wrapper(fun):
Expand Down Expand Up @@ -938,7 +938,8 @@ def error_handler(
.. code:: python
@app.error_handler(ValueError)
def value_error(req):
def value_error(req, error):
log.exception("ValueError %s", error)
return "Values %s are not correct." % req.args, "text/plain"
"""
def wrapper(fun):
Expand Down Expand Up @@ -991,7 +992,7 @@ def state_from_table(self, req: SimpleRequest, status_code: int, **kwargs):
else:
return not_implemented(req, status_code)

def error_from_table(self, req: SimpleRequest, error: Exception, **kwargs):
def error_from_table(self, req: SimpleRequest, error: Exception):
"""Internal method, which is called when exception was raised."""

handler = None
Expand All @@ -1004,7 +1005,7 @@ def error_from_table(self, req: SimpleRequest, error: Exception, **kwargs):
if handler:
try:
req.error_handler = handler
return handler(req, error, **kwargs)
return handler(req, error)

except HTTPException as http_err:
response = http_err.make_response()
Expand Down

0 comments on commit a7c6464

Please sign in to comment.