D-Bus 1.15.12
Data Structures | Typedefs | Functions

Error reporting. More...

Data Structures

struct  DBusError
 Object representing an exception. More...
 

Typedefs

typedef struct DBusError DBusError
 Mostly-opaque type representing an error that occurred.
 

Functions

void dbus_error_init (DBusError *error)
 Initializes a DBusError structure.
 
void dbus_error_free (DBusError *error)
 Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_init().
 
void dbus_set_error_const (DBusError *error, const char *name, const char *message)
 Assigns an error name and message to a DBusError.
 
void dbus_move_error (DBusError *src, DBusError *dest)
 Moves an error src into dest, freeing src and overwriting dest.
 
dbus_bool_t dbus_error_has_name (const DBusError *error, const char *name)
 Checks whether the error is set and has the given name.
 
dbus_bool_t dbus_error_is_set (const DBusError *error)
 Checks whether an error occurred (the error is set).
 
void dbus_set_error (DBusError *error, const char *name, const char *format,...)
 Assigns an error name and message to a DBusError.
 
void _dbus_set_error_valist (DBusError *error, const char *name, const char *format, va_list args)
 

Detailed Description

Error reporting.

Types and functions related to reporting errors.

In essence D-Bus error reporting works as follows:

DBusError error;
dbus_error_init (&error);
dbus_some_function (arg1, arg2, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "an error occurred: %s\n", error.message);
dbus_error_free (&error);
}
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Object representing an exception.
Definition dbus-errors.h:51
const char * message
public error message field
Definition dbus-errors.h:53

By convention, all functions allow NULL instead of a DBusError*, so callers who don't care about the error can ignore it.

There are some rules. An error passed to a D-Bus function must always be unset; you can't pass in an error that's already set. If a function has a return code indicating whether an error occurred, and also a DBusError parameter, then the error will always be set if and only if the return code indicates an error occurred. i.e. the return code and the error are never going to disagree.

An error only needs to be freed if it's been set, not if it's merely been initialized.

You can check the specific error that occurred using dbus_error_has_name().

Errors will not be set for programming errors, such as passing invalid arguments to the libdbus API. Instead, libdbus will print warnings, exit on a failed assertion, or even crash in those cases (in other words, incorrect use of the API results in undefined behavior, possibly accompanied by helpful debugging output if you're lucky).

Typedef Documentation

◆ DBusError

typedef struct DBusError DBusError

Mostly-opaque type representing an error that occurred.

Definition at line 45 of file dbus-errors.h.

Function Documentation

◆ _dbus_set_error_valist()

void _dbus_set_error_valist ( DBusError error,
const char *  name,
const char *  format,
va_list  args 
)

Definition at line 376 of file dbus-errors.c.

◆ dbus_error_free()

DBUS_EXPORT void dbus_error_free ( DBusError error)

◆ dbus_error_has_name()

DBUS_EXPORT dbus_bool_t dbus_error_has_name ( const DBusError error,
const char *  name 
)

Checks whether the error is set and has the given name.

Parameters
errorthe error
namethe name
Returns
TRUE if the given named error occurred

Definition at line 304 of file dbus-errors.c.

References _dbus_assert, _dbus_string_equal(), _dbus_string_init_const(), FALSE, DBusError::message, DBusError::name, and NULL.

Referenced by _dbus_read_uuid_file(), dbus_connection_register_fallback(), dbus_connection_register_object_path(), and dbus_get_local_machine_id().

◆ dbus_error_init()

DBUS_EXPORT void dbus_error_init ( DBusError error)

Initializes a DBusError structure.

Does not allocate any memory; the error only needs to be freed if it is set at some point.

Parameters
errorthe DBusError.

Definition at line 190 of file dbus-errors.c.

References DBusRealError::const_message, DBusRealError::message, DBusRealError::name, NULL, and TRUE.

Referenced by _dbus_generate_uuid(), _dbus_keyring_new_for_credentials(), _dbus_listen_tcp_socket(), dbus_error_free(), and dbus_move_error().

◆ dbus_error_is_set()

DBUS_EXPORT dbus_bool_t dbus_error_is_set ( const DBusError error)

Checks whether an error occurred (the error is set).

Parameters
errorthe error object
Returns
TRUE if an error occurred

Definition at line 331 of file dbus-errors.c.

References _dbus_assert, FALSE, DBusError::message, DBusError::name, and NULL.

Referenced by _dbus_read_credentials_socket(), _dbus_server_new_for_socket(), _dbus_transport_open(), _dbus_transport_open_platform_specific(), _dbus_write_pid_to_file_and_pipe(), dbus_address_unescape_value(), dbus_parse_address(), and dbus_server_listen().

◆ dbus_move_error()

DBUS_EXPORT void dbus_move_error ( DBusError src,
DBusError dest 
)

Moves an error src into dest, freeing src and overwriting dest.

Both src and dest must be initialized. src is reinitialized to an empty error. dest may not contain an existing error. If the destination is NULL, just frees and reinits the source error.

Parameters
srcthe source error
destthe destination error or NULL

Definition at line 281 of file dbus-errors.c.

References dbus_error_free(), and dbus_error_init().

Referenced by _dbus_read_uuid_file(), _dbus_transport_open(), _dbus_transport_open_platform_specific(), and dbus_server_listen().

◆ dbus_set_error()

DBUS_EXPORT void dbus_set_error ( DBusError error,
const char *  name,
const char *  format,
  ... 
)

Assigns an error name and message to a DBusError.

Does nothing if error is NULL.

The format may be NULL, which means a (pretty much useless) default message will be deduced from the name. This is not a good idea, just go ahead and provide a useful error message. It won't hurt you.

If no memory can be allocated for the error message, an out-of-memory error message will be set instead.

Parameters
errorthe error.or NULL
namethe error name
formatprintf-style format string.

Definition at line 356 of file dbus-errors.c.

References NULL.

Referenced by _dbus_append_address_from_socket(), _dbus_babysitter_set_child_exit_error(), _dbus_become_daemon(), _dbus_change_to_daemon_user(), _dbus_check_dir_is_private_to_user(), _dbus_close(), _dbus_close_socket(), _dbus_command_for_pid(), _dbus_connect_exec(), _dbus_connect_unix_socket(), _dbus_create_directory(), _dbus_create_file_exclusively(), _dbus_credentials_add_from_user(), _dbus_delete_directory(), _dbus_delete_file(), _dbus_directory_get_next_file(), _dbus_directory_open(), _dbus_dup(), _dbus_ensure_directory(), _dbus_file_get_contents(), _dbus_generate_random_bytes(), _dbus_generate_uuid(), _dbus_get_autolaunch_address(), _dbus_is_console_user(), _dbus_keyring_new_for_credentials(), _dbus_listen_systemd_sockets(), _dbus_listen_tcp_socket(), _dbus_listen_unix_socket(), _dbus_lookup_launchd_socket(), _dbus_make_file_world_readable(), _dbus_message_iter_get_args_valist(), _dbus_object_tree_register(), _dbus_read_credentials_socket(), _dbus_read_local_machine_uuid(), _dbus_send_credentials_socket(), _dbus_server_listen_unix_socket(), _dbus_server_new_for_domain_socket(), _dbus_server_new_for_launchd(), _dbus_server_new_for_tcp_socket(), _dbus_set_bad_address(), _dbus_set_socket_nonblocking(), _dbus_socketpair(), _dbus_spawn_async_with_babysitter(), _dbus_stat(), _dbus_string_save_to_file(), _dbus_transport_new_for_domain_socket(), _dbus_transport_new_for_tcp_socket(), _dbus_transport_open_platform_specific(), _dbus_user_database_lookup(), _dbus_user_database_lookup_group(), _dbus_write_pid_to_file_and_pipe(), dbus_connection_send_with_reply_and_block(), dbus_message_demarshal(), dbus_parse_address(), dbus_server_listen(), dbus_set_error_from_message(), dbus_signature_validate(), dbus_signature_validate_single(), dbus_try_get_local_machine_id(), dbus_validate_bus_name(), dbus_validate_error_name(), dbus_validate_interface(), dbus_validate_member(), dbus_validate_path(), and dbus_validate_utf8().

◆ dbus_set_error_const()

DBUS_EXPORT void dbus_set_error_const ( DBusError error,
const char *  name,
const char *  message 
)

Assigns an error name and message to a DBusError.

Does nothing if error is NULL. The message may be NULL, which means a default message will be deduced from the name. The default message will be totally useless, though, so using a NULL message is not recommended.

Because this function does not copy the error name or message, you must ensure the name and message are global data that won't be freed. You probably want dbus_set_error() instead, in most cases.

Parameters
errorthe error or NULL
namethe error name (not copied!!!)
messagethe error message (not copied!!!)

Definition at line 245 of file dbus-errors.c.

References _dbus_assert, DBusRealError::const_message, DBusRealError::message, DBusError::message, DBusRealError::name, DBusError::name, NULL, and TRUE.

Referenced by _dbus_get_autolaunch_address(), _dbus_keyring_get_best_key(), _dbus_keyring_new_for_credentials(), _dbus_listen_systemd_sockets(), and _dbus_lookup_launchd_socket().