Files
BookStack/app/Exceptions/NotifyException.php
T
Dan Brown 2183fc7fc8 API: Limited exception details shown
Updated exception handler to reduce the amount of detail shown to
prevent potentially sensitive details (like internal paths) being shown
in the error message.

Added an interface for specifically marking exceptions whos messages we
may want to show.

Thanks to Tanner Marks for reporting.
2026-07-28 13:27:42 +01:00

70 lines
1.8 KiB
PHP

<?php
namespace BookStack\Exceptions;
use Exception;
use Illuminate\Contracts\Support\Responsable;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
/**
* An exception that is thrown to notify the user of something which went wrong.
* Typically, these should be translated messages since they will be shown to the end user
* via a pop-up notification error message in the UI.
*
* This exception is not intended to be used for internal system/application errors,
* and therefore will not be logged by the exception handler.
*/
class NotifyException extends Exception implements Responsable, HttpExceptionInterface, ShowsApiExceptionMessage
{
public function __construct(
string $message,
public string $redirectLocation = '/',
protected int $status = 500
) {
$this->message = $message;
parent::__construct();
}
/**
* Get the desired HTTP status code for this exception.
*/
public function getStatusCode(): int
{
return $this->status;
}
/**
* Get the desired HTTP headers for this exception.
*/
public function getHeaders(): array
{
return [];
}
/**
* Send the response for this type of exception.
*
* {@inheritdoc}
*/
public function toResponse($request)
{
$message = $this->getMessage();
// Front-end JSON handling. API-side handling managed via handler.
if ($request->wantsJson()) {
return response()->json(['error' => $message], $this->getStatusCode());
}
if (!empty($message)) {
session()->flash('error', $message);
}
return redirect($this->redirectLocation);
}
public function getMessageForApi(): string
{
return $this->getMessage();
}
}