zeuxisoo / php-slim-whoops

PHP whoops error on slim framework
131 stars 10 forks source link

Fatal error: Call to undefined method Closure::getContainer() #13

Closed zhiephie closed 8 years ago

zhiephie commented 8 years ago

I see, https://github.com/zeuxisoo/php-slim-whoops/issues/12 but not working for me, can your help

zeuxisoo commented 8 years ago

Hello, i tested my code and upgrade the slim to 3.3.*, it's work. So, could you mind to provide code to reproduce you problem ?

zhiephie commented 8 years ago

after update to slim 3.3.*

Type: Error
Message: Call to undefined method Closure::getContainer()
File: /home/dev2/repos/arsitagx/vendor/zeuxisoo/slim-whoops/src/Zeuxisoo/Whoops/Provider/Slim/WhoopsMiddleware.php
Line: 13
Trace

#0 [internal function]: Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#1 /home/dev2/repos/arsitagx/vendor/slim/slim/Slim/DeferredCallable.php(37): call_user_func_array(Object(Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware), Array)
#2 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#3 /home/dev2/repos/arsitagx/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(67): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#4 /home/dev2/repos/arsitagx/vendor/tuupola/slim-jwt-auth/src/JwtAuthentication.php(80): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 [internal function]: Slim\Middleware\JwtAuthentication->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#6 /home/dev2/repos/arsitagx/vendor/slim/slim/Slim/DeferredCallable.php(37): call_user_func_array(Object(Slim\Middleware\JwtAuthentication), Array)
#7 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#8 /home/dev2/repos/arsitagx/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(67): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#9 /home/dev2/repos/arsitagx/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(116): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#10 /home/dev2/repos/arsitagx/vendor/slim/slim/Slim/App.php(337): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#11 /home/dev2/repos/arsitagx/vendor/slim/slim/Slim/App.php(298): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#12 /home/dev2/repos/arsitagx/app/server.php(41): Slim\App->run()
#13 /home/dev2/repos/arsitagx/public/index.php(3): require('/home/dev2/repo...')
#14 {main}

in middleware.php

use Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware;

$app->add(new WhoopsMiddleware);

What wrong?

zeuxisoo commented 8 years ago

Base on this call stack, I can't find out why the getContainer() is undefined if the WhoopsMiddleware is first added. So, can you make a simple example for me to reproduce this issue ?

Because i cannot reproduce your issue in my examples with slim 3.3.*.

zhiephie commented 8 years ago

okey sample code

server.php //server running
// Instantiate the app
$settings = require PATH_ROOT.'config/setting.php';
$app = new \Slim\App($settings);

// Set up dependencies
require PATH_ROOT.'app/dependency.php';

// Register middleware
require PATH_ROOT.'app/middleware.php';

// Register routes
require PATH_ROOT.'app/route.php';

// Register database
require PATH_ROOT.'config/database.php';

// Run app
$app->run();

in dependece.php

$container = $app->getContainer();

I think the troubled in $app = new \Slim\App($settings);

zeuxisoo commented 8 years ago

I agree with you. Because the dependencies.php only contain $container = $app->getContainer();, it will not change the middleware stack, So what is the content of $settings ?

zhiephie commented 8 years ago

content of setting.php

return [
    'settings' => [
        'debug' => true,
        'whoops.editor' => 'sublime',
        'displayErrorDetails' => true, // set to false in production

        // PHP Renderer settings
        'renderer' => [
            'template_path' => PATH_ROOT.'component/',
        ],

@zeuxisoo Ok working for error with whoops. Question if debug set to false error not like :Whoops, looks like something went wrong.

I'm set in dependence.php errorHanlde like

// error handle
$container['errorHandler'] = function ($c) {
  return function ($request, $response, $exception) use ($c) {
    $data = [
      'code' => $exception->getCode(),
      'message' => $exception->getMessage(),
      'file' => $exception->getFile(),
      'line' => $exception->getLine(),
      'trace' => explode("\n", $exception->getTraceAsString()),
    ];

    return $c->get('response')->withStatus(500)
             ->withHeader('Content-Type', 'application/json')
             ->write(json_encode($data));
  };
};
zeuxisoo commented 8 years ago

Oh guys, The whoops middleware will update the errorHandler automatically. You don't need to custom the errorHandler if you using this middleware. Because it will replaced.

But, In your question, do you mean that if running on production, the debug set to false, it will show the original slim error ? But when you in the development mode, you want a whoops error ?

If yes, the code will like:

$app = new App([
    'settings' => [
        'debug'         => false,
        'whoops.editor' => 'sublime',
    ]
]);

if ($app->getContainer()->settings['debug'] === false) {
    $container['errorHandler'] = function ($c) {
        return function ($request, $response, $exception) use ($c) {
            $data = [
                'code' => $exception->getCode(),
                'message' => $exception->getMessage(),
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
                'trace' => explode("\n", $exception->getTraceAsString()),
            ];

            return $c->get('response')->withStatus(500)
                    ->withHeader('Content-Type', 'application/json')
                    ->write(json_encode($data));
        };
    };
}else{
    $app->add(new WhoopsMiddleware);
}
zhiephie commented 8 years ago

Hai @zeuxisoo I'm set production debug to false, but I not will show the original slim error.

yapss, me want a whoops error like this website https://laramap.com/

zeuxisoo commented 8 years ago

Um, This website is is using the laravel base on the error message. And this error style is handle by the symfony not whoops library.

The path and source location when installed laravel: vendor/symfony/debug/ExceptionHandler.php

So, Back to your question, I think you need to ask in slim not here. because it is not handled by whoops.