improve error handling

This commit is contained in:
Wim Taymans 2019-06-19 16:22:22 +02:00
parent c4f35825fe
commit 00ea15dc1f
30 changed files with 465 additions and 228 deletions

View file

@ -83,13 +83,14 @@ static void *create_object(void *_data,
struct pw_device *device;
const char *factory_name, *name;
struct device_data *nd;
int res;
if (properties == NULL)
goto no_properties;
goto error_properties;
factory_name = pw_properties_get(properties, SPA_KEY_FACTORY_NAME);
if (factory_name == NULL)
goto no_properties;
goto error_properties;
name = pw_properties_get(properties, PW_KEY_DEVICE_NAME);
if (name == NULL)
@ -103,8 +104,10 @@ static void *create_object(void *_data,
0,
properties,
sizeof(struct device_data));
if (device == NULL)
goto no_mem;
if (device == NULL) {
res = -errno;
goto error_device;
}
nd = pw_spa_device_get_user_data(device);
nd->device = device;
@ -120,17 +123,19 @@ static void *create_object(void *_data,
return device;
no_properties:
error_properties:
res = -EINVAL;
pw_log_error("factory %p: usage: " FACTORY_USAGE, data->this);
if (resource) {
pw_resource_error(resource, -EINVAL, "usage: " FACTORY_USAGE);
}
return NULL;
no_mem:
pw_log_error("can't create device: no memory");
if (resource) {
pw_resource_error(resource, -ENOMEM, "no memory");
}
if (resource)
pw_resource_error(resource, res, "usage: " FACTORY_USAGE);
goto error_exit;
error_device:
pw_log_error("can't create device: %s", spa_strerror(res));
if (resource)
pw_resource_error(resource, res, "can't create device: %s", spa_strerror(res));
goto error_exit;
error_exit:
errno = -res;
return NULL;
}
@ -174,6 +179,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie
struct pw_core *core = pw_module_get_core(module);
struct pw_factory *factory;
struct factory_data *data;
int res;
factory = pw_factory_new(core,
"spa-device-factory",
@ -182,7 +188,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie
NULL,
sizeof(*data));
if (factory == NULL)
return -ENOMEM;
return -errno;
data = pw_factory_get_user_data(factory);
data->this = factory;
@ -198,9 +204,17 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie
pw_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
pw_factory_register(factory, NULL, pw_module_get_global(module), NULL);
if ((res = pw_factory_register(factory,
NULL,
pw_module_get_global(module),
NULL)) < 0)
goto error_register;
return 0;
error_register:
pw_factory_destroy(factory);
return res;
}
SPA_EXPORT

View file

@ -174,6 +174,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie
struct pw_core *core = pw_module_get_core(module);
struct pw_factory *factory;
struct factory_data *data;
int res;
factory = pw_factory_new(core,
"spa-node-factory",
@ -182,7 +183,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie
NULL,
sizeof(*data));
if (factory == NULL)
return -ENOMEM;
return -errno;
data = pw_factory_get_user_data(factory);
data->this = factory;
@ -198,9 +199,15 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie
pw_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
pw_factory_register(factory, NULL, pw_module_get_global(module), NULL);
if ((res = pw_factory_register(factory,
NULL, pw_module_get_global(module), NULL)) < 0)
goto error_register;
return 0;
error_register:
pw_factory_destroy(factory);
return res;
}
SPA_EXPORT

View file

@ -52,7 +52,6 @@ struct impl {
void *unload;
struct spa_handle *handle;
struct spa_device *device;
char *factory_name;
struct spa_hook device_listener;
@ -69,7 +68,6 @@ static void device_destroy(void *data)
spa_hook_remove(&impl->device_listener);
if (impl->handle)
pw_unload_spa_handle(impl->handle);
free(impl->factory_name);
}
static const struct pw_device_events device_events = {
@ -90,6 +88,7 @@ pw_spa_device_new(struct pw_core *core,
{
struct pw_device *this;
struct impl *impl;
int res;
this = pw_device_new(core, name, properties, sizeof(struct impl) + user_data_size);
if (this == NULL)
@ -109,10 +108,16 @@ pw_spa_device_new(struct pw_core *core,
pw_device_add_listener(this, &impl->device_listener, &device_events, impl);
pw_device_set_implementation(this, impl->device);
if (!SPA_FLAG_CHECK(impl->flags, PW_SPA_DEVICE_FLAG_NO_REGISTER))
pw_device_register(this, impl->owner, impl->parent, NULL);
if (!SPA_FLAG_CHECK(impl->flags, PW_SPA_DEVICE_FLAG_NO_REGISTER)) {
if ((res = pw_device_register(this, impl->owner, impl->parent, NULL)) < 0)
goto error_register;
}
return this;
error_register:
pw_device_destroy(this);
errno = -res;
return NULL;
}
void *pw_spa_device_get_user_data(struct pw_device *device)
@ -131,7 +136,6 @@ struct pw_device *pw_spa_device_load(struct pw_core *core,
size_t user_data_size)
{
struct pw_device *this;
struct impl *impl;
struct spa_handle *handle;
void *iface;
int res;
@ -139,6 +143,7 @@ struct pw_device *pw_spa_device_load(struct pw_core *core,
handle = pw_core_load_spa_handle(core, factory_name,
properties ? &properties->dict : NULL);
if (handle == NULL) {
res = -errno;
pw_log_error("can't load device handle: %m");
goto exit;
}
@ -151,17 +156,16 @@ struct pw_device *pw_spa_device_load(struct pw_core *core,
this = pw_spa_device_new(core, owner, parent, name, flags,
iface, handle, properties, user_data_size);
if (this == NULL) {
res = -errno;
pw_log_error("can't create device: %m");
goto exit;
goto exit_unload;
}
impl = this->user_data;
impl->factory_name = strdup(factory_name);
return this;
exit_unload:
pw_unload_spa_handle(handle);
exit:
errno = -res;
return NULL;
}

View file

@ -98,6 +98,9 @@ static struct monitor_object *add_object(struct pw_spa_monitor *this, uint32_t i
else
props = pw_properties_new(NULL, NULL);
if (props == NULL)
return NULL;
if ((name = pw_properties_get(props, PW_KEY_DEVICE_NAME)) == NULL)
name = "unknown";
@ -112,16 +115,21 @@ static struct monitor_object *add_object(struct pw_spa_monitor *this, uint32_t i
handle = pw_core_load_spa_handle(core, info->factory_name,
&props->dict);
if (handle == NULL) {
res = -errno;
pw_log_error("can't make factory instance: %m");
goto error_free_props;
}
if ((res = spa_handle_get_interface(handle, info->type, &iface)) < 0) {
pw_log_error("can't get %d interface: %d", info->type, res);
pw_log_error("can't get %d interface: %s", info->type, spa_strerror(res));
goto error_free_handle;
}
obj = calloc(1, sizeof(struct monitor_object));
if (obj == NULL) {
res = -errno;
goto error_free_handle;
}
obj->id = id;
obj->name = strdup(name);
obj->handle = handle;
@ -139,6 +147,7 @@ static struct monitor_object *add_object(struct pw_spa_monitor *this, uint32_t i
break;
}
default:
res = -ENOTSUP;
pw_log_error("interface %d not implemented", obj->type);
goto error_free_object;
}
@ -154,6 +163,7 @@ error_free_handle:
pw_unload_spa_handle(handle);
error_free_props:
pw_properties_free(props);
errno = -res;
return NULL;
}
@ -213,7 +223,7 @@ static int on_monitor_object_info(void *data, uint32_t id,
} else if (obj == NULL) {
obj = add_object(this, id, info, now_nsec);
if (obj == NULL)
return -ENOMEM;
return -errno;
} else {
change_object(this, obj, info, now_nsec);
}
@ -267,17 +277,21 @@ struct pw_spa_monitor *pw_spa_monitor_load(struct pw_core *core,
handle = pw_core_load_spa_handle(core,
factory_name,
properties ? &properties->dict : NULL);
if (handle == NULL)
if (handle == NULL) {
res = -errno;
goto exit;
}
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Monitor, &iface)) < 0) {
pw_log_error("can't get MONITOR interface: %d", res);
pw_log_error("can't get MONITOR interface: %s", spa_strerror(res));
goto exit_unload;
}
impl = calloc(1, sizeof(struct impl) + user_data_size);
if (impl == NULL)
if (impl == NULL) {
res = -errno;
goto exit_unload;
}
impl->core = core;
impl->parent = parent;
@ -302,6 +316,7 @@ struct pw_spa_monitor *pw_spa_monitor_load(struct pw_core *core,
exit_unload:
pw_unload_spa_handle(handle);
exit:
errno = -res;
return NULL;
}

View file

@ -139,9 +139,7 @@ pw_spa_node_new(struct pw_core *core,
impl->user_data = SPA_MEMBER(impl, sizeof(struct impl), void);
pw_node_add_listener(this, &impl->node_listener, &node_events, impl);
res = pw_node_set_implementation(this, impl->node);
if (res < 0)
if ((res = pw_node_set_implementation(this, impl->node)) < 0)
goto clean_node;
if (flags & PW_SPA_NODE_FLAG_ASYNC) {
@ -153,6 +151,7 @@ pw_spa_node_new(struct pw_core *core,
clean_node:
pw_node_destroy(this);
errno = -res;
return NULL;
}
@ -180,7 +179,7 @@ setup_props(struct pw_core *core, struct spa_node *spa_node, struct pw_propertie
&b);
if (res != 1) {
if (res < 0)
pw_log_debug("spa_node_get_props failed: %d", res);
pw_log_debug("spa_node_get_props failed: %s", spa_strerror(res));
return res;
}
@ -230,7 +229,7 @@ setup_props(struct pw_core *core, struct spa_node *spa_node, struct pw_propertie
}
if ((res = spa_node_set_param(spa_node, SPA_PARAM_Props, 0, props)) < 0) {
pw_log_debug("spa_node_set_props failed: %d", res);
pw_log_debug("spa_node_set_props failed: %s", spa_strerror(res));
return res;
}
return 0;
@ -256,8 +255,10 @@ struct pw_node *pw_spa_node_load(struct pw_core *core,
handle = pw_core_load_spa_handle(core,
factory_name,
properties ? &properties->dict : NULL);
if (handle == NULL)
if (handle == NULL) {
res = -errno;
goto exit;
}
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &iface)) < 0) {
pw_log_error("can't get node interface %d", res);
@ -270,14 +271,16 @@ struct pw_node *pw_spa_node_load(struct pw_core *core,
if (properties != NULL) {
if (setup_props(core, spa_node, properties) < 0) {
pw_log_debug("Unrecognized properties");
pw_log_warn("can't setup properties: %s", spa_strerror(res));
}
}
this = pw_spa_node_new(core, owner, parent, name, flags,
spa_node, handle, properties, user_data_size);
if (this == NULL)
if (this == NULL) {
res = -errno;
goto exit;
}
impl = this->user_data;
impl->handle = handle;
@ -288,5 +291,6 @@ struct pw_node *pw_spa_node_load(struct pw_core *core,
exit_unload:
pw_unload_spa_handle(handle);
exit:
errno = -res;
return NULL;
}