# Buggy code
def handle_exception(ex):
    # For error code, follow guidelines at https://docs.python.org/2/library/sys.html#sys.exit,
    from msrestazure.azure_exceptions import CloudError
    from msrest.exceptions import (
        HttpOperationError,
        ValidationError,
        ClientRequestError,
    )
    from azure.cli.core.azlogging import CommandLoggerContext

    with CommandLoggerContext(logger):
        if isinstance(ex, (CLIError, CloudError)):
            logger.error(ex.args[0])
            return ex.args[1] if len(ex.args) >= 2 else 1
        if isinstance(ex, ValidationError):
            logger.error("validation error: %s", ex)
            return 1
        if isinstance(ex, ClientRequestError):
            logger.error("request failed: %s", ex)
            return 1
        if isinstance(ex, KeyboardInterrupt):
            return 1
        if isinstance(ex, HttpOperationError):
            try:
                response_dict = json.loads(ex.response.text)
                error = response_dict["error"]

                # ARM should use ODATA v4. So should try this first.
                # http://docs.oasis-open.org/odata/odata-json-format/v4.0/os/odata-json-format-v4.0-os.html#_Toc372793091
                if isinstance(error, dict):
                    code = "{} - ".format(error.get("code", "Unknown Code"))
                    message = error.get("message", ex)
                    logger.error("%s%s", code, message)
                else:
                    logger.error(error)

            except (ValueError, KeyError):
                logger.error(ex)
            return 1

        logger.error(
            "The command failed with an unexpected error. Here is the traceback:\n"
        )
        logger.exception(ex)
        logger.warning("\nTo open an issue, please run: 'az feedback'")

        return 1


# Traceback (jmespath.exceptions.JMESPathTypeError)
The result is not complete. You can still use '--all' to get all of them with long latency expected, or provide a filter through command arguments
The command failed with an unexpected error. Here is the traceback:

In function contains(), invalid type for value: None, expected one of: ['array', 'string'], received: "null"
Traceback (most recent call last):
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\knack\knack\cli.py", line 206, in invoke
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\azure-cli-core\azure\cli\core\commands\__init__.py", line 572, in execute
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\knack\knack\cli.py", line 162, in raise_event
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\knack\knack\query.py", line 46, in filter_output
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\jmespath\jmespath\parser.py", line 509, in search
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\jmespath\jmespath\visitor.py", line 94, in visit
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\jmespath\jmespath\visitor.py", line 180, in visit_filter_projection
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\jmespath\jmespath\visitor.py", line 94, in visit
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\jmespath\jmespath\visitor.py", line 171, in visit_function_expression
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\jmespath\jmespath\functions.py", line 80, in call_function
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\jmespath\jmespath\functions.py", line 91, in _validate_arguments
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\jmespath\jmespath\functions.py", line 98, in _type_check
File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-dsvrkf2x\jmespath\jmespath\functions.py", line 113, in _type_check_single
jmespath.exceptions.JMESPathTypeError: In function contains(), invalid type for value: None, expected one of: ['array', 'string'], received: "null"

# Fixed code
def handle_exception(ex):  # pylint: disable=too-many-return-statements
    # For error code, follow guidelines at https://docs.python.org/2/library/sys.html#sys.exit,
    from jmespath.exceptions import JMESPathTypeError
    from msrestazure.azure_exceptions import CloudError
    from msrest.exceptions import (
        HttpOperationError,
        ValidationError,
        ClientRequestError,
    )
    from azure.cli.core.azlogging import CommandLoggerContext

    with CommandLoggerContext(logger):
        if isinstance(ex, JMESPathTypeError):
            logger.error("\nInvalid jmespath query supplied for `--query`:\n%s", ex)
            logger.error(
                "To learn more about --query, please visit: "
                "https://docs.microsoft.com/en-us/cli/azure/query-azure-cli?view=azure-cli-latest"
            )
            return 1
        if isinstance(ex, (CLIError, CloudError)):
            logger.error(ex.args[0])
            return ex.args[1] if len(ex.args) >= 2 else 1
        if isinstance(ex, ValidationError):
            logger.error("validation error: %s", ex)
            return 1
        if isinstance(ex, ClientRequestError):
            logger.error("request failed: %s", ex)
            return 1
        if isinstance(ex, KeyboardInterrupt):
            return 1
        if isinstance(ex, HttpOperationError):
            try:
                response_dict = json.loads(ex.response.text)
                error = response_dict["error"]

                # ARM should use ODATA v4. So should try this first.
                # http://docs.oasis-open.org/odata/odata-json-format/v4.0/os/odata-json-format-v4.0-os.html#_Toc372793091
                if isinstance(error, dict):
                    code = "{} - ".format(error.get("code", "Unknown Code"))
                    message = error.get("message", ex)
                    logger.error("%s%s", code, message)
                else:
                    logger.error(error)

            except (ValueError, KeyError):
                logger.error(ex)
            return 1

        logger.error(
            "The command failed with an unexpected error. Here is the traceback:\n"
        )
        logger.exception(ex)
        logger.warning("\nTo open an issue, please run: 'az feedback'")

        return 1