Level 53
You can do this in App\Exception\Handler.php
public function handleException($request, Exception $exception)
{
if ($exception instanceof AuthorizationException) {
return $this->errorResponse($exception->getMessage(), 403);
}
}
You can customize most of the exceptions in this method. Then, in something like an api controller:
class ApiController extends Controller
{
use ApiResponser;
/**
* ApiController constructor.
*/
public function __construct()
{
$this->middleware('auth:api');
}
/**
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
protected function allowedAdminAction()
{
if (Gate::denies('admin-action')) {
throw new AuthorizationException('This action is unauthorized');
}
}
}