-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for CorrelationIdPlugin and CorrelationIdPlugin still executing when application encounters an exception #9
Comments
I removed my answers twice. At first I thought there's nothing to fix, then I've noticed what the issue was, but now I still think there's nothing to fix. Originally I was confused because you wrote
but I think you meant responses. So let's dig into it.
But you ask about 500, which is handled by
As it's outermost there's nothing I can do about it. So if you want to set response headers for I think the best solution for that problem would be to add another middleware which only does try catch for Anyway, I will write some tests and examples for that over the weekend. class Starlette:
def __init__(
self,
debug: bool = False,
# ... more things ....
middleware: typing.Sequence[Middleware] = None,
exception_handlers: typing.Dict[
typing.Union[int, typing.Type[Exception]], typing.Callable
] = None,
) -> None:
self.exception_handlers = (
{} if exception_handlers is None else dict(exception_handlers)
)
self.user_middleware = [] if middleware is None else list(middleware)
self.middleware_stack = self.build_middleware_stack()
def build_middleware_stack(self) -> ASGIApp:
debug = self.debug
error_handler = None
exception_handlers = {}
for key, value in self.exception_handlers.items():
if key in (500, Exception):
error_handler = value
else:
exception_handlers[key] = value
middleware = (
[Middleware(ServerErrorMiddleware, handler=error_handler, debug=debug,)]
+ self.user_middleware
+ [
Middleware(
ExceptionMiddleware, handlers=exception_handlers, debug=debug,
)
] |
Yes the headers x-request-id and x-correlation-id being set in responses was what I meant. Updated the initial issue comment to fix the typo. |
I was digging into it for some time and I don't think there's a clean solution for that. I don't think you can do much by adding additional middleware. Subclassing For exceptions that are not 500 (basically those that are handled), you need to assign headers in exception function like in here.
So you can handle that For 500 or anything that isn't handled by your Please consider closing this ticket or opening a PR. I will leave this ticket open as I don't know if you have any ideas. I run out of them. |
I will close this issue. Thanks for the explanation. |
@derekbekoe this case is now supported. https://github.com/tomwojcik/starlette-context/pull/79/files |
Issue
When a route raises an exception and so returns a 500 Internal Server Error, x-request-id and x-correlation-id are not set.
The culprit seems to be
starlette-context/starlette_context/middleware.py
Lines 46 to 52 in 79aa8ff
This is similar to this comment - tiangolo/fastapi#397 (comment)
Expectation
I'd expect those headers to be set for all responses. One benefit of correlation ids is when there are errors, we can use the ids to track down the issue.
Steps to reproduce
Sample.
The "/" endpoint returns the x-request-id and x-correlation-id headers.
The "/error" does not.
The text was updated successfully, but these errors were encountered: