Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

How to catch 403 error and show a flashy message ?

Hello,

I know how to show a flashy message. But I don't know how to catch a 403 error.

I have seen various solutions on the web, I have tested them, but no one is working.

I use a policy in a controller method :

$this->authorize('roadmap', $game);

I just want to catch the error and then send a flashy message on the screen.

Thank you for your answer.

Vincent

0 likes
9 replies
Sinnbeck's avatar

How are you submitting the data? A form or ajax?

vincent15000's avatar

I use a simple link in the webpage.

@if ($game->isOpen())
    <a href="{{ route('player.roadmap', ['game' => $game ]) }}">
        <p class="game title shadow rounded">
            Jouer
        </p>
    </a>
@endif
Snapey's avatar

Better to not allow the the user to access the function in the first place.

Don't show them the button if they are not allowed to click it.

If they try to access the function by when not allowed show an ugly FO message.

jlrdw's avatar

And also here is one example I use to throw a 403 when using fetch js:

if (!ChkAuth::chklog('user')) {  // ignore this line I have custom auth
      return response()->json(['message' => 'not auth'], 403);
}

then in fetch:

                    if (response.status === 403 || response.status === 500) {
                        response.json().then(function (data) {
                            var div = document.getElementById('msg');
                            div.innerHTML += "Not Authorized";
                           //  do your flashy thing here
                        })
                    }

Again just example.

I still have 500, but that's in work.

vincent15000's avatar

@jlrdw I already have tried it ... it doesn't work ... some documents on the web explain that 403 error is not possible with HTTP Exceptions ... I don't understand why.

@snapey I don't show the button ... that's ok ... but I also want to prevent from entering the URL to access the route. What do you mean by saying "ugly FO message" ? I do not find the translation for FO on the web ... ;).

vincent15000's avatar

Initially I wanted to use a similar code as this one in the Handler.php exception file.

public function render($request, Throwable $exception)
{
    if ($exception instanceof NotFoundHttpException)
    {
        return redirect(RouteServiceProvider::HOME);
    }
    return parent::render($request, $exception);
}

@snapey I think the best way is perhaps to prevent the user from accessing to the function. Perhaps with a gate ?

jlrdw's avatar

@vincent15000 Try a 404.

But I also had some trouble, thus is why I have the or 500, it worked.

But being new to fetch js, I am still experimenting. You could also just redirect to a certain page with a friendly error message, many ways to handle this. Even have the page you redirect to show the message "flashy".

vincent15000's avatar
vincent15000
OP
Best Answer
Level 63

I just tried to code a gate and I use it in the controller method. I do not have found another solution. ;)

Please or to participate in this conversation.