Execute the callback, optionally passing it parsed, and return
    its return value.  If an exception occurs, determine which kind it
    is, output an appropriate message, and return the corresponding
    error code.

def handle_errors(callback, parsed=None, out=sys.stderr):

    """Execute the callback, optionally passing it parsed, and return
    its return value.  If an exception occurs, determine which kind it
    is, output an appropriate message, and return the corresponding
    error code."""

    try:
        if parsed:
            return callback(parsed)
        else:
            return callback()
    except DeploymentError as e:
        traceback.print_exc(file=out)
        print(e, file=out)
        if hasattr(e, 'value') and hasattr(e.value, 'args') and len(e.value.args) > 0 and \
                'open_sftp_client' in e.value.args[0]:
            print('Timeout', file=out)
            return TIMEOUT
        return DEPLOYMENT_ERROR
    except MalformedResponseError as e:
        traceback.print_exc(file=out)
        print(e, file=out)
        if 'Service Unavailable' in e.body:
            return SERVICE_UNAVAILABLE
        return MALFORMED_RESPONSE
    except SystemExit:
        pass
    except:
        traceback.print_exc(file=out)
        return EXCEPTION