# Buggy code
def handle_long_running_operation_exception(ex):
    import json
    import azure.cli.core.telemetry as telemetry

    telemetry.set_exception(
        ex,
        fault_type="failed-long-running-operation",
        summary="Unexpected client exception in {}.".format(
            LongRunningOperation.__name__
        ),
    )

    message = getattr(ex, "message", ex)
    error_message = "Deployment failed."

    try:
        correlation_id = ex.response.headers["x-ms-correlation-request-id"]
        error_message = "{} Correlation ID: {}.".format(error_message, correlation_id)
    except:  # pylint: disable=bare-except
        pass

    try:
        inner_message = json.loads(ex.response.text)["error"]["details"][0]["message"]
        error_message = "{} {}".format(error_message, inner_message)
    except:  # pylint: disable=bare-except
        error_message = "{} {}".format(error_message, message)

    cli_error = CLIError(error_message)
    # capture response for downstream commands (webapp) to dig out more details
    setattr(cli_error, "response", getattr(ex, "response", None))
    raise cli_error


# Traceback (FileNotFoundError)
(env) C:\Users\trpresco\Documents\github\azure-cli>run_tests --module network --parallel
The run_tests command is going to be replaced by 'azdev test' command.

Traceback (most recent call last):
File "C:\Users\trpresco\Documents\github\azure-cli\env\Scripts\run_tests-script.py", line 11, in <module>
load_entry_point('azure-cli-dev-tools', 'console_scripts', 'run_tests')()
File "c:\users\trpresco\documents\github\azure-cli\tools\automation\tests\__init__.py", line 79, in legacy_entry_point
args.func(args)
File "c:\users\trpresco\documents\github\azure-cli\tools\automation\tests\__init__.py", line 20, in execute
current_profile = check_output(shlex.split('az cloud show --query profile -otsv')).decode('utf-8').strip()
File "C:\Users\trpresco\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\trpresco\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\trpresco\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\trpresco\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

# Fixed code
def handle_long_running_operation_exception(ex):
    import azure.cli.core.telemetry as telemetry

    telemetry.set_exception(
        ex,
        fault_type="failed-long-running-operation",
        summary="Unexpected client exception in {}.".format(
            LongRunningOperation.__name__
        ),
    )

    message = getattr(ex, "message", ex)
    error_message = "Deployment failed."

    try:
        correlation_id = ex.response.headers["x-ms-correlation-request-id"]
        error_message = "{} Correlation ID: {}.".format(error_message, correlation_id)
    except:  # pylint: disable=bare-except
        pass

    try:
        inner_message = json.loads(ex.response.text)["error"]["details"][0]["message"]
        error_message = "{} {}".format(error_message, inner_message)
    except:  # pylint: disable=bare-except
        error_message = "{} {}".format(error_message, message)

    cli_error = CLIError(error_message)
    # capture response for downstream commands (webapp) to dig out more details
    setattr(cli_error, "response", getattr(ex, "response", None))
    raise cli_error