jenssegers / lean

Use the PHP League's Container package with auto-wiring support as the core container in Slim 3
31 stars 5 forks source link

errorHandler not being handled #8

Closed fastsol closed 5 years ago

fastsol commented 5 years ago

I have a slim project that has a custom errorHandler setup. It was working great before switching over to the league container. I have made a service provider for the errorHandler after trying the basic way of $container->share('errorHandler' function{}); and returning the custom handler.

This is the customer handler:

use \Exception;
use \ReflectionClass;

class Handler
{
    public function __invoke($request, $response, $exception)
    {
        die('HERE');
        $shortName = $this->getExceptionShortName($exception);

        if(method_exists($this, $handler = "handle{$shortName}")){
            return $this->{$handler}($request, $response, $exception);
        }

        return $this->rethrowException($exception);
    }

    protected function getExceptionShortName(Exception $e)
    {
        return (new ReflectionClass($e))->getShortName();
    }

    protected function handleValidationFailedException($request, $response, $exception)
    {
        return $exception->getReturn();
    }

    protected function handleFileNotFoundException($request, $response, $exception)
    {
        return $response->withStatus(404)->write('Not Found');
    }

    protected function rethrowException(Exception $e)
    {
        throw $e;
    }
}

Then I try to throw this exception in the controller:

namespace App\Exceptions;

class ValidationFailedException extends \Exception
{
    protected $request;
    protected $response;
    protected $container;
    protected $errors;

    public function __construct($request, $response, $container, string $errors)
    {
        $this->request = $request;
        $this->response = $response;
        $this->container = $container;
        $this->errors = $errors;
    }

    protected function getPath()
    {
        return $this->request->getUri()->getPath();
    }

    public function getReturn()
    {
        if($this->request->getParam('ajax')){
            return $this->container->get('ajaxJson')->message($this->errors)->sendResponse($this->response);
        }

        return $this->response->withRedirect($this->getPath());
    }
}

This is the code that throws the exception:

if($check->failed()){
            throw new ValidationFailedException($request, $response, $this->container, $this->errorMessageBox($check->errors()));
        }

The furthest that the exception gets is the __construct and then the browser just returns the exception rather than calling the getReturn() like it should. I'm stuck on this.

fastsol commented 5 years ago

This is in my service provider

public function register()
    {
        $container = $this->getContainer();

        $container->share('errorHandler', function(){
            return new Handler();
        });
    }

And this is how it's added $container->addServiceProvider(new ErrorHandlerServiceProvider());

jenssegers commented 5 years ago

Are you using the version on dev-master or the last tagged version?

jenssegers commented 5 years ago

I just tagged a v1.0.1 version, can you check if that fixes it?

fastsol commented 5 years ago

The new version fixed that issue, thank you very much. I'm finding another issue now though, I'll make another post about it.