def translate_error(error, error_mappings=None):
    '''Translate any error type into an EosCompanionAppError error.'''
    if error.domain == GLib.quark_to_string(
            EosCompanionAppService.error_quark()):
        return (EosCompanionAppService.error_quark(),
                EosCompanionAppService.Error(error.code))

    for src_domain, src_code, target_code in generate_error_mappings(
            error_mappings):
        if error.matches(src_domain, src_code):
            return (EosCompanionAppService.error_quark(),
                    EosCompanionAppService.Error(target_code))

    return (EosCompanionAppService.error_quark(),
            EosCompanionAppService.Error.FAILED)
def not_found_response(msg, path):
    '''Respond with an error message and 404.'''
    msg.set_status(Soup.Status.NOT_FOUND)
    error = serialize_error_as_json_object(
        EosCompanionAppService.error_quark(),
        EosCompanionAppService.Error.INVALID_REQUEST,
        detail={'invalid_path': path})
    EosCompanionAppService.set_soup_message_response(
        msg, 'application/json', json.dumps({
            'status': 'error',
            'error': error
        }))
Exemple #3
0
        def middleware(server, msg, path, query, *args):
            '''Middleware to check the query parameters.'''
            rectified_query = query or {}
            if not rectified_query.get(param, None):
                return json_response(
                    msg, {
                        'status':
                        'error',
                        'error':
                        serialize_error_as_json_object(
                            EosCompanionAppService.error_quark(),
                            EosCompanionAppService.Error.INVALID_REQUEST,
                            detail={'missing_querystring_param': param})
                    })

            return handler(server, msg, path, rectified_query, *args)
    def _on_finished_loading_shards(shard_init_results):
        '''Callback for when shards have finished initializing.

        Check for any errors and invoke the callback accordingly,
        otherwise, invoke the callback with the resolved shards
        and metadata now.
        '''
        try:
            callback(None,
                     list(_iterate_init_shard_results(shard_init_results)))
        except GLib.Error as error:
            callback(GLib.Error(error.message,  # pylint: disable=no-member
                                EosCompanionAppService.error_quark(),
                                EosCompanionAppService.Error.FAILED),
                     None)
            return
def load_record_from_shards_async(shards, content_id, attr, callback):
    '''Load bytes from stream for app and content_id.

    :attr: must be one of 'data' or 'metadata'.

    Once loading is complete, callback will be invoked with a GAsyncResult,
    use EosCompanionAppService.finish_load_all_in_stream_to_bytes
    to get the result or handle the corresponding error.

    Returns LOAD_FROM_ENGINE_SUCCESS if a stream could be loaded,
    LOAD_FROM_ENGINE_NO_SUCH_CONTENT if the content wasn't found.
    '''
    def _callback(_, result):
        '''Marshal the GAsyncReady callback into an (error, data) callback.'''
        try:
            bytes_data = EosCompanionAppService.finish_load_all_in_stream_to_bytes(
                result)
        except GLib.Error as error:
            callback(error, None)
            return

        callback(None, bytes_data)

    status, blob = load_record_blob_from_shards(shards, content_id, attr)

    if status == LOAD_FROM_ENGINE_NO_SUCH_CONTENT:
        GLib.idle_add(lambda: callback(
            GLib.Error(
                'EKN ID {} not found in shards'.format(content_id),
                GLib.quark_to_string(EosCompanionAppService.error_quark()),
                EosCompanionAppService.Error.INVALID_CONTENT_ID), None))
        return

    EosCompanionAppService.load_all_in_stream_to_bytes(
        blob.get_stream(),
        chunk_size=BYTE_CHUNK_SIZE,
        cancellable=None,
        callback=_callback)
def _dbus_error_to_companion_app_error(error):
    '''Translate the GDBusError from EknServices to a Companion App error.'''
    code = _EKS_ERROR_TO_COMPANION_APP_ERROR[Gio.DBusError.get_remote_error(error)]
    return GLib.Error(error.message,  # pylint: disable=no-member
                      EosCompanionAppService.error_quark(),
                      code)