inertiajs / inertia-laravel

The Laravel adapter for Inertia.js.
https://inertiajs.com
MIT License
2.1k stars 235 forks source link

403 doesn't trigger onError event #685

Open kratos-digital opened 1 week ago

kratos-digital commented 1 week ago

I am using form requests and when an action is not authorized I return false from authorize() method in the request.

This renders the dialog with the message 403 Not authorized, but only onFinish event triggers.

The problem is when using dialogs (shadcn ui), even though the 403 dialog renders above the dialog with form, the focus is still on the dialog behind (maening you can't interact with 403 dialog until the one below is closed), and what I wanted to do with onError is to hide that dialog so the focus would be on 403.

But it seems none of the events is firing when there is any error response, other than validation errors (which returns 200 response and not an actual 422 Unprocessable Content error response). It seems onError triggers only when there is an error bag present in 200 response, and I think there is missing something to handle such response with proper events.

wizzymore commented 3 days ago

What i recommend to you is to handle the error in laravel's error handler and to manually set the error into the session so inertia.js will receive it, this way the onError will trigger.

kratos-digital commented 3 days ago

I don't know what you mean. I use form request authorized method (which runs before validation) and which should return true or false, not an error or exception. Handling authorization manually would make the built-in features useless

wizzymore commented 1 day ago

Take a look here at this example: https://github.com/wizzymore/inertia-errors-example