Laravel 内部错误页面显示异常消息而不是一般错误消息

Laravel Internal Error Page is showing exception message instead generic error message

我有一个 laravel 5.7 应用程序,当我停用调试模式时,内部服务器错误页面一直显示异常消息而不是一般消息 "Whoops, something went wrong on our servers!"。

这里是异常处理器的代码:

    <?php

namespace App\Exceptions;

use Doctrine\DBAL\Query\QueryException;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        \Illuminate\Auth\AuthenticationException::class,
        \Illuminate\Auth\Access\AuthorizationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Session\TokenMismatchException::class,
        \Illuminate\Validation\ValidationException::class,
        \Illuminate\Database\QueryException::class,
    ];

    /**
     * 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)
    {
        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 an unauthenticated response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

        return redirect()->guest('login');
    }
}

我没有自定义 500 错误视图,默认情况下是异常处理程序。 有谁知道为什么会这样?

此致

文件 .env

APP_DEBUG=false

文件config/app.php

'debug' => env('APP_DEBUG', false),

运行 命令

php artisan config:cache

你可以在 app/Exceptions/Handler.php 中做这样的事情:

if ($this->isHttpException($exception)) {

        if ($exception->getStatusCode() == 404) {
            return response()->view('partials.error' . '_404', [], 404);
        }

        if ($exception->getStatusCode() == 403) {
            return response()->view('partials.error' . '_403', [], 403);
        }
    }

    if ($exception instanceof QueryException) {
        abort(500);
    }

此外,您可以使 instanceof ErrorException 更通用

好吧,我们真的没有什么可继续的。如果让我猜测的话,我会说您似乎在尝试调用您尚未创建的控制器 AdsController。 在您的 app/Http/Controllers 目录中确认您有一个名为 AdsController.php

的文件

---编辑---

您要做的是在生产中将 .env 文件中的 APP_DEBUG 设置为 false。但是在您的开发或离线环境中,您会希望将该选项设置为 true,以便为您提供您习惯于获得的可靠的详细错误消息。

问题已解决!

我 运行 全新安装了 composer,异常消息消失了。

感谢您到目前为止给出的所有答案