Handle BadRequest exception in Backblaze B2 config flow and setup (#167482)

This commit is contained in:
Nick Haghiri
2026-04-06 12:12:55 +02:00
committed by GitHub
parent ba26d119f7
commit 0674de2ce4
5 changed files with 34 additions and 0 deletions
@@ -74,6 +74,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: BackblazeConfigEntry) ->
translation_domain=DOMAIN,
translation_key="invalid_bucket_name",
) from err
except exception.BadRequest as err:
raise ConfigEntryNotReady(
translation_domain=DOMAIN,
translation_key="bad_request",
translation_placeholders={"error_message": str(err)},
) from err
except (
exception.B2ConnectionError,
exception.B2RequestTimeout,
@@ -174,6 +174,14 @@ class BackblazeConfigFlow(ConfigFlow, domain=DOMAIN):
"Backblaze B2 bucket '%s' does not exist", user_input[CONF_BUCKET]
)
errors[CONF_BUCKET] = "invalid_bucket_name"
except exception.BadRequest as err:
_LOGGER.error(
"Backblaze B2 API rejected the request for Key ID '%s': %s",
user_input[CONF_KEY_ID],
err,
)
errors["base"] = "bad_request"
placeholders["error_message"] = str(err)
except (
exception.B2ConnectionError,
exception.B2RequestTimeout,
@@ -6,6 +6,7 @@
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
},
"error": {
"bad_request": "The Backblaze B2 API rejected the request: {error_message}",
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_bucket_name": "[%key:component::backblaze_b2::exceptions::invalid_bucket_name::message%]",
"invalid_capability": "[%key:component::backblaze_b2::exceptions::invalid_capability::message%]",
@@ -60,6 +61,9 @@
}
},
"exceptions": {
"bad_request": {
"message": "The Backblaze B2 API rejected the request: {error_message}"
},
"cannot_connect": {
"message": "Cannot connect to endpoint"
},
@@ -177,6 +177,16 @@ async def test_already_configured(
"cannot_connect",
"base",
),
(
"bad_request",
{
"patch": "b2sdk.v2.RawSimulator.authorize_account",
"exception": exception.BadRequest,
"args": ["test", "bad_request"],
},
"bad_request",
"base",
),
(
"unknown_error",
{
@@ -252,6 +262,11 @@ async def test_config_flow_errors(
"brand_name": "Backblaze B2",
"allowed_prefix": "test/",
}
elif error_type == "bad_request":
assert result.get("description_placeholders") == {
"brand_name": "Backblaze B2",
"error_message": "test (bad_request)",
}
@pytest.mark.parametrize(
@@ -57,6 +57,7 @@ async def test_setup_entry_invalid_auth(
(exception.RestrictedBucket("testBucket"), ConfigEntryState.SETUP_RETRY),
(exception.NonExistentBucket(), ConfigEntryState.SETUP_RETRY),
(exception.ConnectionReset(), ConfigEntryState.SETUP_RETRY),
(exception.BadRequest("test", "bad_request"), ConfigEntryState.SETUP_RETRY),
(exception.MissingAccountData("key"), ConfigEntryState.SETUP_ERROR),
],
)