Action uncanny-automator

uap_options_db_error

Fires when a database error occurs during option saving. Passes the error message and option key.

add_action( 'uap_options_db_error', $callback, 10, 2 );

Description

Fires when Automator_Options encounters a database error while fetching an option. Developers can use this to log the error or implement custom error handling. The hook passes the database error message and the option key that caused the issue.


Usage

add_action( 'uap_options_db_error', 'your_function_name', 10, 2 );

Parameters

$error (mixed)
This parameter contains the last database error message that occurred.
$key (mixed)
This parameter contains the database error message if an error occurred during the database query.

Examples

add_action( 'uap_options_db_error', 'my_custom_uap_db_error_handler', 10, 2 );

/**
 * Custom handler for Automator_Options database errors.
 *
 * This function logs the database error and the option key that caused it,
 * and can be extended to perform other actions like sending email notifications
 * or clearing a cache if the error is temporary.
 *
 * @param string $error The database error message.
 * @param string $key   The option key that caused the database error.
 */
function my_custom_uap_db_error_handler( $error, $key ) {
    // Log the error details to a custom log file for better tracking.
    error_log( sprintf(
        'Automator_Options DB Error: Failed to fetch option "%s". Error: "%s"',
        $key,
        $error
    ) );

    // Potentially send an email notification to administrators if this is a critical issue.
    // For example, you might check if the error is persistent or if certain options are failing.
    // if ( strpos( $error, 'Table' ) !== false && strpos( $error, 'doesn't exist' ) !== false ) {
    //     wp_mail(
    //         get_option( 'admin_email' ),
    //         'URGENT: Automator_Options Database Table Missing',
    //         sprintf(
    //             'The Automator_Options plugin encountered a critical database error.<br>Table might be missing or corrupted.<br><br>Option Key: %s<br>Error: %s',
    //             $key,
    //             $error
    //         )
    //     );
    // }

    // You could also implement logic to try and clear a relevant cache here
    // if you suspect the error is a transient issue.
    // delete_transient( 'automator_option_' . $key );
}

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/core/classes/class-automator-options.php:206

private function fetch_from_database_with_caching( string $key, $default_value, bool $update_object_cache ) {

		$raw_value = $this->fetch_raw_value_from_database( $key );

		// Handle database errors.
		if ( null === $raw_value && $this->query->get_db()->last_error ) {

			$error = $this->query->get_db()->last_error;

			// Send an action hook when Automator_Options encounters a database error fetching an option.
			do_action( 'uap_options_db_error', $error, $key );

			if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
				// Log only when debug logging is enabled.
				error_log( 'Automator_Options DB error: ' . $error ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log

			}

			return $default_value;
		}

		// Handle missing option.
		if ( null === $raw_value ) {
			if ( $update_object_cache ) {
				$this->cache->set_miss( $key );
			}
			return $default_value;
		}

		// Decode and cache the value.
		// Use null as default to keep stored '__null__' observable and consistent with cache paths.
		$decoded_value = Automator_Option_Formatter::format_value( $raw_value, null );
		$this->cache->set( $key, $decoded_value, $raw_value, false, $update_object_cache );

		return $decoded_value;
	}

Scroll to Top