master

laravel/framework

Last updated at: 29/12/2023 09:23

Exceptions.php

TLDR

The provided file, Exceptions.php, is part of the Illuminate\Foundation\Configuration namespace in the Demo Projects project. It contains a class named Exceptions which is responsible for handling exceptions and configuring the exception handling behavior in the project.

Methods

__construct

The method __construct is the constructor of the Exceptions class. It takes a Handler object as a parameter and assigns it to the handler property.

reportable

The method reportable registers a callback to handle reportable exceptions. It takes a callable as a parameter and returns an instance of Illuminate\Foundation\Exceptions\ReportableHandler.

renderable

The method renderable registers a callback to handle renderable exceptions. It takes a callable as a parameter and returns the current instance of the Exceptions class.

throttle

The method throttle specifies the callback to be used to throttle reportable exceptions. It takes a callable as a parameter and returns the current instance of the Exceptions class.

map

The method map registers a new exception mapping. It takes two parameters: $from (a closure or string) and $to (a closure, string, or null). It returns the current instance of the Exceptions class. If the given parameters are not valid, an InvalidArgumentException is thrown.

level

The method level sets the log level for a given exception type. It takes two parameters: $type (a class string representing a throwable) and $level (a Psr\Log\LogLevel). It returns the current instance of the Exceptions class.

context

The method context registers a closure to build exception context data. It takes a closure as a parameter and returns the current instance of the Exceptions class.

dontReport

The method dontReport indicates that the given exception type should not be reported. It takes a string parameter $class representing the class name and returns the current instance of the Exceptions class.

dontReportDuplicates

The method dontReportDuplicates configures the exception handler to not report duplicate exceptions. It returns the current instance of the Exceptions class.

dontFlash

The method dontFlash indicates that the specified attributes should not be flashed to the session on validation errors. It takes an array or string parameter $attributes and returns the current instance of the Exceptions class.

Classes

None

<?php

namespace Illuminate\Foundation\Configuration;

use Closure;
use Illuminate\Foundation\Exceptions\Handler;

class Exceptions
{
    /**
     * Create a new exception handling configuration instance.
     *
     * @param  \Illuminate\Foundation\Exceptions\Handler  $handler
     * @return void
     */
    public function __construct(public Handler $handler)
    {
    }

    /**
     * Register a reportable callback.
     *
     * @param  callable  $reportUsing
     * @return \Illuminate\Foundation\Exceptions\ReportableHandler
     */
    public function reportable(callable $reportUsing)
    {
        return $this->handler->reportable($reportUsing);
    }

    /**
     * Register a renderable callback.
     *
     * @param  callable  $renderUsing
     * @return $this
     */
    public function renderable(callable $renderUsing)
    {
        $this->handler->renderable($renderUsing);

        return $this;
    }

    /**
     * Specify the callback that should be used to throttle reportable exceptions.
     *
     * @param  callable  $throttleUsing
     * @return $this
     */
    public function throttle(callable $throttleUsing)
    {
        $this->handler->throttleUsing($throttleUsing);

        return $this;
    }

    /**
     * Register a new exception mapping.
     *
     * @param  \Closure|string  $from
     * @param  \Closure|string|null  $to
     * @return $this
     *
     * @throws \InvalidArgumentException
     */
    public function map($from, $to = null)
    {
        $this->handler->map($from, $to);

        return $this;
    }

    /**
     * Set the log level for the given exception type.
     *
     * @param  class-string<\Throwable>  $type
     * @param  \Psr\Log\LogLevel::*  $level
     * @return $this
     */
    public function level($type, $level)
    {
        $this->handler->level($type, $level);

        return $this;
    }

    /**
     * Register a closure that should be used to build exception context data.
     *
     * @param  \Closure  $contextCallback
     * @return $this
     */
    public function context(Closure $contextCallback)
    {
        $this->handler->buildContextUsing($contextCallback);

        return $this;
    }

    /**
     * Indicate that the given exception type should not be reported.
     *
     * @param  string  $class
     * @return $this
     */
    public function dontReport(string $class)
    {
        $this->handler->dontReport($class);

        return $this;
    }

    /**
     * Do not report duplicate exceptions.
     *
     * @return $this
     */
    public function dontReportDuplicates()
    {
        $this->handler->dontReportDuplicates();

        return $this;
    }

    /**
     * Indicate that the given attributes should never be flashed to the session on validation errors.
     *
     * @param  array|string  $attributes
     * @return $this
     */
    public function dontFlash($attributes)
    {
        $this->handler->dontFlash($attributes);

        return $this;
    }
}