78 lines
2.1 KiB
PHP
78 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use App\Support\ApiError;
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Throwable;
|
|
|
|
class Handler extends ExceptionHandler
|
|
{
|
|
/**
|
|
* The list of the inputs that are never flashed to the session on validation exceptions.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $dontFlash = [
|
|
'current_password',
|
|
'password',
|
|
'password_confirmation',
|
|
];
|
|
|
|
/**
|
|
* Register the exception handling callbacks for the application.
|
|
*/
|
|
public function register(): void
|
|
{
|
|
$this->reportable(function (Throwable $e) {
|
|
//
|
|
});
|
|
}
|
|
|
|
public function render($request, Throwable $e)
|
|
{
|
|
if ($request->expectsJson()) {
|
|
if ($e instanceof ValidationException) {
|
|
return ApiError::response(
|
|
'validation_failed',
|
|
'Validation failed',
|
|
'The given data was invalid.',
|
|
422,
|
|
['errors' => $e->errors()],
|
|
);
|
|
}
|
|
|
|
$status = $this->isHttpException($e)
|
|
? $this->toHttpException($e)->getStatusCode()
|
|
: 500;
|
|
|
|
$code = $status >= 500 ? 'server_error' : 'request_failed';
|
|
|
|
return ApiError::response(
|
|
$code,
|
|
$status >= 500 ? 'Unexpected error' : 'Request could not be completed',
|
|
$this->buildGenericMessage($status),
|
|
$status,
|
|
);
|
|
}
|
|
|
|
if ($request->inertia()) {
|
|
if ($e instanceof ValidationException) {
|
|
return back()->withErrors($e->errors())->withInput($request->all());
|
|
}
|
|
}
|
|
|
|
return parent::render($request, $e);
|
|
}
|
|
|
|
private function buildGenericMessage(int $status): string
|
|
{
|
|
if ($status >= 500) {
|
|
return 'Something went wrong on our side. Please try again later.';
|
|
}
|
|
|
|
return 'Your request could not be processed. Please verify the details and try again.';
|
|
}
|
|
}
|