Skip to content

Commit

Permalink
Merge pull request #77 from tykeal/fix_error_reporting
Browse files Browse the repository at this point in the history
Fix: Better URL error handling
  • Loading branch information
tykeal authored Feb 3, 2022
2 parents 82d2947 + 88dfba6 commit 3d3c298
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
30 changes: 17 additions & 13 deletions custom_components/rental_control/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,24 @@ async def _start_config_flow(
# We require that the URL be an SSL URL
if not re.search("^https://", user_input["url"]):
errors["base"] = "invalid_url"

session = async_get_clientsession(
cls.hass, verify_ssl=user_input["verify_ssl"]
)
with async_timeout.timeout(REQUEST_TIMEOUT):
resp = await session.get(user_input["url"])
if resp.status != 200:
_LOGGER.error(
"%s returned %s - %s", user_input["url"], resp.status, resp.reason
else:
session = async_get_clientsession(
cls.hass, verify_ssl=user_input["verify_ssl"]
)
errors["base"] = "unknown"
# We require text/calendar in the content-type header
if "text/calendar" not in resp.content_type:
errors["base"] = "bad_ics"
with async_timeout.timeout(REQUEST_TIMEOUT):
resp = await session.get(user_input["url"])
if resp.status != 200:
_LOGGER.error(
"%s returned %s - %s",
user_input["url"],
resp.status,
resp.reason,
)
errors["base"] = "unknown"
else:
# We require text/calendar in the content-type header
if "text/calendar" not in resp.content_type:
errors["base"] = "bad_ics"
except vol.Invalid as err:
_LOGGER.exception(err.msg)
errors["base"] = "invalid_url"
Expand Down
4 changes: 2 additions & 2 deletions custom_components/rental_control/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"bad_time": "Check-in/out time is invalid. Use 24 hour time",
"invalid_url": "Only https URLs are supported",
"same_name": "Name already in use",
"unknown": "Unexpected error"
"unknown": "Unexpected error, check logs for more details"
},
"step": {
"user": {
Expand All @@ -32,7 +32,7 @@
"bad_time": "Check-in/out time is invalid. Use 24 hour time",
"invalid_url": "Only https URLs are supported",
"same_name": "Name already in use",
"unknown": "Unexpected error"
"unknown": "Unexpected error, check logs for more details"
},
"step": {
"init": {
Expand Down
4 changes: 2 additions & 2 deletions custom_components/rental_control/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"bad_time": "Check-in/out time is invalid. Use 24 hour time",
"invalid_url": "Only https URLs are supported",
"same_name": "Name already in use",
"unknown": "Unexpected error"
"unknown": "Unexpected error, check logs for more details"
},
"step": {
"user": {
Expand All @@ -32,7 +32,7 @@
"bad_time": "Check-in/out time is invalid. Use 24 hour time",
"invalid_url": "Only https URLs are supported",
"same_name": "Name already in use",
"unknown": "Unexpected error"
"unknown": "Unexpected error, check logs for more details"
},
"step": {
"init": {
Expand Down

0 comments on commit 3d3c298

Please sign in to comment.