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
Nicer error handling after eof, warning for second response being created #2323
Changes from all commits
ff6972f
978412a
7b4cd01
d6e38af
584c5bc
7af89ef
f3c9dd6
c79cb8e
7cc05dd
23956ed
bc873f1
25e0eb0
df9c404
08c3eb4
94d8db9
8fe123b
bb83527
8ee9270
b65ee67
83b4aba
777e9f6
3dab5fc
be9b4cd
153be3c
893d432
8ca425f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,9 @@ | |
from sanic.compat import Header, open_async | ||
from sanic.constants import DEFAULT_HTTP_CONTENT_TYPE | ||
from sanic.cookies import CookieJar | ||
from sanic.exceptions import ResponseException, SanicException | ||
from sanic.helpers import has_message_body, remove_entity_headers | ||
from sanic.http import Http | ||
from sanic.http import Http, Stage | ||
from sanic.models.protocol_types import HTMLProtocol, Range | ||
|
||
|
||
|
@@ -101,7 +102,7 @@ def processed_headers(self) -> Iterator[Tuple[bytes, bytes]]: | |
|
||
async def send( | ||
self, | ||
data: Optional[Union[AnyStr]] = None, | ||
data: Optional[AnyStr] = None, | ||
end_stream: Optional[bool] = None, | ||
) -> None: | ||
""" | ||
|
@@ -112,8 +113,16 @@ async def send( | |
""" | ||
if data is None and end_stream is None: | ||
end_stream = True | ||
if end_stream and not data and self.stream.send is None: | ||
return | ||
if self.stream.send is None: | ||
if end_stream and not data: | ||
return | ||
elif self.stream.stage == Stage.IDLE: | ||
raise ResponseException( | ||
"Response stream was ended, no more response data is " | ||
"allowed to be sent." | ||
) | ||
else: | ||
raise SanicException("Send response function isn't available") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure. I couldn't think about any other situation that the function is None, but I added a else just in case it falls to this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that it is the correct approach to have a failsafe, but I was just thinking that if that happens maybe we should include a little bit of information about whatever unexpected state was reached. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What info can be included do you think? I couldn't think about more than that. |
||
data = ( | ||
data.encode() # type: ignore | ||
if hasattr(data, "encode") | ||
|
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 like this better. Let's just use a better variable name:
should_reuse_response