<?php
defined( 'ABSPATH' ) or exit;   // show a blank page if try to access this file directly

class EWZ_Exception extends Exception {

    /**
     * Make sure an error is logged ( when error logging is on ) as soon as it happens
     *
     * When error logging is on, make the constructor log the exception
     * Generate the message from $user_message, plus the input data if present.
     *
     * @param   string   $user_message  The message to be translated and passed to the Exception
     * @param   mixed    $data1         Extra data to be logged but not displayed
     */
    public function __construct( $user_message, $data1='' ) {
        // assert( is_string( $user_message ) );
        if(  empty( $data1 ) ){
            $data1 = '#';
        } elseif(  is_string( $data1 ) || is_int( $data1 ) ){
            $data1 = esc_attr( $data1 );
        } else {
            $data1 = serialize( esc_attr( $data1 ) );
        }
        $data1 .= ' {user ' . get_current_user_id() . '}';

        // format in one line for error log. ~ is replaced with newline for js alerts
        $logmsg = sprintf( "%s ;; %s", $user_message, $data1 );

        $file = str_replace( EWZ_PLUGIN_DIR, '', parent::getFile() );

        error_log( "EWZ: $logmsg~{ $file, line " . parent::getLine() . " }~" );

        $umsg = wp_kses( $user_message, array( 'br' => array(),
                                             ));

        // just in case of a blank message, which shouldn't happen
        if( !preg_match( '/\w/',  $umsg ) ){
            $umsg = "$file : " . parent::getLine();
        }
        parent::__construct( $umsg, 0, null );

        $preamble = "*** EntryWizard ERROR ***";

        $this->message = "$preamble " . str_replace( $preamble, '', $this->message );

    }

}
