Kirby is mean to me and won't let me dirty debug :)

Hi there,

I am quickly hacking away on some dirty panel stuff with API calls. Specifically, I am calling APIs when the status of a blog post changes to “listed”. I do this by using a hook in the config.php:

    'hooks' => [
        'page.changeStatus:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage) {
            if($newPage->status() == "listed" && $newPage->includeInNewsletter() !== null){

                $data = [
                    'name'      => $newPage->slug(),
                    'type'      => 'regular',
                    segments' => json_decode($newPage->includeInNewsletter(), true), // We want to convert the JSON into an associative array
                    'emails'    => [
                        [
                        'subject' => $newPage->title()->toString(),
                        'from_name' => 'xxxxxxx',
                        'from' => 'office@xxxxxx.xxx',
                        'content' => snippet('newsletterNotification', ['article' => $newPage ], true)
                        ]
                    ]
                ];
                $request = Remote::request('https://connect.xxxxxx.com/api/xxxxx', [
                    'method'  => 'POST',
                    'headers' => [
                        'Content-Type: application/json',
                        'Accept: application/json',
                        'Authorization: Bearer '.option('xxxxx.xxxxx')
                    ],
                    'data'    => json_encode($data),
                ]);

                if ($response->code() != 200) {
                    throw new Exception('Error posting to xxxxx, please try again.');
                }
            }

Now, I get this error here in the panel, since obviously there was an error in the API request:

Now, the obvious thing to do is to check $data. But no matter what I do - die(), print_r(), var_dump() or any combination such as die(print_r($data)); the panel vanishes and there is redirect to a different URL showing this screen:

I get it. I am hacking really dirty in the panel. Okay. But please, I only need to see this variables output, fix the error and be done with it without installing xdebug or anything else complicated.

What am I doing wrong here?

Thanks
Andreas

One way would be to test this code outside the Panel context, e.g. in a template.

Another option could be to throw an exception $data variable.

But there is an obvious error in your code

You assign the remote request to a variable $request, but then check the response code on an undefined variable $response. I’m a bit surprised your IDE doesn’t complain about an undefined variable…

There’s also a single quote missing before segments

Thanks, I indeed missed that!

That is a copy and paste error - the ’ is there.

Thanks, I will give the exception idea a try!

@texnixe : Throwing an exception did not work, neither did declaring an anonymous function.

I don’t know how I should use a template for this.

What I don’t understand: if I die() in PHP, any output should stop immediately afterwards, no? It seems like the frontend is still doing stuff via API/JS. Any way to stop that?

Thanks
Andreas

I use xDebug and Ray app for debugging.

Another option could be to write to the PHP error log. (error_log())

Right, so the PHP Error Log Version did not work either.

In the end I “debugged” by outputting the information via a template and then “blind coding” in the config.php until it worked.

Thanks for your help @texnixe .

Andreas