winter icon indicating copy to clipboard operation
winter copied to clipboard

Make errors in components show the correct path

Open multiwebinc opened this issue 4 years ago • 18 comments

Winter CMS Build

1.1

PHP Version

7.4

Database engine

SQLite

Plugins installed

No response

Issue description

If you have an error in a component, the error page does not give any indication of which actual line of code is producing the error either in the error message or the stack trace.

For example:

image

The line given (/www/themes/myTheme/pages/photos.htm line 133) is simply the line where the component is being included into the page. The actual line producing the error can literally be anywhere.

Steps to replicate

This happened to me when I had a model that implemented TranslatableModel, but didn't define a $translatable property.

class MyClass extends Model
{
    // ...
    public $implement = ['@Winter.Translate.Behaviors.TranslatableModel'];

    // public $translatable = ['images'];
}

multiwebinc avatar Feb 08 '22 06:02 multiwebinc

I'm not sure if with Winter you make a habit of modifying/extending the files from other vendors, but in vendor/twig/twig/src/Template.php, if you remove the second catch in displayWithErrorHandling(), it fixes the problem.

    protected function displayWithErrorHandling(array $context, array $blocks = [])
    {
        try {
            $this->doDisplay($context, $blocks);
        } catch (Error $e) {
            if (!$e->getSourceContext()) {
                $e->setSourceContext($this->getSourceContext());
            }

            // this is mostly useful for \Twig\Error\LoaderError exceptions
            // see \Twig\Error\LoaderError
            if (-1 === $e->getTemplateLine()) {
                $e->guess();
            }

            throw $e;
        }
        // catch (\Exception $e) {
        //     $e = new RuntimeError(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $this->getSourceContext(), $e);
        //     $e->guess();

        //     throw $e;
        // }
    }

Now the path and the stack trace represent what's actually causing the error:

image

multiwebinc avatar Feb 08 '22 20:02 multiwebinc

I believe the solution is actually to make the following lines of code work with the current version of Winter: https://github.com/wintercms/winter/blob/develop/modules/system/classes/ErrorHandler.php#L20-L35; but since we're planning on switching to Ignition for the error page handler with the upgrade to Laravel 9 I'll probably look into that then.

LukeTowers avatar Feb 08 '22 21:02 LukeTowers

@LukeTowers Oh, Ignition looks amazing. Laravel 9 was also released today too.

multiwebinc avatar Feb 08 '22 22:02 multiwebinc

Yes, I'll be working on finishing up support for Laravel 9 this week.

LukeTowers avatar Feb 08 '22 22:02 LukeTowers

This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days. If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue. If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

github-actions[bot] avatar Apr 10 '22 00:04 github-actions[bot]

This issue will be closed and archived in 3 days, as there has been no activity in this issue for the last 6 months. If this issue is still relevant or you would like to see it actioned, please respond within 3 days. If this issue is critical for your business, please reach out to us at [email protected].

github-actions[bot] avatar Oct 10 '22 00:10 github-actions[bot]

@multiwebinc Winter 1.2 with Laravel 9 support is now out. Have you had a chance to test this out on Winter 1.2?

bennothommo avatar Oct 10 '22 01:10 bennothommo

@bennothommo Sorry for the delay. I just tried with 1.2.0 and it hasn't been fixed.

multiwebinc avatar Oct 21 '22 00:10 multiwebinc

I think this happens when there is an error in the component's onRender() method. I tried adding an error to several other methods and it displays the error correctly.

e.g. This outputs the error like in the first screenshot:

public function onRender()
{
    throw new \Exception('test');
}

multiwebinc avatar Oct 21 '22 04:10 multiwebinc

@multiwebinc are you able to try out the ignition plugin and see if that helps? You might have to use this WIP PR: https://github.com/wintercms/wn-ignition-plugin/pull/2 but @robertalexa should be finishing it up within the week.

LukeTowers avatar Oct 24 '22 19:10 LukeTowers

Sorry for the long delay. I just installed via composer require winter/wn-ignition-plugin:dev-wip/1.2 and then in a plugin added the code from my previous comment and I get a 500 response with the following errors in the log:

2023/02/01 23:25:05 [error] 2005954#2005954: *12419 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught TypeError: Winter\Ignition\Middleware\AddWinterContextData::handle(): Argument #1 ($report) must be of type Facade\FlareClient\Report, Spatie\FlareClient\Report given, called in /path/to/winter/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php on line 180 and defined in /path/to/winter/plugins/winter/ignition/middleware/AddWinterContextData.php:12
Stack trace:
#0 /path/to/winter/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Winter\Ignition\Middleware\AddWinterContextData->handle()
#1 /path/to/winter/vendor/spatie/flare-client-php/src/FlareMiddleware/AddSolutions.php(28): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#2 /path/to/winter/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Spatie\FlareClient\FlareMiddleware\AddSolutions->handle()
#3 /home/mike/Seafile/www/laravel/octo...PHP message: PHP Fatal error:  Uncaught TypeError: Winter\Ignition\Middleware\AddWinterContextData::handle(): Argument #1 ($report) must be of type Facade\FlareClient\Report, Spatie\FlareClient\Report given, called in /path/to/winter/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php on line 180 and defined in /path/to/winter/plugins/winter/ignition/middleware/AddWinterContextData.php:12
Stack trace:
#0 /path/to/winter/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Winter\Ignition\Middleware\AddWinterContextData->handle()
#1 /path/to/winter/vendor/spatie/flare-client-php/src/FlareMiddleware/AddSolutions.php(28): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#2 /path/to/winter/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Spatie\FlareClient\FlareMiddleware\AddS

multiwebinc avatar Feb 02 '23 05:02 multiwebinc

@multiwebinc can you try again with the main branch of the Ignition plugin?

LukeTowers avatar Feb 26 '23 14:02 LukeTowers

@LukeTowers No, that doesn't appear to work for me. If I throw an Exception in a component's onRender method, the error page does not mention the file the error was thrown in.

image

multiwebinc avatar Feb 27 '23 03:02 multiwebinc

@multiwebinc I think the solution (for both Winter default and Ignition) is to get https://github.com/wintercms/winter/blob/develop/modules/system/classes/ErrorHandler.php#L20-L35 working again, do you have a few minutes to play around with that as an example to see where it needs to live now in order to take care of the issue?

LukeTowers avatar Feb 27 '23 08:02 LukeTowers

This issue will be closed and archived in 3 days, as there has been no activity in this issue for the last 6 months. If this issue is still relevant or you would like to see it actioned, please respond within 3 days. If this issue is critical for your business, please reach out to us at [email protected].

github-actions[bot] avatar Aug 29 '23 00:08 github-actions[bot]

This issue will be closed and archived in 3 days, as there has been no activity in this issue for the last 6 months. If this issue is still relevant or you would like to see it actioned, please respond within 3 days. If this issue is critical for your business, please reach out to us at [email protected].

github-actions[bot] avatar Feb 28 '24 00:02 github-actions[bot]