一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Laravel中错误与异常处理的用法示例

时间:2018-09-19 编辑:猪哥 来源:一聚教程网

在本文中,我们将探讨 Laravel Web 框架中最重要和最少讨论的功能之一 - 异常处理。 Laravel 带有一个内置的异常处理程序,可以让您轻松地以友好的方式报告和呈现异常。

Laravel 自带错误和异常处理,AppExceptionsHandler 负责上报异常和如何返回内容,以及未登录的处理。

AppExceptionsHandler 位于 appExceptionsHandler.php,下面介绍这个类的属性和用法。好了,话不多说了,来一起看看详细的介绍吧

忽略异常

在 $dontReport 中可以定义忽略的异常类名:

protected $dontReport = [
 IlluminateAuthAuthenticationException::class,
 IlluminateAuthAccessAuthorizationException::class,
 SymfonyComponentHttpKernelExceptionHttpException::class,
 IlluminateDatabaseEloquentModelNotFoundException::class,
 IlluminateValidationValidationException::class,
];

这些异常就不会经过 report 方法。

几个重要方法

主要介绍这三个方法,report,render 和 unauthenticated 的用法。

report方法

report 方法可以用来记录日志,可以根据不同的异常类型(包括自定义异常类型),如 ClientException,ConnectException 定制不同的日志级别和日志内容。

if ($exception instanceof ABCException) {
 Log::emergency('ABC异常', $context);
} else if ($exception instanceof HeheException) {
 Log::info('Hehe异常', $context);
}

report 方法没有返回值,也不应该在这里中断程序。

render方法

render 方法可以根据不同的异常类型,返回不同的数据。如:

if (get_class($exception) == 'Exception' || $exception instanceof NotAllowedException) {
 return response()->json(['message' => $exception->getMessage()], 400);
} elseif ( $exception instanceof ValidationException) {
 return response()->json(['message' => '校验失败', 'errors'=> $exception->validator->errors()], 400);
}

unauthenticated

在访问需要登录态的页面时,用户未登录就会进入这个方法进行处理,举个例子说明:

protected function unauthenticated($request, AuthenticationException $exception)
{
 if ($request->expectsJson()) {
  return response()->json(['error' => 'Unauthenticated.'], 401);
 }
 
 //如果是后台页面未认证,跳转到后台登陆页面
 $guard = $exception->guards();
 if (in_array('admin', $guard)) {
  return redirect()->guest('/admin/login');
 }
 
 return redirect()->guest('login');
}

如果是返回 json,则统一返回格式。

默认情况下返回前台的登录页,如果是访问后台页面未登录,则跳转到后台登录页。

官方文档

Laravel 5.6

https://laravel-china.org/docs/laravel/5.6/errors/1373

热门栏目