MicrosoftDocs / Advertising-docs

Creative Commons Attribution 4.0 International
15 stars 59 forks source link

Example walkthrough code is unnecessarily verbose #346

Closed jnalley closed 5 years ago

jnalley commented 6 years ago

Hi, I think the code in many cases is unnecessarily verbose. For example the function output_webfault_errors() could be written without repeating the same code blocks:

error_types = (
    ["ApiFault", "OperationErrors", "OperationError"],
    ["ApiFaultDetail", "Errors", "AdApiError"],
    ["ApiFaultDetail", "BatchErrors", "BatchError"],
    ["EditorialApiFaultDetail", "EditorialErrors", "EditorialError"],
    ["EditorialApiFaultDetail", "OperationErrors", "OperationError"],
    ["ExceptionDetail"],
)

def handle_error(detail, fields):
    api_errors = detail
    for field in fields:
        api_errors = getattr(api_errors, field, None)
    if api_errors is not None:
        if isinstance(api_errors, list):
            for api_error in api_errors:
                output_bing_ads_webfault_error(api_error)
        else:
            output_bing_ads_webfault_error(api_errors)
        return True
    return False

def output_webfault_errors(ex):
    if not hasattr(ex.fault, "detail"):
        raise Exception("Unknown WebFault")

    for et in error_types:
        if handle_error(ex.fault.detail, et):
            return

    raise Exception("Unknown WebFault")

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

eric-urban commented 6 years ago

@jnalley thanks for the suggestion! Will try it.

eric-urban commented 5 years ago

@jnalley with some modifications the doc is now updated. Thanks again for the suggestion.