Handle webhook errors

A webhook fails when the request does not complete, for example by timeout or a blocked address, or when the response code is not one of the Success Codes. By default those are 200, 201, 202, 204 and 205.

Then the form shows its error message (the Contact Form 7 message for a failed email), the plugin emails the error with the request and the response, and the next webhooks of the form are not sent.

Settings

Under Advanced settings, Errors has the emails that get the notification (the site admin email by default, separated by commas) and the success codes.

Show the error to the visitor

The ctz_trigger_webhook_error_message filter changes the form message. This shows the error itself, which helps while testing but exposes technical details. Add the code as explained in Add custom code:

add_filter( 'ctz_trigger_webhook_error_message', function( $message, $exception ) {
    return $exception->getMessage();
}, 10, 2 );

Returning an empty string hides the error from the visitor: the form shows success and the next webhooks are still sent. See Make multiple webhooks optionals.

Your own rules

The ctz_post_request_result action receives every response. Throw an exception there to treat a response as an error:

add_action( 'ctz_post_request_result', function( $result, $hook_url ) {
    $body = json_decode( wp_remote_retrieve_body( $result ), true );

    if ( isset( $body['status'] ) && 'error' === $body['status'] ) {
        throw new Exception( 'The webhook did not accept the data.' );
    }
}, 10, 2 );

Other hooks: ctz_trigger_webhook_error_mails changes who gets the notification, ctz_post_request_ignore_errors returns true to skip the checks for a request, and the ctz_trigger_webhook_errors action runs after all webhooks with the list of errors.

While developing

With WP_DEBUG on and define( 'CFTZ_ADD_DEBUG_FUNCTIONS', true ); in wp-config.php, errors go to debug.log instead of email.