Adds handling for closed transports in the server for `write_response`
as well as `write_error`, letting it all flow to `bail_out` seemed to be
a little light handed in terms of telling the logs where the
error actually occured.
Handling to fix the infinite `write_error` loop is still there and those
exceptions will get reported on in the debug logs.
When user specifies HTTP methods to function handlers, it automatically
will be overloaded unless they duplicate.
Example:
# This is a new route. It works as before.
@app.route('/overload', methods=['GET'])
async def handler1(request):
return text('OK1')
# This is the exiting route but a new method. They are merged and
# work as combined. The route will serve all of GET, POST and PUT.
@app.route('/overload', methods=['POST', 'PUT'])
async def handler2(request):
return text('OK2')
# This is the existing route and PUT method is the duplicated method.
# It raises RouteExists.
@app.route('/overload', methods=['PUT', 'DELETE'])
async def handler3(request):
return text('Duplicated')
When Sanic has an exception in a request middleware, it fails to
save request object in `results`. In `sanic_endpoint_test`, because
it always requires `results` to have both `request` and `response` objects,
it prints traceback like attached example. It is not a user code and
it doesn't give any information to users, it is better to suppress
to print this kind of error.
To fix it, this patch insert collect hook as first request middleware
to guarantee to successfully run it always.
```
app = <sanic.sanic.Sanic object at 0x1102b5358>, method = 'get', uri = '/ping/', gather_request = True, loop = None
debug = True, request_args = (), request_kwargs = {}
_collect_request = <function sanic_endpoint_test.<locals>._collect_request at 0x11286c158>
_collect_response = <function sanic_endpoint_test.<locals>._collect_response at 0x11286c378>
def sanic_endpoint_test(app, method='get', uri='/', gather_request=True,
loop=None, debug=False, *request_args,
**request_kwargs):
results = []
exceptions = []
if gather_request:
@app.middleware
def _collect_request(request):
results.append(request)
async def _collect_response(sanic, loop):
try:
response = await local_request(method, uri, *request_args,
**request_kwargs)
results.append(response)
except Exception as e:
exceptions.append(e)
app.stop()
app.run(host=HOST, debug=debug, port=42101,
after_start=_collect_response, loop=loop)
if exceptions:
raise ValueError("Exception during request: {}".format(exceptions))
if gather_request:
try:
> request, response = results
E ValueError: not enough values to unpack (expected 2, got 1)
../sanic/sanic/utils.py:46: ValueError
```
1. not list() -> callable()
The args of hooking parameters of Sanic have to be callables.
For wrong parameters, errors will be generated from:
```
listeners += args
```
By checking just list type, the raised error will be associated
with `[args]` instead of `args`, which is not given by users.
With this patch, the raised error will be associated with `args`.
Then users can notice their argument was neither callable nor list
in the easier way.
2. Function -> Functions in document
Regarding the parameter as a list is harmless to the user code.
But unawareness of its type can be list can limit the potent of
the user code.
Response middleware are useful to apply some post-process information, just before send to the user. For example: Add some HTTP headers (security headers, for example), remove "Server" banner (for security reasons) or cookie management.
The change is very very simple: although an "request" middleware has produced any response, we'll even apply the response middlewares.