This is a community collaborative translation of the article, has been translated, more information please click
Introduction to collaborative translation 。


Exception handling

In the first half of this article, we’ll explore the default Settings provided by exception handlers. In fact, we’ll start by looking at the default Handler class to understand how Laravel handles exceptions.

In the second half of the article, we will continue to show you how to create custom exception handlers so that you can catch custom exceptions.


0
retranslation

Seaony

The basic configuration

Before we dive into exception-handling classes, let’s take a look at a few important exception-related parameter configurations.

config/app.php

. . / * | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | debug mode of application | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | | when the reference is in debug mode, detailed stack information will display an error production | if disable, Only display a simple error page | * / 'debug' = > env (' APP_DEBUG, false),... .Copy the code

TRUE
.env
APP_DEBUG
TRUE
config/app.php

. . 'log' => env('APP_LOG', 'single'), 'log_level' => env('APP_LOG_LEVEL', 'debug'), ... .Copy the code

In Laravel framework, Monolog database was used to record logs. You can configure the Monolog libraries through the configuration items above.

The default path for saving log files is storage/logs/laravel.log, which you generally do not need to change. In the meantime, you can specify the level of errors to be logged to the log file via APP_LOG_LEVEL.


0
retranslation

Summer

This section describes the basic configuration of exceptions and logs.

app/Exceptions/Handler.php

<? php namespace App\Exceptions; use Exception; use Illuminate\Auth\AuthenticationException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; Class Handler extends ExceptionHandler {/** * defines exception types that do not need to 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 ** You can report exceptions to Sentry in this method, * * @param \Exception $Exception * @return void */ public function report(Exception $Exception) { parent::report($exception); } /** * return the Exception via Http ** @param \Illuminate\Http\Request $Request * @param \Exception $Exception * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { return parent::render($request, $exception); } /** * Convert authentication-related exceptions to unauthenticated responses (401) ** @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(route('login')); }}Copy the code

The above processing class has two main functions: reporting and displaying all exception information.


0
retranslation

Summer
report

/** * Reports or logs an exception. * * This is a good time to send exceptions to agencies like Sentry and Bugsnag. * * @param \Exception $exception * @return void */ public function report(Exception $exception) { parent::report($exception); }Copy the code

dontReport
render

/** * Renders the exception to the HTTP response value. * * @param \Illuminate\Http\Request $request * @param \Exception $exception * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { return parent::render($request, $exception); }Copy the code

report
render

The Render method also allows you to customize response values for different types of errors, which we’ll learn about in the next chapter.

Finally, the unauthenticated method handles the AuthenticationException exception, where you can determine what is displayed when a user accesses an unauthorized page.


0
retranslation

rayle

Custom exception classes

CustomException
app/Exceptions/CustomException.php

<? php namespace App\Exceptions; use Exception; Class CustomException extends Exception {/** * Reports this Exception. ** @return void */ public function report() {} /** * Render the exception to the HTTP response value. * * @param \Illuminate\Http\Request * @return \Illuminate\Http\Response */ public function render($request) { return response()->view( 'errors.custom', array( 'exception' => $this ) ); }}Copy the code

CustomException
Exception

0
retranslation

rayle
errors.custom
resources/views/errors/custom.blade.php

Exception details: <b>{{ $exception->getMessage() }}</b>Copy the code

This is a fairly simple view file that displays only one line of error messages, but you can design the view however you want.

app/Exceptions/Handler.php
app/Exceptions/Handler.php

. . /** * Renders the exception to the HTTP response value. * * @param \Illuminate\Http\Request $request * @param \Exception $exception * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { if ($exception instanceof \App\Exceptions\CustomException) { return $exception->render($request); } return parent::render($request, $exception); }... .Copy the code

As you can see, we first check the type of exception in the Render method. If the exception is of the class \App\Exceptions\CustomException, we call the Render method of the class.


0
retranslation

rayle
app/Http/Controllers/ExceptionController.php

<? php namespace App\Http\Controllers; use App\Http\Controllers\Controller; Class ExceptionController extends Controller {public function index() { CustomException Throw new \App\Exceptions\CustomException('Something Went Wrong.'); }}Copy the code

routes/web.php

// Exception routes
Route::get('exception/index', 'ExceptionController@index');Copy the code

Your-laravel-site.com/exception/i…
errors.custom

This way, you can handle exceptions in Laravel in your own way. Finished! Have fun coding with Laravel!


0
retranslation

Summer

conclusion

Today, we took a closer look at the exception handling features in Laravel. At the beginning of the article, we searched for some of the basic configurations provided by Laravel for displaying and reporting exceptions. Next, we took a brief look at the default exception handling classes.

In the second half of the article, we demonstrate how to handle custom exceptions in your application by creating a custom exception handling class.

 Envato Market

Look forward to hearing any kind of consultation or advice!


0
retranslation

Summer

All translations in this article are for study and communication purposes only. Please note the translator, source, and link to this article


Our translation work is in accordance with
CCIf our work has violated your rights and interests, please contact us immediately.