From bcc11fa7fe598c9d41476513968a5db4e7e4a7dd Mon Sep 17 00:00:00 2001 From: Colin Caine Date: Sun, 30 Sep 2018 09:36:55 +0100 Subject: [PATCH 1/3] Fix whitespace in error message --- sanic/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/app.py b/sanic/app.py index acdf70c8..5af21751 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -170,7 +170,7 @@ class Sanic: return handler else: raise ValueError( - 'Required parameter `request` missing' + 'Required parameter `request` missing ' 'in the {0}() route?'.format( handler.__name__)) From d100f54551d6b6c888738508c386091fa3927d50 Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Sun, 30 Sep 2018 15:59:16 +0200 Subject: [PATCH 2/3] Check error message and fix some lint error in test config. --- tests/test_config.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 3db35f4d..14b28d46 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -15,24 +15,28 @@ def test_load_from_object(app): assert app.config.CONFIG_VALUE == 'should be used' assert 'not_for_config' not in app.config + def test_auto_load_env(): environ["SANIC_TEST_ANSWER"] = "42" app = Sanic() assert app.config.TEST_ANSWER == 42 del environ["SANIC_TEST_ANSWER"] + def test_dont_load_env(): environ["SANIC_TEST_ANSWER"] = "42" app = Sanic(load_env=False) - assert getattr(app.config, 'TEST_ANSWER', None) == None + assert getattr(app.config, 'TEST_ANSWER', None) is None del environ["SANIC_TEST_ANSWER"] + def test_load_env_prefix(): environ["MYAPP_TEST_ANSWER"] = "42" app = Sanic(load_env='MYAPP_') assert app.config.TEST_ANSWER == 42 del environ["MYAPP_TEST_ANSWER"] + def test_load_from_file(app): config = b""" VALUE = 'some value' @@ -68,12 +72,16 @@ def test_load_from_envvar(app): def test_load_from_missing_envvar(app): - with pytest.raises(RuntimeError): + with pytest.raises(RuntimeError) as e: app.config.from_envvar('non-existent variable') + assert str(e.value) == ("The environment variable 'non-existent " + "variable' is not set and thus configuration " + "could not be loaded.") def test_overwrite_exisiting_config(app): app.config.DEFAULT = 1 + class Config: DEFAULT = 2 @@ -82,5 +90,6 @@ def test_overwrite_exisiting_config(app): def test_missing_config(app): - with pytest.raises(AttributeError): + with pytest.raises(AttributeError) as e: app.config.NON_EXISTENT + assert str(e.value) == ("Config has no 'NON_EXISTENT'") From 790047e450a696f87b71d8246e133af7f80828c3 Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Wed, 3 Oct 2018 10:59:24 +1000 Subject: [PATCH 3/3] Fixes #1340 --- sanic/server.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/sanic/server.py b/sanic/server.py index d5a8f211..17529a32 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -150,10 +150,7 @@ class HttpProtocol(asyncio.Protocol): self._request_stream_task.cancel() if self._request_handler_task: self._request_handler_task.cancel() - try: - raise RequestTimeout('Request Timeout') - except RequestTimeout as exception: - self.write_error(exception) + self.write_error(RequestTimeout('Request Timeout')) def response_timeout_callback(self): # Check if elapsed time since response was initiated exceeds our @@ -170,10 +167,7 @@ class HttpProtocol(asyncio.Protocol): self._request_stream_task.cancel() if self._request_handler_task: self._request_handler_task.cancel() - try: - raise ServiceUnavailable('Response Timeout') - except ServiceUnavailable as exception: - self.write_error(exception) + self.write_error(ServiceUnavailable('Response Timeout')) def keep_alive_timeout_callback(self): # Check if elapsed time since last response exceeds our configured @@ -199,8 +193,7 @@ class HttpProtocol(asyncio.Protocol): # memory limits self._total_request_size += len(data) if self._total_request_size > self.request_max_size: - exception = PayloadTooLarge('Payload Too Large') - self.write_error(exception) + self.write_error(PayloadTooLarge('Payload Too Large')) # Create parser if this is the first time we're receiving data if self.parser is None: @@ -218,8 +211,7 @@ class HttpProtocol(asyncio.Protocol): message = 'Bad Request' if self._debug: message += '\n' + traceback.format_exc() - exception = InvalidUsage(message) - self.write_error(exception) + self.write_error(InvalidUsage(message)) def on_url(self, url): if not self.url: @@ -233,8 +225,7 @@ class HttpProtocol(asyncio.Protocol): if value is not None: if self._header_fragment == b'Content-Length' \ and int(value) > self.request_max_size: - exception = PayloadTooLarge('Payload Too Large') - self.write_error(exception) + self.write_error(PayloadTooLarge('Payload Too Large')) try: value = value.decode() except UnicodeDecodeError: @@ -433,7 +424,7 @@ class HttpProtocol(asyncio.Protocol): self.log_response(response) try: self.transport.close() - except AttributeError as e: + except AttributeError: logger.debug('Connection lost before server could close it.') def bail_out(self, message, from_error=False): @@ -443,8 +434,7 @@ class HttpProtocol(asyncio.Protocol): self.transport.get_extra_info('peername')) logger.debug('Exception:\n%s', traceback.format_exc()) else: - exception = ServerError(message) - self.write_error(exception) + self.write_error(ServerError(message)) logger.error(message) def cleanup(self):