inertiajs.com icon indicating copy to clipboard operation
inertiajs.com copied to clipboard

(Docs) Change `$response->status()` to `$response->getStatusCode()` in error-handling documentation

Open chrislow opened this issue 3 years ago • 0 comments

I found out, that running phpstan threw an error at this part of the error-handling code documented here. I added a pull request to change the documented error-handling code. Feel free to merge it.

Further explanation:

# The command i ran
vendor/bin/phpstan analyse app -c ./vendor/nunomaduro/larastan/extension.neon  --level=4 --no-progress --memory-limit=2G

# The errors i got: 
------ ---------------------------------------------------------------------------------------------------------------------
 Line   Exceptions/Handler.php
------ ---------------------------------------------------------------------------------------------------------------------
 63     Call to an undefined method Symfony\Component\HttpFoundation\Response::status().
 64     Call to an undefined method Symfony\Component\HttpFoundation\Response::status().
 64     Method App\Exceptions\Handler::render() should return Illuminate\Http\RedirectResponse|Inertia\Response but returns
        Symfony\Component\HttpFoundation\Response.
 66     Call to an undefined method Symfony\Component\HttpFoundation\Response::status().
 67     Call to an undefined method Symfony\Component\HttpFoundation\Response::status().
 73     Method App\Exceptions\Handler::render() should return Illuminate\Http\RedirectResponse|Inertia\Response but returns
        Symfony\Component\HttpFoundation\Response.
------ ---------------------------------------------------------------------------------------------------------------------

Change $response->status() to $response->getStatusCode()

    public function render($request, Throwable $e)
    {
        $response = parent::render($request, $e);

        if (!app()->environment(['local', 'testing']) && in_array($response->getStatusCode(), [500, 503, 404, 403])) {
            return Inertia::render('Error', ['status' => $response->getStatusCode()])
                ->toResponse($request)
                ->setStatusCode($response->getStatusCode());
        } else if ($response->getStatusCode() === 419) {
            return back()->with([
                'message' => 'The page expired, please try again.',
            ]);
        }

        return $response;
    }

chrislow avatar Nov 18 '22 13:11 chrislow