laravel中的异常:级别、处理、Log记录

本教程操作环境:windows7系统、Laravel9版,DELL G3电脑。

laravel中的异常级别

常量 说明
E_ERROR 致命的运行时错误。这类错误一般是不可恢复的情况,例如内存分配导致的问题。后果是导致脚本终止不再继续运行。
E_WARNING 运行时警告 (非致命错误)。仅给出提示信息,但是脚本不会终止运行。
E_PARSE 编译时语法解析错误。解析错误仅仅由分析器产生。
E_NOTICE 运行时通知。表示脚本遇到可能会表现为错误的情况,但是在可以正常运行的脚本里面也可能会有类似的通知。
E_CORE_ERROR 在 PHP 初始化启动过程中发生的致命错误。该错误类似 E_ERROR,但是是由 PHP 引擎核心产生的。
E_CORE_WARNING PHP 初始化启动过程中发生的警告 (非致命错误) 。类似 E_WARNING,但是是由 PHP 引擎核心产生的。
E_COMPILE_ERROR 致命编译时错误。类似 E_ERROR, 但是是由 Zend 脚本引擎产生的。
E_COMPILE_WARNING 编译时警告 (非致命错误)。类似 E_WARNING,但是是由 Zend 脚本引擎产生的。
E_USER_ERROR 用户产生的错误信息。类似 E_ERROR, 但是是由用户自己在代码中使用 PHP 函数 trigger_error () 来产生的。
E_USER_WARNING 用户产生的警告信息。类似 E_WARNING, 但是是由用户自己在代码中使用 PHP 函数 trigger_error () 来产生的。
E_USER_NOTICE 用户产生的通知信息。类似 E_NOTICE, 但是是由用户自己在代码中使用 PHP 函数 trigger_error () 来产生的。
E_STRICT 启用 PHP 对代码的修改建议,以确保代码具有最佳的互操作性和向前兼容性。
E_RECOVERABLE_ERROR 可被捕捉的致命错误。 它表示发生了一个可能非常危险的错误,但是还没有导致 PHP 引擎处于不稳定的状态。 如果该错误没有被用户自定义句柄捕获 (参见 set_error_handler ()),将成为一个 E_ERROR 从而脚本会终止运行。
E_DEPRECATED 运行时通知。启用后将会对在未来版本中可能无法正常工作的代码给出警告。
E_USER_DEPRECATED 用户产少的警告信息。 类似 E_DEPRECATED, 但是是由用户自己在代码中使用 PHP 函数 trigger_error () 来产生的。
E_ALL 用户产少的警告信息。 类似 E_DEPRECATED, 但是是由用户自己在代码中使用 PHP 函数 trigger_error () 来产生的。

Laravel 异常处理

laravel 的异常处理由类 \Illuminate\Foundation\Bootstrap\HandleExceptions::class 完成:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

class HandleExceptions

{

    public function bootstrap(Application $app)

    {

        $this->app = $app;

        error_reporting(-1);

        set_error_handler([$this, 'handleError']);

        set_exception_handler([$this, 'handleException']);

        register_shutdown_function([$this, 'handleShutdown']);

        if (! $app->environment('testing')) {

            ini_set('display_errors', 'Off');

        }

    }

}

异常转化

laravel 的异常处理均由函数 handleException 负责。

PHP7 实现了一个全局的 throwable 接口,原来的 Exception 和部分 Error 都实现了这个接口, 以接口的方式定义了异常的继承结构。于是,PHP7 中更多的 Error 变为可捕获的 Exception 返回给开发者,如果不进行捕获则为 Error ,如果捕获就变为一个可在程序内处理的 Exception。这些可被捕获的 Error 通常都是不会对程序造成致命伤害的 Error,例如函数不存在。

PHP7 中,基于 /Error exception,派生了 5 个新的 engine exception:ArithmeticError / AssertionError / DivisionByZeroError / ParseError / TypeError。在 PHP7 里,无论是老的 /Exception 还是新的 /Error ,它们都实现了一个共同的 interface: /Throwable。

因此,遇到非 Exception 类型的异常,首先就要将其转化为 FatalThrowableError 类型:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public function handleException($e)

{

    if (! $e instanceof Exception) {

        $e = new FatalThrowableError($e);

    }

    $this->getExceptionHandler()->report($e);

    if ($this->app->runningInConsole()) {

        $this->renderForConsole($e);

    } else {

        $this->renderHttpResponse($e);

    }

}

FatalThrowableError 是 Symfony 继承 \ErrorException 的错误异常类:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

class FatalThrowableError extends FatalErrorException

{

    public function __construct(\Throwable $e)

    {

        if ($e instanceof \ParseError) {

            $message = 'Parse error: '.$e->getMessage();

            $severity = E_PARSE;

        } elseif ($e instanceof \TypeError) {

            $message = 'Type error: '.$e->getMessage();

            $severity = E_RECOVERABLE_ERROR;

        } else {

            $message = $e->getMessage();

            $severity = E_ERROR;

        }

        \ErrorException::__construct(

            $message,

            $e->getCode(),

            $severity,

            $e->getFile(),

            $e->getLine()

        );

        $this->setTrace($e->getTrace());

    }

}

异常 Log

当遇到异常情况的时候,laravel 首要做的事情就是记录 log,这个就是 report 函数的作用。

1

2

3

4

protected function getExceptionHandler()

{

    return $this->app->make(ExceptionHandler::class);

}

laravel 在 Ioc 容器中默认的异常处理类是 Illuminate\Foundation\Exceptions\Handler:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

class Handler implements ExceptionHandlerContract

{

    public function report(Exception $e)

    {

        if ($this->shouldntReport($e)) {

            return;

        }

        try {

            $logger = $this->container->make(LoggerInterface::class);

        } catch (Exception $ex) {

            throw $e; // throw the original exception

        }

        $logger->error($e);

    }

    protected function shouldntReport(Exception $e)

    {

        $dontReport = array_merge($this->dontReport, [HttpResponseException::class]);

        return ! is_null(collect($dontReport)->first(function ($type) use ($e) {

            return $e instanceof $type;

        }));

    }

}

异常页面展示

记录 log 后,就要将异常转化为页面向开发者展示异常的信息,以便查看问题的来源:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

protected function renderHttpResponse(Exception $e)

{

    $this->getExceptionHandler()->render($this->app['request'], $e)->send();

}

class Handler implements ExceptionHandlerContract

{

    public function render($request, Exception $e)

    {

        $e = $this->prepareException($e);

        if ($e instanceof HttpResponseException) {

            return $e->getResponse();

        } elseif ($e instanceof AuthenticationException) {

            return $this->unauthenticated($request, $e);

        } elseif ($e instanceof ValidationException) {

            return $this->convertValidationExceptionToResponse($e, $request);

        }

        return $this->prepareResponse($request, $e);

    }

}

对于不同的异常,laravel 有不同的处理,大致有 HttpException、HttpResponseException、AuthorizationException、ModelNotFoundException、AuthenticationException、ValidationException。由于特定的不同异常带有自身的不同需求,本文不会特别介绍。本文继续介绍最普通的异常 HttpException 的处理:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

protected function prepareResponse($request, Exception $e)

{

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

        return $this->toIlluminateResponse($this->renderHttpException($e), $e);

    } else {

        return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e);

    }

}

protected function renderHttpException(HttpException $e)

{

    $status = $e->getStatusCode();

    view()->replaceNamespace('errors', [

        resource_path('views/errors'),

        __DIR__.'/views',

    ]);

    if (view()->exists("errors::{$status}")) {

        return response()->view("errors::{$status}", ['exception' => $e], $status, $e->getHeaders());

    } else {

        return $this->convertExceptionToResponse($e);

    }

}

对于 HttpException 来说,会根据其错误的状态码,选取不同的错误页面模板,若不存在相关的模板,则会通过 SymfonyResponse 来构造异常展示页面:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

protected function convertExceptionToResponse(Exception $e)

{

    $e = FlattenException::create($e);

    $handler = new SymfonyExceptionHandler(config('app.debug'));

    return SymfonyResponse::create($handler->getHtml($e), $e->getStatusCode(), $e->getHeaders());

}

protected function toIlluminateResponse($response, Exception $e)

{

    if ($response instanceof SymfonyRedirectResponse) {

        $response = new RedirectResponse($response->getTargetUrl(), $response->getStatusCode(), $response->headers->all());

    } else {

        $response = new Response($response->getContent(), $response->getStatusCode(), $response->headers->all());

    }

    return $response->withException($e);

}

THE END