Skip to content

Latest commit

 

History

History
110 lines (81 loc) · 3.81 KB

error-handling.md

File metadata and controls

110 lines (81 loc) · 3.81 KB

Error Handling Tips (cd ..)

Laravel Tip 💡: Mapping Exceptions via Handler (⬆️)

Note

Since Laravel 11, the directory structure has changed, and exception handling is now managed in app.php. This tip applies to Laravel versions earlier than 11.

Did you know that you can map exceptions to other exceptions? This is helpful when you have custom exceptions that you'd like to be handled like internal ones, or when you want to wrap third-party exceptions and map them to your own 🚀

<?php

namespace App\Exceptions;

use App\Exceptions\MyCustomException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    // MyCustomException is now handled as if it were ModelNotFoundException
    protected $exceptionMap = [
        MyCustomException::class => ModelNotFoundException::class,
    ];
}

Laravel Tip 💡: Mapping Exception via Custom Exception Classes (⬆️)

Note

Since Laravel 11, the directory structure has changed, and exception handling is now managed in app.php. This tip applies to Laravel versions earlier than 11.

When defining a custom exception, there is a secret way to map the exception to another one, and this is done by defining the getInnerException method 🚀

Curious about how the mapping is done? Take a look at Illuminate\Foundation\Exceptions\Handler::mapException()

<?php

namespace App\Exceptions;

use Exception;
use Throwable;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class MyCustomException extends Exception
{
    // MyCustomException is now handled as if it were ModelNotFoundException
    public function getInnerException(): Throwable
    {
        return new ModelNotFoundException;
    }
}

Laravel Tip 💡: Report Internal Exceptions (⬆️)

Note

Since Laravel 11, the directory structure has changed, and exception handling is now managed in app.php. This tip applies to Laravel versions earlier than 11.

Sometimes, you just wish to report internal exceptions. For instance, if you want to know exactly where that 404 error is being thrown, you can do so by overriding the internalDontReport array 🚀

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

// The ModelNotFoundException exception will now be reported
class Handler extends ExceptionHandler
{
    protected $internalDontReport = [
        AuthenticationException::class,
        AuthorizationException::class,
        BackedEnumCaseNotFoundException::class,
        HttpException::class,
        HttpResponseException::class,
        //ModelNotFoundException::class,
        MultipleRecordsFoundException::class,
        RecordsNotFoundException::class,
        SuspiciousOperationException::class,
        TokenMismatchException::class,
        ValidationException::class,
    ];
}

Laravel Tip 💡: Skip Exception Reporting (⬆️)

There are some exceptions that you may not want to report to your monitoring tool. While you could manually register them in "app.php", you can just mark the exception with the "ShouldntReport" interface 🚀

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Contracts\Debug\ShouldntReport;

class PodcastProcessingException extends Exception implements ShouldntReport
{
    //
}