use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
-use Symfony\Component\HttpKernel\Exception\HttpException;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Throwable;
class Handler extends ExceptionHandler
*/
protected function renderApiException(Throwable $e): JsonResponse
{
- $code = $e->getCode() === 0 ? 500 : $e->getCode();
+ $code = 500;
$headers = [];
- if ($e instanceof HttpException) {
+ if ($e instanceof HttpExceptionInterface) {
$code = $e->getStatusCode();
$headers = $e->getHeaders();
}
use Exception;
use Illuminate\Contracts\Support\Responsable;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
-class PrettyException extends Exception implements Responsable
+class PrettyException extends Exception implements Responsable, HttpExceptionInterface
{
/**
* @var ?string
*/
protected $details = null;
+ /**
+ * @var array
+ */
+ protected $headers = [];
+
/**
* Render a response for when this exception occurs.
*
*/
public function toResponse($request)
{
- $code = ($this->getCode() === 0) ? 500 : $this->getCode();
+ $code = $this->getStatusCode();
return response()->view('errors.' . $code, [
'message' => $this->getMessage(),
return $this;
}
+
+ /**
+ * Get the desired HTTP status code for this exception.
+ */
+ public function getStatusCode(): int
+ {
+ return ($this->getCode() === 0) ? 500 : $this->getCode();
+ }
+
+ /**
+ * Get the desired HTTP headers for this exception.
+ * @return array<mixed>
+ */
+ public function getHeaders(): array
+ {
+ return $this->headers;
+ }
+
+ /**
+ * Set the desired HTTP headers for this exception.
+ * @param array<mixed> $headers
+ */
+ public function setHeaders(array $headers): void
+ {
+ $this->headers = $headers;
+ }
}