Filter uncanny-automator

automator_error_message

Filters the specific error message Filters the error message displayed when an Automator action fails, allowing customization of the output.

add_filter( 'automator_error_message', $callback, 10, 2 );

Description

This filter hook allows developers to modify specific error messages generated by Uncanny Automator. It fires when an error message is being constructed, providing the original message, its key, and any additional information. Developers can use this hook to customize error outputs for better user feedback or internal logging.


Usage

add_filter( 'automator_error_message', 'your_function_name', 10, 2 );

Parameters

$error_message (mixed)
- **$error_key** `mixed`
$additional_information (mixed)

Return Value

The filtered value.


Examples

add_filter( 'automator_error_message', 'my_custom_automator_error_message', 10, 3 );

/**
 * Customizes a specific Uncanny Automator error message.
 *
 * This function intercepts the default error message for a given error key
 * and modifies it based on the provided additional information.
 *
 * @param string $error_message The default error message generated by Uncanny Automator.
 * @param string $error_key     The unique key identifying the error.
 * @param string $additional_information Any extra context or details for the error.
 * @return string The modified or custom error message.
 */
function my_custom_automator_error_message( $error_message, $error_key, $additional_information ) {
    // Example: If the error key is 'recipe_not_found', and additional info contains a specific ID,
    // we can provide a more user-friendly message.
    if ( 'recipe_not_found' === $error_key && strpos( $additional_information, 'Recipe ID:' ) !== false ) {
        // Extract the recipe ID for a more personalized message.
        preg_match( '/Recipe ID: (d+)/', $additional_information, $matches );
        $recipe_id = isset( $matches[1] ) ? $matches[1] : 'unknown';

        // Construct a more helpful message.
        return sprintf(
            esc_html__( 'Oops! The recipe with ID %1$s could not be found. Please check the recipe ID and try again. Original details: %2$s', 'your-text-domain' ),
            esc_html( $recipe_id ),
            esc_html( $additional_information )
        );
    }

    // For any other error keys, we can append a general site-specific note.
    if ( ! empty( $error_message ) && ! empty( $additional_information ) ) {
        return $error_message . ' ' . esc_html__( 'Please contact support if the issue persists.', 'your-text-domain' );
    }

    // If the original error message is empty, return a default with a custom prefix.
    if ( empty( $error_message ) ) {
        return esc_html__( 'Automator Error: ', 'your-text-domain' ) . esc_html( $additional_information );
    }

    // Otherwise, return the original error message, as we don't have a specific override for it.
    return $error_message;
}

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/lib/utilities/error/class-automator-error-messages.php:80

public function get( $error_key = null, $additional_information = '' ) {

		/**
		 * Filters all error messages before a specific error message is set
		 */
		$error_messages = apply_filters( 'automator_error_messages', $this->error_messages );
		if ( ! isset( $error_messages[ $error_key ] ) ) {
			return esc_html__( 'No message', 'uncanny-automator' );
		}
		$error_message = $error_messages[ $error_key ] . $additional_information;

		/**
		 * Filters the specific error message
		 */
		return apply_filters( 'automator_error_message', $error_message, $error_key, $additional_information );
	}


Scroll to Top