Custom Integrations

Creating Custom Integrations


Fatal Error Notify supports custom plugin integrations through an extensible architecture. You can create integrations for your own plugins or third-party plugins.

Integration Class Structure

Create a class that extends FEN_Integration:

class FEN_My_Plugin extends FEN_Integration {

    public $slug = 'my-plugin';
    public $title = 'My Plugin';

    /**
     * Initialize hooks specific to this integration.
     */
    public function init() {
        add_action( 'my_plugin_error', array( $this, 'handle_plugin_error' ) );
        add_filter( 'my_plugin_setting', array( $this, 'enable_error_logging' ) );
    }

    /**
     * Get registered error types for this integration.
     *
     * @return array Types
     */
    public function get_error_types() {
        return array(
            'api_error'      => __( 'API Connection Error', 'my-plugin' ),
            'validation'     => __( 'Data Validation Error', 'my-plugin' ),
            'payment_failed' => __( 'Payment Processing Error', 'my-plugin' ),
        );
    }

    /**
     * Handle errors from our plugin.
     */
    public function handle_plugin_error( $error_data ) {
        $error = array(
            'type'    => $error_data['type'], // Should match a key from get_error_types()
            'message' => $error_data['message'],
            'user'    => get_current_user_id(),
        );

        // Add additional context if available
        if ( isset( $error_data['context'] ) ) {
            $error['message'] .= "\n\nContext: " . print_r( $error_data['context'], true );
        }

        $this->handle_error( $error );
    }

    /**
     * Enable error logging in our plugin.
     */
    public function enable_error_logging( $value ) {
        // Force enable logging if FEN integration is active
        if ( $this->is_enabled() ) {
            return true;
        }
        return $value;
    }
}

// Initialize the integration
new FEN_My_Plugin();

Integration Registration

Register your integration with Fatal Error Notify using the fen_integrations filter:

/**
 * Register custom integrations.
 *
 * @param array $integrations Existing integrations.
 * @return array Modified integrations.
 */
add_filter( 'fen_integrations', function( $integrations ) {
    $integrations['my-plugin'] = 'My_Plugin_Main_Class';
    return $integrations;
} );

Integration Helper Methods

The FEN_Integration base class provides several helper methods:

// Check if integration is enabled for any notification channel
if ( $this->is_enabled() ) {
    // Integration is active
}

// Get enabled notification channels
$channels = $this->get_enabled_channels(); // Returns array like ['email', 'slack']

// Get enabled error types
$types = $this->get_enabled_types(); // Returns array like ['api_error' => true]

Manual Error Triggering


You can manually trigger error notifications from your code:

Trigger PHP Error Notification

$error = array(
    'type'    => E_WARNING,
    'message' => 'Custom error message from my plugin',
    'file'    => __FILE__,
    'line'    => __LINE__,
    'user'    => get_current_user_id(),
);

fatal_error_notify()->public->handle_error( $error );

Trigger Custom Integration Error

// Assuming you have a custom integration class instance
$integration = fatal_error_notify()->integrations->{'my-plugin'};

$error = array(
    'type'    => 'api_error', // Must match a type from get_error_types()
    'message' => 'API connection failed: timeout after 30 seconds',
    'user'    => get_current_user_id(),
);

$integration->handle_error( $error );

Error Data Structure


Understanding the error data structure helps when working with hooks and filters.

PHP Error Structure

array(
    'type'    => 1,              // PHP error constant (E_ERROR, E_WARNING, etc.)
    'message' => 'Error message',
    'file'    => 'path/to/file.php',
    'line'    => 123,
    'user'    => 1,              // WordPress user ID (if available)
)

Plugin Integration Error Structure

array(
    'type'    => 'plugin_error_type', // Integration slug + underscore + error type
    'message' => 'Error message',
    'user'    => 1,                   // WordPress user ID (if available)
    'url'     => 'https://...',       // Optional: relevant URL
    'file'    => 'path/to/file.php',  // Optional: relevant file
    'line'    => 123,                 // Optional: relevant line number
)

HTTP Error Structure

array(
    'type'    => 'http_error',
    'message' => 'HTTP API error: timeout',
    'url'     => 'https://api.example.com/endpoint',
    'user'    => 1,
)

JavaScript Error Structure

array(
    'type'    => 'js_fatal',
    'message' => 'Uncaught TypeError: Cannot read property...',
    'url'     => 'https://yoursite.com/page',
    'file'    => 'script.js',
    'line'    => 42,
    'user'    => 1,
)