我正在开发一个Laravel软件包,拥有一个包含视图和所有内容的服务提供程序,但我需要有自定义错误消息。如何在服务提供商中注册自定义错误处理程序?
答案 0 :(得分:2)
您可以通过将自定义处理程序与服务提供程序上的Laravel异常处理程序类绑定来注册它。
首先,您必须创建自定义异常处理程序类。
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class CustomHandler 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,
];
/**
* 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)
{
if ($exception instanceof NotFoundHttpException) {
return response()->view('errors.404', [], 404);
}
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');
}
}
现在在AppServiceProvider
<?php
namespace App\Providers;
use App\Exceptions\CustomHandler;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
/**
* Do not forget to import them before using!
*/
$this->app->bind(
ExceptionHandler::class,
CustomHandler::class
);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
}
有关详细信息,请查看此博客http://blog.sarav.co/registering-custom-exception-handler-laravel-5/
答案 1 :(得分:0)
从 Laravel 8.0 开始,您可以在绑定到 ExceptionHandler 契约 (https://laravel.com/docs/8.x/errors#rendering-exceptions) 的 Handler 类上使用内置的可渲染和可报告方法。
例如:
<?php
namespace App\Providers;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
/** @var ExceptionHandler $exceptionHandler */
$exceptionHandler = resolve(ExceptionHandler::class);
$exceptionHandler->renderable(function (NotFoundHttpException $e, $request) {
if ($request->wantsJson()) {
return response()->json([
'success' => false,
'message' => 'Endpoint not found'
], 404);
}
});
}
}