# Buggy code
def delete(name, timeout=90):
    """
    Delete the named service

    Args:

        name (str): The name of the service to delete

        timeout (int):
            The time in seconds to wait for the service to be deleted before
            returning. This is necessary because a service must be stopped
            before it can be deleted. Default is 90 seconds

            .. versionadded:: 2017.7.9, 2018.3.4

    Returns:
        bool: ``True`` if successful, otherwise ``False``

    CLI Example:

    .. code-block:: bash

        salt '*' service.delete <service name>
    """
    handle_scm = win32service.OpenSCManager(None, None, win32service.SC_MANAGER_CONNECT)

    try:
        handle_svc = win32service.OpenService(
            handle_scm, name, win32service.SERVICE_ALL_ACCESS
        )
    except pywintypes.error as exc:
        raise CommandExecutionError(
            "Failed to open {0}. {1}".format(name, exc.strerror)
        )

    try:
        win32service.DeleteService(handle_svc)
    except pywintypes.error as exc:
        raise CommandExecutionError(
            "Failed to delete {0}. {1}".format(name, exc.strerror)
        )
    finally:
        log.debug("Cleaning up")
        win32service.CloseServiceHandle(handle_scm)
        win32service.CloseServiceHandle(handle_svc)

    end_time = time.time() + int(timeout)
    while name in get_all() and time.time() < end_time:
        time.sleep(1)

    return name not in get_all()


# Traceback (TypeError)
c:\salt\salt-call -l debug --local service.restart postgresql-x64-9.6
winion-0:
[DEBUG   ] Reading configuration from c:\salt\conf\minion
[DEBUG   ] Including configuration from 'c:\salt\conf\minion.d\_schedule.conf'
[DEBUG   ] Reading configuration from c:\salt\conf\minion.d\_schedule.conf
[DEBUG   ] Using cached minion ID from c:\salt\conf\minion_id: winion-0
[DEBUG   ] Configuration file path: c:\salt\conf\minion
[WARNING ] Insecure logging configuration detected! Sensitive data may be logged.
[DEBUG   ] Grains refresh requested. Refreshing grains.
[DEBUG   ] Reading configuration from c:\salt\conf\minion
[DEBUG   ] Including configuration from 'c:\salt\conf\minion.d\_schedule.conf'
[DEBUG   ] Reading configuration from c:\salt\conf\minion.d\_schedule.conf
[DEBUG   ] Determining pillar cache
[DEBUG   ] LazyLoaded jinja.render
[DEBUG   ] LazyLoaded yaml.render
[DEBUG   ] LazyLoaded jinja.render
[DEBUG   ] LazyLoaded yaml.render
[DEBUG   ] LazyLoaded service.restart
[DEBUG   ] LazyLoaded cmd.run
[INFO    ] Executing command ['net', 'stop', '/y', 'postgresql-x64-9.6'] in directory 'C:\Windows\system32\config\systemprofile'
[ERROR   ] Command '['net', 'stop', '/y', 'postgresql-x64-9.6']' failed with return code: 2
[ERROR   ] stdout: The postgresql-x64-9.6 service is not started.



More help is available by typing NET HELPMSG 3521.
[ERROR   ] retcode: 2
[ERROR   ] Command '['net', 'stop', '/y', 'postgresql-x64-9.6']' failed with return code: 2
[ERROR   ] output: The postgresql-x64-9.6 service is not started.



More help is available by typing NET HELPMSG 3521.

Passed invalid arguments: 'error' object does not support indexing.

Usage:
Traceback (most recent call last):
File "c:\salt\bin\lib\site-packages\salt\modules\win_service.py", line 449, in stop
win32serviceutil.StopService(name)
File "c:\salt\bin\lib\site-packages\win32\lib\win32serviceutil.py", line 409, in StopService
return ControlService(serviceName, win32service.SERVICE_CONTROL_STOP, machine)
File "c:\salt\bin\lib\site-packages\win32\lib\win32serviceutil.py", line 320, in ControlService
status = win32service.ControlService(hs, code)
pywintypes.error: (1062, 'ControlService', 'The service has not been started.')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\salt\bin\lib\site-packages\salt\cli\caller.py", line 212, in call
ret['return'] = func(*args, **kwargs)
File "c:\salt\bin\lib\site-packages\salt\modules\win_service.py", line 488, in restart
return stop(name) and start(name)
File "c:\salt\bin\lib\site-packages\salt\modules\win_service.py", line 451, in stop
if exc[0] != 1062:
TypeError: 'error' object does not support indexing

Restart the named service. This issues a stop command followed by a start.

Args:
name: The name of the service to restart.

.. note::
If the name passed is ``salt-minion`` a scheduled task is created and
executed to restart the salt-minion service.

Returns:
bool: ``True`` if successful, ``False`` otherwise

CLI Example:

.. code-block:: bash

salt '*' service.restart <service name>

# Fixed code
def delete(name, timeout=90):
    """
    Delete the named service

    Args:

        name (str): The name of the service to delete

        timeout (int):
            The time in seconds to wait for the service to be deleted before
            returning. This is necessary because a service must be stopped
            before it can be deleted. Default is 90 seconds

            .. versionadded:: 2017.7.9, 2018.3.4

    Returns:
        bool: ``True`` if successful, otherwise ``False``. Also returns ``True``
            if the service is not present

    CLI Example:

    .. code-block:: bash

        salt '*' service.delete <service name>
    """
    handle_scm = win32service.OpenSCManager(None, None, win32service.SC_MANAGER_CONNECT)

    try:
        handle_svc = win32service.OpenService(
            handle_scm, name, win32service.SERVICE_ALL_ACCESS
        )
    except pywintypes.error as exc:
        win32service.CloseServiceHandle(handle_scm)
        if exc.winerror != 1060:
            raise CommandExecutionError(
                "Failed to open {0}. {1}".format(name, exc.strerror)
            )
        log.debug('Service "{0}" is not present'.format(name))
        return True

    try:
        win32service.DeleteService(handle_svc)
    except pywintypes.error as exc:
        raise CommandExecutionError(
            "Failed to delete {0}. {1}".format(name, exc.strerror)
        )
    finally:
        log.debug("Cleaning up")
        win32service.CloseServiceHandle(handle_scm)
        win32service.CloseServiceHandle(handle_svc)

    end_time = time.time() + int(timeout)
    while name in get_all() and time.time() < end_time:
        time.sleep(1)

    return name not in get_all()