Laravel 9如果数据库连接失败,则抛出自定义错误视图?

huangapple go评论98阅读模式
英文:

Laravel 9 throw custom error view if DB connection fails?

问题

我想要做的是,如果与数据库的连接失败,而不是出现“无法建立连接,因为目标机器主动拒绝了连接”的错误,我想要抛出一个自定义视图,显示一个简单的h1标签,其中包含连接失败的文本。我该如何做?

英文:

what i am trying to do is if the connection to the database fails, instead of getting No connection could be made because the target machine actively refused it, i want to throw a custom view that displays a simple h1 with the text that the connection fails. How can i do that?

答案1

得分: 1

你可以通过修改你的 app/Exceptions/Handler.php 文件来捕获特定错误:

  1. public function render($request, Throwable $exception) {
  2. if ($exception instanceof QueryException) {
  3. return response()->view('errorpage');
  4. }
  5. return parent::render($request, $exception);
  6. }

当然,你可以将 QueryException 更改为任何你想要处理的异常,比如:Illuminate\Database\Eloquent\ModelNotFoundException 或其他异常类型。

英文:

You can do this by modifying your app/Exceptions/Handler.php to catch that specific error:

  1. public function render($request, Throwable $exception) {
  2. if ($exception instanceof QueryException) {
  3. return response()->view('errorpage');
  4. }
  5. return parent::render($request, $exception);
  6. }

Of course you can change the QueryException to any exception that you want to handle, such as: Illuminate\Database\Eloquent\ModelNotFoundException or any other.

答案2

得分: 1

app/Exceptions/Handler.php

  1. use PDOException;
  2. use App\Exceptions\Handler;
  3. class ExceptionHandler extends Handler
  4. {
  5. public function render($request, Exception $exception)
  6. {
  7. if ($exception instanceof PDOException) {
  8. return response()->view('errors.database', [], 500);
  9. }
  10. return parent::render($request, $exception);
  11. }
  12. }

resources/views/errors 中,errors.database,你可以创建自定义样式。

英文:

In app/Exceptions/Handler.php

  1. use PDOException;
  2. use App\Exceptions\Handler;
  3. class ExceptionHandler extends Handler
  4. {
  5. public function render($request, Exception $exception)
  6. {
  7. if ($exception instanceof PDOException) {
  8. return response()->view('errors.database', [], 500);
  9. }
  10. return parent::render($request, $exception);
  11. }
  12. }

In resources/views/errors, errors.database, you can create custom style

答案3

得分: 0

以下是翻译好的部分:

  1. 答案已经在顶部给出,但我通过一些修改使其正常工作。
  2. use Throwable;
  3. use PDOException;
  4. public function render($request, Throwable $exception)
  5. {
  6. if ($exception instanceof PDOException) {
  7. return response()->view('errors.database', [], 500);
  8. }
  9. return parent::render($request, $exception);
  10. }
英文:

The answer was given at the top, but i made it work with some modifications.

  1. use Throwable;
  2. use PDOException;
  3. public function render($request, Throwable $exception)
  4. {
  5. if ($exception instanceof PDOException) {
  6. return response()->view('errors.database', [], 500);
  7. }
  8. return parent::render($request, $exception);
  9. }

huangapple
  • 本文由 发表于 2023年1月8日 23:41:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定