问题
I'm developing a Laravel package, have a service provider with views and everything, but I need to have custom error messages. How can I register custom error handler in my service provider?
回答1:
You can register custom handler by binding it with Laravel's exception handler class on service provider.
Create Custom Handler
First you have to create custom exception handler class.
<?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');
}
}
Register Your Handler
Now register the class on your 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()
{
}
}
For more information check this blog http://blog.sarav.co/registering-custom-exception-handler-laravel-5/
回答2:
Since Laravel 8.0 you can use the built-in renderable and reportable methods on the Handler class that is bound to the ExceptionHandler contract (https://laravel.com/docs/8.x/errors#rendering-exceptions).
So for example:
<?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);
}
});
}
}
来源:https://stackoverflow.com/questions/39698429/how-can-i-register-custom-error-handler-in-laravel-5