Files
panel/app/Exceptions/DisplayException.php

100 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2016-12-07 22:46:38 +00:00
2024-03-12 22:39:16 -04:00
namespace App\Exceptions;
use Exception;
2024-05-11 21:48:36 -04:00
use Filament\Notifications\Notification;
2025-09-24 13:34:19 +02:00
use Illuminate\Container\Container;
2024-10-19 21:00:11 -04:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
2025-09-24 13:34:19 +02:00
use Psr\Log\LoggerInterface;
2022-05-22 18:21:38 -04:00
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
2025-09-24 13:34:19 +02:00
use Throwable;
/**
* @deprecated
*/
2024-03-12 22:39:16 -04:00
class DisplayException extends PanelException implements HttpExceptionInterface
{
2021-01-23 12:33:34 -08:00
public const LEVEL_DEBUG = 'debug';
2024-10-19 18:29:44 -04:00
2021-01-23 12:33:34 -08:00
public const LEVEL_INFO = 'info';
2024-10-19 18:29:44 -04:00
2021-01-23 12:33:34 -08:00
public const LEVEL_WARNING = 'warning';
2024-10-19 18:29:44 -04:00
2021-01-23 12:33:34 -08:00
public const LEVEL_ERROR = 'error';
2017-10-06 00:16:22 -05:00
2017-09-26 22:54:34 -05:00
/**
* DisplayException constructor.
2017-09-26 22:54:34 -05:00
*/
public function __construct(string $message, ?Throwable $previous = null, protected string $level = self::LEVEL_ERROR, int $code = 0)
{
parent::__construct($message, $code, $previous);
}
2017-09-26 22:54:34 -05:00
public function getErrorLevel(): string
2017-09-26 22:54:34 -05:00
{
return $this->level;
}
public function getStatusCode(): int
{
return Response::HTTP_BAD_REQUEST;
}
/**
* @return array<string, string>
*/
public function getHeaders(): array
2022-05-22 18:21:38 -04:00
{
return [];
}
/**
* Render the exception to the user by adding a flashed message to the session
* and then redirecting them back to the page that they came from. If the
* request originated from an API hit, return the error in JSONAPI spec format.
*/
2024-10-19 21:00:11 -04:00
public function render(Request $request): bool|RedirectResponse|JsonResponse
{
2024-05-31 01:20:25 -04:00
if ($request->is('livewire/update')) {
2024-05-11 21:48:36 -04:00
Notification::make()
->title(static::class)
->body($this->getMessage())
->danger()
->send();
2024-10-19 21:00:11 -04:00
return false;
2024-05-11 21:48:36 -04:00
}
2022-05-22 18:21:38 -04:00
if ($request->expectsJson()) {
return response()->json(Handler::toArray($this), $this->getStatusCode(), $this->getHeaders());
}
return redirect()->back()->withInput();
}
/**
* Log the exception to the logs using the defined error level only if the previous
* exception is set.
*
* @throws Throwable
*/
2024-10-19 21:00:11 -04:00
public function report(): void
{
if (!$this->getPrevious() instanceof Exception || !Handler::isReportable($this->getPrevious())) {
2024-10-19 21:00:11 -04:00
return;
}
try {
$logger = Container::getInstance()->make(LoggerInterface::class);
} catch (Exception) {
throw $this->getPrevious();
}
2024-10-19 21:00:11 -04:00
$logger->{$this->getErrorLevel()}($this->getPrevious());
}
}