Skip to content

Commit

Permalink
[Tables] Remove unnecessary function _reraise_error() (Azure#31298)
Browse files Browse the repository at this point in the history
  • Loading branch information
YalinLi0312 authored Aug 15, 2023
1 parent 321464d commit d17f662
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
20 changes: 9 additions & 11 deletions sdk/tables/azure-data-tables/azure/data/tables/_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import sys
import re
from enum import Enum

Expand Down Expand Up @@ -117,6 +116,14 @@ def _validate_tablename_error(decoded_error, table_name):
_validate_cosmos_tablename(table_name)


def _validate_key_values(decoded_error, partition_key, row_key):
if decoded_error.error_code == "PropertiesNeedValue":
if partition_key is None:
raise ValueError("PartitionKey must be present in an entity") from decoded_error
if row_key is None:
raise ValueError("RowKey must be present in an entity") from decoded_error


def _decode_error(response, error_message=None, error_type=None, **kwargs): # pylint: disable=too-many-branches
error_code = response.headers.get("x-ms-error-code")
additional_data = {}
Expand Down Expand Up @@ -186,23 +193,14 @@ def _decode_error(response, error_message=None, error_type=None, **kwargs): # p
return error


def _reraise_error(decoded_error):
_, _, exc_traceback = sys.exc_info()
try:
raise decoded_error.with_traceback(exc_traceback)
except AttributeError as exc:
decoded_error.__traceback__ = exc_traceback
raise decoded_error from exc


def _process_table_error(storage_error, table_name=None):
try:
decoded_error = _decode_error(storage_error.response, storage_error.message)
except AttributeError as exc:
raise storage_error from exc
if table_name:
_validate_tablename_error(decoded_error, table_name)
_reraise_error(decoded_error)
raise decoded_error from storage_error


def _reprocess_error(decoded_error, identifiers=None):
Expand Down
17 changes: 10 additions & 7 deletions sdk/tables/azure-data-tables/azure/data/tables/_table_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@

from ._base_client import parse_connection_str, TablesBaseClient
from ._entity import TableEntity
from ._error import _decode_error, _process_table_error, _reprocess_error, _reraise_error, _validate_tablename_error
from ._error import (
_decode_error,
_process_table_error,
_reprocess_error,
_validate_tablename_error,
_validate_key_values,
)
from ._generated.models import SignedIdentifier, TableProperties
from ._serialize import (
serialize_iso,
Expand Down Expand Up @@ -366,13 +372,10 @@ def create_entity(self, entity: EntityType, **kwargs) -> Dict[str, Any]:
)
except HttpResponseError as error:
decoded = _decode_error(error.response, error.message)
if decoded.error_code == "PropertiesNeedValue":
if entity.get("PartitionKey") is None:
raise ValueError("PartitionKey must be present in an entity") from error
if entity.get("RowKey") is None:
raise ValueError("RowKey must be present in an entity") from error
_validate_key_values(decoded, entity.get("PartitionKey"), entity.get("RowKey"))
_validate_tablename_error(decoded, self.table_name)
_reraise_error(error)
# We probably should have been raising decoded error before removing _reraise_error()
raise error
return _trim_service_metadata(metadata, content=content) # type: ignore

@distributed_trace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@
_get_match_headers,
)
from .._deserialize import deserialize_iso, _return_headers_and_deserialized, _convert_to_entity, _trim_service_metadata
from .._error import _decode_error, _process_table_error, _reprocess_error, _reraise_error, _validate_tablename_error
from .._error import (
_decode_error,
_process_table_error,
_reprocess_error,
_validate_tablename_error,
_validate_key_values,
)
from .._table_client import EntityType, TransactionOperationType
from ._base_client_async import AsyncTablesBaseClient
from ._models import TableEntityPropertiesPaged
Expand Down Expand Up @@ -373,13 +379,10 @@ async def create_entity(self, entity: EntityType, **kwargs) -> Mapping[str, Any]
)
except HttpResponseError as error:
decoded = _decode_error(error.response, error.message)
if decoded.error_code == "PropertiesNeedValue":
if entity.get("PartitionKey") is None:
raise ValueError("PartitionKey must be present in an entity") from error
if entity.get("RowKey") is None:
raise ValueError("RowKey must be present in an entity") from error
_validate_key_values(decoded, entity.get("PartitionKey"), entity.get("RowKey"))
_validate_tablename_error(decoded, self.table_name)
_reraise_error(error)
# We probably should have been raising decoded error before removing _reraise_error()
raise error
return _trim_service_metadata(metadata, content=content) # type: ignore

@distributed_trace_async
Expand Down

0 comments on commit d17f662

Please sign in to comment.