You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When following these instructions, I would run into errors like the following:
Main 2024-09-16 00:32:51 -0400 ERROR: Exception occurred while handling uri: 'http://localhost:8080/api/sessions/54b75e23-7ef4-4de1-9953-b562437080ee'
Traceback (most recent call last):
File "handle_request", line 75, in handle_request
from sanic.logging.setup import setup_logging
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'types.SimpleNamespace' object has no attribute 'request_middleware'
self.route
<Route: name=REDACTED.wrapped_handler path=api/sessions/<session_id:str>>
self.route.extra
namespace(ident='REDACTED.wrapped_handler', ignore_body=False, stream=False, hosts=[None], static=False, error_format='', websocket=False)
Main 2024-09-16 00:32:51 -0400 ERROR: Exception occurred in one of response middleware handlers
Traceback (most recent call last):
File "handle_request", line 75, in handle_request
from sanic.logging.setup import setup_logging
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'types.SimpleNamespace' object has no attribute 'request_middleware'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Volumes/workspace/Projects/ontology/oracle/automaton/.venv/lib/python3.11/site-packages/sanic/request/types.py", line 405, in respond
self.route and self.route.extra and self.route.extra.response_middleware
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'types.SimpleNamespace' object has no attribute 'response_middleware'
Specifically, I found that removing the app.router.reset() and app.router.finalize() from options.py fixed this issue:
fromcollectionsimportdefaultdictfromtypingimportDictfromsanicimportempty, Request, HTTPResponse, Sanicfromsanic.routerimportRoutefrom .corsimport_add_cors_headersdef_compile_routes_needing_options(routes: Dict[str, Route]) ->Dict[str, str]:
needs_options=defaultdict(list)
# This is 21.12 and later. You will need to change this for older versions.forrouteinroutes:
if"OPTIONS"notinroute.methods:
needs_options[route.uri].extend(route.methods)
return {
uri: ",".join(methods) foruri, methodsindict(needs_options).items()
}
asyncdefoptions_handler(request: Request, *args, **kwargs) ->HTTPResponse:
returnempty()
defsetup_options(app: Sanic, _):
uri_methods_mapping=_compile_routes_needing_options(app.router.routes)
app.ctx.uri_methods_mapping=uri_methods_mappingforuri, methodsinuri_methods_mapping.items():
app.add_route(options_handler, uri, methods= ["OPTIONS"])
I understand this may not suite everyone's needs - especially if folks need custom OPTIONS endpoints that have more specific logic. Nonetheless, I figured I would share my working configuration.
The following is the requirements.txt for this project:
Is there an existing issue for this?
Describe the bug
I have found the instructions for configuring CORS listed here to be incorrect: https://sanic.dev/en/guide/how-to/cors.html
When following these instructions, I would run into errors like the following:
Specifically, I found that removing the
app.router.reset()
andapp.router.finalize()
fromoptions.py
fixed this issue:I'm not sure how critical it is for these to be invoked, but it seems everything is working fine without them.
Additionally, I was able to reduce the
cors.py
andoptions.py
to the following, which are working:cors.py
options.py
I understand this may not suite everyone's needs - especially if folks need custom
OPTIONS
endpoints that have more specific logic. Nonetheless, I figured I would share my working configuration.The following is the
requirements.txt
for this project:The following is the
requirements-dev.txt
for this project:Code snippet
No response
Expected Behavior
No response
How do you run Sanic?
As a script (
app.run
orSanic.serve
)Operating System
Linux
Sanic Version
24.6.0
Additional context
No response
The text was updated successfully, but these errors were encountered: