【问题标题】:Laravel Internal Error Page is showing exception message instead generic error messageLaravel 内部错误页面显示异常消息而不是通用错误消息
【发布时间】:2019-10-24 10:28:31
【问题描述】:

我有一个 laravel 5.7 应用程序,当我停用调试模式时,内部服务器错误页面一直显示异常消息,而不是一般消息“糟糕,我们的服务器出了点问题!”。

这里是异常处理程序的代码:

    <?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 等于true
  • APP_DEBUG 为假,我不想激活 degu 模式。我只是不想让用户在前端看到异常消息。
  • 请提供app/Exceptions/Handler.php的代码
  • Exceptions\Handler 代码添加到问题@SerhiiPosternak

标签: laravel laravel-5.7 internal-server-error


【解决方案1】:

文件 .env

APP_DEBUG=false

文件配置/app.php

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

运行命令

php artisan config:cache

【讨论】:

  • 我按照您@Nick 建议的方式进行了这些配置
【解决方案2】:

您可以在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 更通用

【讨论】:

    【解决方案3】:

    好吧,我们真的没有太多事情要做。但是,如果我要进行猜测,我会说您似乎正在尝试调用您尚未创建的控制器 AdsController。 在您的 app/Http/Controllers 目录中确认您有一个名为 AdsController.php 的文件

    ---编辑---

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

    【讨论】:

    • 我知道我的控制器没有创建,我强迫它产生内部服务器错误。问题是页面而不是显示通用消息显示异常消息
    【解决方案4】:

    问题解决了!

    我重新安装了 composer,异常消息消失了。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-06
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2011-07-12
      相关资源
      最近更新 更多