#I can't render default 404 page with method ->withExceptions() from bootstrap\app.php

9 messages · Page 1 of 1 (latest)

ionic stone
#

in my laravel 10, I have a app\Exceptions\Handler.php class like this?

<?php

namespace App\Exceptions;

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

class Handler extends ExceptionHandler
{
    protected $dontReport = [];

    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    public function register()
    {
        $this->reportable(function (Throwable $e) {});
    }

    public function render($request, Throwable $e)
    {
        if ($request->is('api/*')) {
            return response()->json(['error' => 404, 'message' => 'route_not_found'], 404);
        } else {
            return parent::render($request, $e);
        }
    }
}

now, in Laravel 11 i have the following in the bootstrap\app.php:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        commands: __DIR__ . '/../routes/console.php',
        api: __DIR__ . '/../routes/api.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->api(append: [
            \App\Http\Middleware\ApiResponse::class
        ])
            ->alias([
                'auth' => \App\Http\Middleware\Authenticate::class
            ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        $exceptions->render(function (Throwable $e, \Illuminate\Http\Request $request) {
            if ($request->is('api/*')) {               
                return response()->json(['error' => 404, 'message' => 'route_not_found'], 404);
            } else
                abort(404);
        });
    })->create();

the thing is I wish to use something like the return parent::render($request, $e); from laravel 10, since it return the default 404 page of the framework. The abort(404) in laravel 11 return a generic 500 error. How can I render the default 404 laravel error page?

orchid sundial
#

Hi, i'm solved using AppServiceProvider!

public function boot(): void
    {
        $this->app->singleton(
            \Illuminate\Contracts\Debug\ExceptionHandler::class,
            Handler::class
        );
    }
#
->withExceptions(function (Exceptions $exceptions) {
      //
        });
ionic stone
orchid sundial
#
composer dump-autoload
ionic stone
#

this error was during composer dump-autoload

orchid sundial
#

composer update

ionic stone
#

it's not a composer issue

orchid sundial
#

🤔