Use HTTP_BAD_REQUEST constant (#33797)

This commit is contained in:
springstan authored and GitHub committed 2020-04-09 21:43:42 +02:00
1 parent d510384c0d
commit 4c38e6cfa5
24 files changed
+111 -61

No files matched your search

+2 -1
View File
@@ -10,6 +10,7 @@ from aiohttp.web_exceptions import HTTPForbidden, HTTPUnauthorized
import voluptuous as vol
from homeassistant.config import load_yaml_config_file
from homeassistant.const import HTTP_BAD_REQUEST
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv
@@ -81,7 +82,7 @@ def log_invalid_auth(func):
async def handle_req(view, request, *args, **kwargs):
"""Try to log failed login attempts if response status >= 400."""
resp = await func(view, request, *args, **kwargs)
if resp.status >= 400:
if resp.status >= HTTP_BAD_REQUEST:
await process_wrong_login(request)
return resp
@@ -4,6 +4,8 @@ import logging
import voluptuous as vol
from homeassistant.const import HTTP_BAD_REQUEST
# mypy: allow-untyped-defs
_LOGGER = logging.getLogger(__name__)
@@ -38,14 +40,16 @@ class RequestDataValidator:
except ValueError:
if not self._allow_empty or (await request.content.read()) != b"":
_LOGGER.error("Invalid JSON received.")
return view.json_message("Invalid JSON.", 400)
return view.json_message("Invalid JSON.", HTTP_BAD_REQUEST)
data = {}
try:
kwargs["data"] = self._schema(data)
except vol.Invalid as err:
_LOGGER.error("Data does not match schema: %s", err)
return view.json_message(f"Message format incorrect: {err}", 400)
return view.json_message(
f"Message format incorrect: {err}", HTTP_BAD_REQUEST
)
result = await method(view, request, *args, **kwargs)
return result