Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Prevent E_NOTICE, E_WARNING, E_DEPRECATED errors WSOD in Sage 10
<?php
namespace App\Providers;
use Roots\Acorn\ServiceProvider;
class ThemeServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->booted(
function($app) {
set_error_handler(function ($level, $message, $file = '', $line = 0, $context = []) {
// Check if this error level is handled by error reporting
if (error_reporting() & $level) {
// Return false for any error levels that should
// be handled by the built in PHP error handler.
if ($level & (E_WARNING | E_NOTICE | E_DEPRECATED)) {
echo "<br />\n";
echo "<b>ERROR</b> [$level] $message<br />\n";
echo " Fatal error on line $line in file $file";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
return false;
}
// Throw an exception to be handled by Laravel for all other errors.
throw new \ErrorException($message, 0, $level, $file, $line);
}
});
}
);
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
}
}
@mike-sheppard

This comment has been minimized.

Copy link

@mike-sheppard mike-sheppard commented May 7, 2021

thanks for this @cfaria, really useful!

I've added some quick & dirty styles + notification to Slack if not in Dev - not sure if you'll find it useful but thought I'd share.

Screenshot_2021-05-07_at_08_03_21-2

// ThemeServiceProvider.php
<?php

namespace App\Providers;

use Roots\Acorn\ServiceProvider;
use Illuminate\Support\Facades\Log;
use ErrorHelper;

class ThemeServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->booted(
            function($app) {
                set_error_handler(function ($level, $message, $file = '', $line = 0, $context = []) {
                    // Check if this error level is handled by error reporting
                    if (error_reporting() & $level) {
                        // Return false for any error levels that should be handled by the built in PHP error handler.
                        if ($level & (E_WARNING | E_NOTICE | E_DEPRECATED)) {
                            $error_label = ErrorHelper::getErrorTypeByValue($level) ?? '';
                            echo "<pre style='position: fixed; z-index: 9999999; bottom: 1rem; right: 1rem; background: #eee; padding: 1rem; border-radius: 5px; box-shadow: 0 0 5px rgb(0 0 0 / 10%); border-left: 5px solid #ff5722; line-height: 1.8; max-width: 80%; overflow: scroll;'>";
                            echo '<div style="position: absolute; top: 0; right: 0; font-size: 2rem; cursor: pointer; padding: 5px; line-height: 1;" onclick="return this.parentNode.remove();">&times;</div>';
                            echo "<b>{$error_label}</b> {$message}\n";
                            echo "File: {$file}\n";
                            echo "Line: {$line}";
                            echo '</pre>';

                            // Also add to logfile / slack
                            $logfile_message = "Error displayed: {$error_label} - {$message}\n File: {$file}\n Line: {$line}";
                            (WP_ENV === 'development') ? Log::info($logfile_message) : Log::channel('slack')->warning($logfile_message);

                            return false;
                        }

                        // Throw an exception to be handled by Laravel for all other errors.
                        throw new \ErrorException($message, 0, $level, $file, $line);
                    }
                });
            }
        );

    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}
// ErrorHelper.php
<?php
class ErrorHelper
{
    public static function getErrorTypeByValue($type)
    {
        $constants = get_defined_constants(true);

        foreach ($constants['Core'] as $key => $value) { // Each Core constant
            if (preg_match('/^E_/', $key)) { // Check error constants
                if ($type == $value) {
                    return $key;
                }

            }
        }
    }

}
@cfaria

This comment has been minimized.

Copy link
Owner Author

@cfaria cfaria commented May 7, 2021

Wow!!! Great @mike-sheppard !! I was struggling a bit with the environment and echoing the message as I'm using this in a project that has WooCommerce and, in the checkout page, if I echo the error (the one that you have styled), the review order part, that refreshes with AJAX (order items and totals), stops working... just to let you know...

Maybe it has sth to do with echoing before rendering...

Anyway...

@mike-sheppard

This comment has been minimized.

Copy link

@mike-sheppard mike-sheppard commented May 7, 2021

aha yeah I see, how about only outputting to admin_footer/footer and prevent display in ajax, ie. just log to the logfile?

Something like this..

// app/Providers/ThemeServiceProvider.php
<?php

namespace App\Providers;

use ErrorHelper;
use function Roots\view;
use Illuminate\Support\Facades\Log;
use Roots\Acorn\ServiceProvider;

class ThemeServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->booted(
            function ($app) {
                set_error_handler(function ($level, $message, $file = '', $line = 0, $context = []) {
                    // Check if this error level is handled by error reporting
                    if (error_reporting() & $level) {
                        // Return false for any error levels that should be handled by the built in PHP error handler.
                        if ($level & (E_WARNING | E_NOTICE | E_DEPRECATED)) {
                            $error_label = ErrorHelper::getErrorTypeByValue($level) ?? '';
                            $error_output = view('admin.error', [
                                'error_label' => $error_label,
                                'message'     => $message,
                                'file'        => $file,
                                'line'        => $line,
                            ]);

                            if (is_admin() && !wp_doing_ajax()) {
                                add_action('admin_footer', function () use ($error_output) {
                                    echo $error_output;
                                });
                            } elseif (!wp_doing_ajax()) {
                                add_action('footer', function () use ($error_output) {
                                    echo $error_output;
                                });
                            }

                            // Also add to logfile / slack
                            $logfile_message = "Error displayed: {$error_label} - {$message}\n File: {$file}\n Line: {$line}";
                            (WP_ENV === 'development') ? Log::info($logfile_message) : Log::channel('slack')->warning($logfile_message);

                            return false;
                        }

                        // Throw an exception to be handled by Laravel for all other errors.
                        throw new \ErrorException($message, 0, $level, $file, $line);
                    }
                });
            }
        );

    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}
// ErrorHelper.php
<?php
class ErrorHelper
{
    public static function getErrorTypeByValue($type)
    {
        $constants = get_defined_constants(true);

        foreach ($constants['Core'] as $key => $value) { // Each Core constant
            if (preg_match('/^E_/', $key)) { // Check error constants
                if ($type == $value) {
                    return $key;
                }

            }
        }
    }

}

views/admin/error.blade.php

<pre style='position: fixed; z-index: 9999999; bottom: 1rem; right: 1rem; background: #eee; padding: 1rem; border-radius: 5px; box-shadow: 0 0 5px rgb(0 0 0 / 10%); border-left: 5px solid #ff5722; line-height: 1.8; max-width: 80%; overflow: scroll;'>
  <div style="position: absolute; top: 0; right: 0; font-size: 2rem; cursor: pointer; padding: 5px; line-height: 1;" onclick="return this.parentNode.remove();">&times;</div>
  <strong>{{ $error_label }}</strong> {{ $message }}
  File: {{ $file }}
  Line: {{ $line }}
</pre>
@mike-sheppard

This comment has been minimized.

Copy link

@mike-sheppard mike-sheppard commented May 7, 2021

In your ajax scenario, maybe return with one of these?

if (wp_doing_ajax()) {
    ...
    wp_send_json( $response:mixed, $status_code:integer|null, $options:integer )
    wp_send_json_error( $data:mixed|null, $status_code:integer|null, $options:integer )
    ...
}

Docs: wp_send_json
Docs: wp_send_json_error

@cfaria

This comment has been minimized.

Copy link
Owner Author

@cfaria cfaria commented May 7, 2021

Works like a charm Mike... even with Ajax requests without using wp_send_json or wp_send_json_error.

Thank you again ;D

@gocsp

This comment has been minimized.

Copy link

@gocsp gocsp commented Oct 15, 2021

🙏 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment