Skip to content

Commit

Permalink
🐛 UnboundLocalError: local variable 'status_code' referenced before a…
Browse files Browse the repository at this point in the history
…ssignment

Not all of the errors thrown by asyncio inherit from Exception. I was throwing an exception in a route to test my sentry intergration and it threw a asyncio.exceptions.CancelledError which inherits from BaseException (see https://github.com/python/cpython/blob/3.9/Lib/asyncio/exceptions.py#L9)
  • Loading branch information
InsidersByte authored and perdy committed Jan 10, 2022
1 parent 672ffc3 commit 90837df
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion starlette_prometheus/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -
before_time = time.perf_counter()
try:
response = await call_next(request)
except Exception as e:
except BaseException as e:
status_code = HTTP_500_INTERNAL_SERVER_ERROR
EXCEPTIONS.labels(method=method, path_template=path_template, exception_type=type(e).__name__).inc()
raise e from None
Expand Down

0 comments on commit 90837df

Please sign in to comment.