flugg / laravel-responder

A Laravel Fractal package for building API responses, giving you the power of Fractal with Laravel's elegancy.
MIT License
861 stars 86 forks source link

error using composer update #159

Closed aligajani closed 4 years ago

aligajani commented 4 years ago
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
PHP Fatal error:  Declaration of Flugg\Responder\Exceptions\Handler::render($request, Throwable $exception) must be compatible with Illuminate\Foundation\Exceptions\Handler::render($request, Exception $e) in /Users/aligajani/Sites/shadow-platform/vendor/flugger/laravel-responder/src/Exceptions/Handler.php on line 16
PHP Fatal error:  Uncaught ReflectionException: Class App\Exceptions\Handler does not exist in /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php:803
Stack trace:
#0 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(803): ReflectionClass->__construct('App\\Exceptions\\...')
#1 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(681): Illuminate\Container\Container->build('App\\Exceptions\\...')
#2 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(785): Illuminate\Container\Container->resolve('App\\Exceptions\\...', Array, false)
#3 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(265): Illuminate\Foundation\Application->resolve('App\\Exceptions\\...', Array, false)
#4 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container. in /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 805
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 255
flugg commented 4 years ago

Hi @aligajani ,

I was afraid this might be a problem. What version of PHP and Laravel are you running on?

They changed the render method signature in v7.0 of Laravel.

aligajani commented 4 years ago

My composer.json is below

PHP is 7.3 on mac Sierra.

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.2",
        "ext-json": "*",
        "aws/aws-sdk-php": "^3.112",
        "barryvdh/laravel-cors": "^0.11.4",
        "browner12/helpers": "^3.0",
        "bugsnag/bugsnag-laravel": "^2.17",
        "doctrine/dbal": "^2.9",
        "fideloper/proxy": "^4.2",
        "flugger/laravel-responder": "^3.0",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/framework": "^6.0",
        "laravel/nova": "*",
        "laravel/helpers": "^1.1",
        "laravel/tinker": "^1.0",
        "laravel/vapor-core": "^2.2",
        "league/flysystem-aws-s3-v3": "~1.0",
        "maatwebsite/excel": "^3.1",
        "nickurt/laravel-postcodeapi": "^1.10",
        "omniphx/forrest": "^2.5",
        "predis/predis": "^1.1",
        "rinvex/countries": "^6.1",
        "tymon/jwt-auth": "1.0.0-rc.5",
        "webpatser/laravel-uuid": "^3.0",
        "wildbit/swiftmailer-postmark": "^3.1"
    },
    "require-dev": {
        "facade/ignition": "^1.4",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^3.0",
        "phpunit/phpunit": "^8.0"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "repositories": [
        {
            "type": "path",
            "url": "./nova"
        }
    ]
}
aligajani commented 4 years ago

@flugger I am not going up to laravel 7.0. I was just upgrading my 6.x.

Guess they changed it there too?

flugg commented 4 years ago

The problem is I changed the typehint from \Exception to \Throwable to follow the v7 changes - I figured it wouldn't break because \Exception extends \Throwable, but they only allowed this type of signature change from PHP 7.4+. However, I see that PHP 7.2+ allows implementation without a typehint registered in an interface, so I just made the change and tagged v3.1.1. Can you check if a composer update works now?

aligajani commented 4 years ago

@flugger

Same

> @php artisan package:discover --ansi
PHP Fatal error:  Declaration of App\Exceptions\Handler::render($request, Exception $exception) must be compatible with Flugg\Responder\Exceptions\Handler::render($request, $exception) in /Users/aligajani/Sites/shadow-platform/app/Exceptions/Handler.php on line 17
PHP Fatal error:  Uncaught ReflectionException: Class App\Exceptions\Handler does not exist in /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php:803
Stack trace:
#0 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(803): ReflectionClass->__construct('App\\Exceptions\\...')
#1 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(681): Illuminate\Container\Container->build('App\\Exceptions\\...')
#2 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(785): Illuminate\Container\Container->resolve('App\\Exceptions\\...', Array, false)
#3 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(265): Illuminate\Foundation\Application->resolve('App\\Exceptions\\...', Array, false)
#4 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container. in /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 805
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 255

Using "flugger/laravel-responder": "^3.0",

flugg commented 4 years ago

Right, I guess this can be fixed by removing the Exception typehint in the render method of your App\Exceptions\Handler class then.

I know it's not ideal, but a new major version of the package will soon be released which should solve this, so it's just a temporary fix.

aligajani commented 4 years ago

@flugger I have the below, where do you mean exactly

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Flugg\Responder\Exceptions\Handler as ExceptionHandler;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\RelationNotFoundException;
use Illuminate\Validation\ValidationException;
use Omniphx\Forrest\Exceptions\SalesforceException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

//use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{

    /**
     * A list of default exception types that should not be converted.
     *
     * @var array
     */
    protected $dontConvert = [
//        ModelNotFoundException::class,
//        ValidationException::class,
//        AuthenticationException::class,
//        AuthorizationException::class,
//        NotFoundHttpException::class,
//        RelationNotFoundException::class
    ];

    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        if ($exception instanceof SalesforceException) {

            $response = $exception->getResponse();
            $request = $exception->getRequest();

            logger('SALESFORCE_EXCEPTION:REQUEST_URL:' . $request->getUri());
            logger('SALESFORCE_EXCEPTION:RESPONSE_STATUS:' .$response->getStatusCode());
            logger('SALESFORCE_EXCEPTION:RESPONSE_BODY:' .$response->getBody()->getContents());
        }

        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }

    /**
     * Convert an authentication exception into a response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Illuminate\Auth\AuthenticationException $exception
     * @return \Illuminate\Http\JsonResponse
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }
}
flugg commented 4 years ago

I suspect things will work if you replace:

    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }

With:

    public function render($request, $exception)
    {
        return parent::render($request, $exception);
    }
aligajani commented 4 years ago

@flugger Same error again :(

flugg commented 4 years ago

Hmm, that sounds very strange. What does the error say exactly? Declaration of App\Exceptions\Handler::render($request, Exception $exception) must be compatible with Flugg\Responder\Exceptions\Handler::render($request, $exception)?

aligajani commented 4 years ago

@flugger This:

Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
PHP Fatal error:  Declaration of App\Exceptions\Handler::render($request, Exception $exception) must be compatible with Flugg\Responder\Exceptions\Handler::render($request, $exception) in /Users/aligajani/Sites/shadow-platform/app/Exceptions/Handler.php on line 17
PHP Fatal error:  Uncaught ReflectionException: Class App\Exceptions\Handler does not exist in /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php:803
Stack trace:
#0 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(803): ReflectionClass->__construct('App\\Exceptions\\...')
#1 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(681): Illuminate\Container\Container->build('App\\Exceptions\\...')
#2 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(785): Illuminate\Container\Container->resolve('App\\Exceptions\\...', Array, false)
#3 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(265): Illuminate\Foundation\Application->resolve('App\\Exceptions\\...', Array, false)
#4 /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container. in /Users/aligajani/Sites/shadow-platform/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 805
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 255
flugg commented 4 years ago

So it still says Declaration of App\Exceptions\Handler::render($request, Exception $exception). Are you sure you made the correct adjustments in the Handler class because this error message contradicts this. It shouldn't say Exception $exception when the render method doesn't have the typehint anymore.

aligajani commented 4 years ago

@flugger My bad, I was updating the wrong place. Fixing.

aligajani commented 4 years ago

Thank you @flugger for your help. It fixed things.

flugg commented 4 years ago

No problems, glad you worked it out. Just ask if you should have more questions!