mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Exception chaining and wrapping improvements (#39320)
* Remove unnecessary exception re-wraps * Preserve exception chains on re-raise We slap "from cause" to almost all possible cases here. In some cases it could conceivably be better to do "from None" if we really want to hide the cause. However those should be in the minority, and "from cause" should be an improvement over the corresponding raise without a "from" in all cases anyway. The only case where we raise from None here is in plex, where the exception for an original invalid SSL cert is not the root cause for failure to validate a newly fetched one. Follow local convention on exception variable names if there is a consistent one, otherwise `err` to match with majority of codebase. * Fix mistaken re-wrap in homematicip_cloud/hap.py Missed the difference between HmipConnectionError and HmipcConnectionError. * Do not hide original error on plex new cert validation error Original is not the cause for the new one, but showing old in the traceback is useful nevertheless.
This commit is contained in:
@@ -173,8 +173,8 @@ def isdevice(value: Any) -> str:
|
||||
try:
|
||||
os.stat(value)
|
||||
return str(value)
|
||||
except OSError:
|
||||
raise vol.Invalid(f"No device at {value} found")
|
||||
except OSError as err:
|
||||
raise vol.Invalid(f"No device at {value} found") from err
|
||||
|
||||
|
||||
def matches_regex(regex: str) -> Callable[[Any], str]:
|
||||
@@ -201,12 +201,12 @@ def is_regex(value: Any) -> Pattern[Any]:
|
||||
try:
|
||||
r = re.compile(value)
|
||||
return r
|
||||
except TypeError:
|
||||
except TypeError as err:
|
||||
raise vol.Invalid(
|
||||
f"value {value} is of the wrong type for a regular expression"
|
||||
)
|
||||
except re.error:
|
||||
raise vol.Invalid(f"value {value} is not a valid regular expression")
|
||||
) from err
|
||||
except re.error as err:
|
||||
raise vol.Invalid(f"value {value} is not a valid regular expression") from err
|
||||
|
||||
|
||||
def isfile(value: Any) -> str:
|
||||
@@ -331,8 +331,8 @@ def time(value: Any) -> time_sys:
|
||||
|
||||
try:
|
||||
time_val = dt_util.parse_time(value)
|
||||
except TypeError:
|
||||
raise vol.Invalid("Not a parseable type")
|
||||
except TypeError as err:
|
||||
raise vol.Invalid("Not a parseable type") from err
|
||||
|
||||
if time_val is None:
|
||||
raise vol.Invalid(f"Invalid time specified: {value}")
|
||||
@@ -347,8 +347,8 @@ def date(value: Any) -> date_sys:
|
||||
|
||||
try:
|
||||
date_val = dt_util.parse_date(value)
|
||||
except TypeError:
|
||||
raise vol.Invalid("Not a parseable type")
|
||||
except TypeError as err:
|
||||
raise vol.Invalid("Not a parseable type") from err
|
||||
|
||||
if date_val is None:
|
||||
raise vol.Invalid("Could not parse date")
|
||||
@@ -380,8 +380,8 @@ def time_period_str(value: str) -> timedelta:
|
||||
second = float(parsed[2])
|
||||
except IndexError:
|
||||
second = 0
|
||||
except ValueError:
|
||||
raise vol.Invalid(TIME_PERIOD_ERROR.format(value))
|
||||
except ValueError as err:
|
||||
raise vol.Invalid(TIME_PERIOD_ERROR.format(value)) from err
|
||||
|
||||
offset = timedelta(hours=hour, minutes=minute, seconds=second)
|
||||
|
||||
@@ -395,8 +395,8 @@ def time_period_seconds(value: Union[float, str]) -> timedelta:
|
||||
"""Validate and transform seconds to a time offset."""
|
||||
try:
|
||||
return timedelta(seconds=float(value))
|
||||
except (ValueError, TypeError):
|
||||
raise vol.Invalid(f"Expected seconds, got {value}")
|
||||
except (ValueError, TypeError) as err:
|
||||
raise vol.Invalid(f"Expected seconds, got {value}") from err
|
||||
|
||||
|
||||
time_period = vol.Any(time_period_str, time_period_seconds, timedelta, time_period_dict)
|
||||
@@ -525,7 +525,7 @@ def template(value: Optional[Any]) -> template_helper.Template:
|
||||
template_value.ensure_valid()
|
||||
return cast(template_helper.Template, template_value)
|
||||
except TemplateError as ex:
|
||||
raise vol.Invalid(f"invalid template ({ex})")
|
||||
raise vol.Invalid(f"invalid template ({ex})") from ex
|
||||
|
||||
|
||||
def dynamic_template(value: Optional[Any]) -> template_helper.Template:
|
||||
@@ -543,7 +543,7 @@ def dynamic_template(value: Optional[Any]) -> template_helper.Template:
|
||||
template_value.ensure_valid()
|
||||
return cast(template_helper.Template, template_value)
|
||||
except TemplateError as ex:
|
||||
raise vol.Invalid(f"invalid template ({ex})")
|
||||
raise vol.Invalid(f"invalid template ({ex})") from ex
|
||||
|
||||
|
||||
def template_complex(value: Any) -> Any:
|
||||
|
||||
Reference in New Issue
Block a user