-
-
Notifications
You must be signed in to change notification settings - Fork 954
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
Handle staticfiles OSError exceptions #1220
Handle staticfiles OSError exceptions #1220
Conversation
tests/test_staticfiles.py
Outdated
|
||
with mock.patch( | ||
"starlette.staticfiles.StaticFiles.lookup_path" | ||
) as mock_lookup_path: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't seen any usage of unittest.mock
in the project, but I couldn't find any better way to create not-so-simple OSError
exceptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any OSError
that might cause this? Maybe a broken symlink or would that just 404?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that would be a FileNotFound. I'll give it another try but all other unhandled OSError
exceptions happened on writing a file or internal sys calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
statting a file you can't access gives you PermissionError:
>>> os.stat("/boot/bar")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/boot/bar'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah you already have a test for that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JayH5 Changed that, please check again if that's what you meant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks yes that is what I meant. Sorry for the slow response 😕
I've been thinking about this again and I'm not sure we actually want to return 500 from within the static files implementation. Should we not just allow/raise the exception since it is so unexpected? IMO, the ServerErrorMiddleware
(and similar) should really be the only thing in Starlette that returns a 500 response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JayH5
That makes sense. What about the other exceptions?
Like 404 and 401, can't ExceptionMiddleware
handle those?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You do make a good point 🤔 I'm not sure why we don't raise an HTTPException
in those cases. I'd be open to that change (if it's as straightforward as it seems) either in this PR or another.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JayH5 I played around this a bit and changed that.
I think we can raise HTTPException
instead of returning PlaintTextResponse
s but there's a difference. If we raise HTTPException
then we need to can't use StaticFiles
alone as we need Starlette
middlewares to handle the exception which StaticFiles
doesn't have.
tests/test_staticfiles.py
Outdated
|
||
with mock.patch( | ||
"starlette.staticfiles.StaticFiles.lookup_path" | ||
) as mock_lookup_path: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any OSError
that might cause this? Maybe a broken symlink or would that just 404?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aminalaee - Looks neatly done. Happy to leave this up to your judgement.
Co-authored-by: Marcelo Trylesinski <[email protected]>
…tarlette into handle-staticfiles-os-errors
Heads up for anyone using I am using I know it's my bad to use an undocumented internal method, but I just don't want to handle path on my own. |
Closes #1123 Issue
And can also closes #1124 old PR
This will handle
staticfiles
exceptions:404
(file not found was already handled)401
OSError
exceptions happen, returns500