pixelfed/app/Exceptions/Handler.php

88 lines
2.0 KiB
PHP
Raw Normal View History

2018-04-15 23:56:48 +00:00
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
2020-05-29 07:01:32 +00:00
use Throwable;
2021-05-28 05:06:47 +00:00
use League\OAuth2\Server\Exception\OAuthServerException;
2018-04-15 23:56:48 +00:00
class Handler extends ExceptionHandler
{
2021-05-28 05:06:47 +00:00
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
OAuthServerException::class,
2021-12-31 08:12:33 +00:00
\Zttp\ConnectionException::class,
\GuzzleHttp\Exception\ConnectException::class,
\Illuminate\Http\Client\ConnectionException::class
2021-05-28 05:06:47 +00:00
];
2018-04-15 23:56:48 +00:00
2021-05-28 05:06:47 +00:00
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
2018-04-15 23:56:48 +00:00
2021-05-28 05:06:47 +00:00
/**
* Report or log an exception.
*
* @param \Exception $exception
*
* @return void
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
2018-04-15 23:56:48 +00:00
2021-12-31 07:58:16 +00:00
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (\BadMethodCallException $e) {
return app()->environment() !== 'production';
});
2021-12-31 08:12:33 +00:00
$this->reportable(function (\Illuminate\Http\Client\ConnectionException $e) {
return app()->environment() !== 'production';
});
2021-12-31 07:58:16 +00:00
}
2021-05-28 05:06:47 +00:00
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
*
* @return \Illuminate\Http\Response
*/
public function render($request, Throwable $exception)
{
if ($exception instanceof \Illuminate\Validation\ValidationException && $request->wantsJson()) {
return response()->json(
[
'message' => $exception->getMessage(),
'errors' => $exception->validator->getMessageBag()
],
method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500
);
} else if ($request->wantsJson()) {
2021-05-28 05:06:47 +00:00
return response()->json(
['error' => $exception->getMessage()],
method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500
);
}
2021-05-28 05:06:47 +00:00
return parent::render($request, $exception);
}
2018-04-15 23:56:48 +00:00
}