F
Filament14mo ago
Yurii

Filament exception handling

What is the best practice for exception handling? Example: I have "Detach" action and somewhere in model observer I am checking that the action is not allowed for this particular record and want to throw an exception. I understand that can disable the action button for this record based on the same condition however I cannot relay only on frontend allowed actions but should ensure that the record is never detached. How can I make it sent as Failure Notification? I used to work with Nova and it sends error notification for all exceptions thrown in models. Do I miss something?
4 Replies
Patrick Boivin
Patrick Boivin14mo ago
Not sure if this is exactly what you are looking for but here's a pattern I'm using extensively with custom Actions:
try {
// something

Notification::make()
->title('Success message')
->success()
->send();
} catch (Throwable $e) {
// Report error internally
report($e);

// Show error message to user
Notification::make()
->title('Error message')
->danger()
->send();
}
try {
// something

Notification::make()
->title('Success message')
->success()
->send();
} catch (Throwable $e) {
// Report error internally
report($e);

// Show error message to user
Notification::make()
->title('Error message')
->danger()
->send();
}
Yurii
YuriiOP14mo ago
What about non-custom action? Currently, I am using Detach action on RelationManager. However, on model level I am restricting the ability to detach for some cases. If I throw an exception from the model and it just displays the expection page. My expectation was that it would show the exception message as a failure notification but I cannot find how to configure it in the way.
@uwascan
@uwascan14mo ago
Take a look at your Relation manager's base class and find out the method that handles saving the relationship. override this method in your sub class and handle the error there using Patrick's idea above. Let the documentation guide you.
Yurii
YuriiOP14mo ago
Thank you, will try!
Want results from more Discord servers?
Add your server