Advice asked for how to handle import errors

Hey, When my client imports data sometimes it can be that for example the user has an csv with none unique header columns. When I do this locally I see an error:
League\ Csv \ SyntaxError
The header record contains duplicate column names.
League\ Csv \ SyntaxError
The header record contains duplicate column names.
My question is how can I handle these errors so that the user can see he uploaded a wrong csv or so? Maybe there is some pre upload hook or pre action hook where I can Do this? Any suggestions? Thanks
15 Replies
JJSanders
JJSanders4mo ago
Actuallly this happend is https://github.com/filamentphp/filament/blob/4d2528c67ead35c30df839b6e0c896722ef8a90f/packages/actions/src/Concerns/CanImportRecords.php#L146 The getHeader function throws the exception. It's unclear to me how to handle the exception
GitHub
filament/packages/actions/src/Concerns/CanImportRecords.php at 4d25...
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS. - filamentphp/filament
JJSanders
JJSanders4mo ago
Does anyone know how I can solve this? Or how I can pass the error to the user?
toeknee
toeknee4mo ago
They must be unique to allow them to import in the current method. Otherwise it's very confusing to know which column is for what. Just ensure they are unique.
JJSanders
JJSanders4mo ago
I know. But when you upload a csv with non unique columns you get a 500 error. I would like to know how I can catch that error and send a notification to the end user with an error message.
toeknee
toeknee4mo ago
Ahh I got you, that makes more sense. I am unsure at present as I haven't used that functionality much but would asume that should be implemented to the core as you shouldn't trigger a 500 error for duplicate column names and instead it should be a validation error
JJSanders
JJSanders4mo ago
Gonna ask this once again. How to implement this error handling in Filament when something is wrong with the file to import?
toeknee
toeknee4mo ago
I would have a look at : https://filamentphp.com/docs/3.x/actions/prebuilt-actions/import#validating-csv-data and build a custom validation rule for same column checking.
JJSanders
JJSanders4mo ago
That works for a single column not for the whole file.
awcodes
awcodes4mo ago
Is it a generic 500 or is it a named Exception.?
JJSanders
JJSanders4mo ago
It is a named exception
wyChoong
wyChoong4mo ago
sounds like error handling that need to be added into core
JJSanders
JJSanders3mo ago
Hey everyoe I am still looking for a solution to this. I am prepared to create a PR to filament for this but I could use a bit of help to work out how to get this done. It's about when you uploade a csv with duplicate headings. It throws an exception. But how can I deal with this exception? I could use some leads for this. THanks
awcodes
awcodes3mo ago
Since you have the name of the exception class you can handle it in laravels exception handler and do what you need with it. https://laravel.com/docs/10.x/errors#the-exception-handler
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
JJSanders
JJSanders3mo ago
Ultimately I want to show the error to the user in the import modal
JJSanders
JJSanders3mo ago
For anyone interested. I managed to create a PR for this. Suggestions are welcome: https://github.com/filamentphp/filament/pull/11893
GitHub
Fix duplicate headers error import by sandersjj · Pull Request #118...
Currently when you import a file with duplicate header names you will get a SyntaxError: \League\Csv\SyntaxError. This in production throws a "Server Error" and the users has no clue what...