Merge pull request #12489 from marcusmoore/fix/encoding-errors-in-importer
Improve error messaging around invalid characters in import
This commit is contained in:
commit
35ab4a4cce
1 changed files with 33 additions and 9 deletions
|
@ -10,6 +10,7 @@ use App\Models\Asset;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
use App\Models\Import;
|
use App\Models\Import;
|
||||||
use Artisan;
|
use Artisan;
|
||||||
|
use Illuminate\Database\Eloquent\JsonEncodingException;
|
||||||
use Illuminate\Support\Facades\Request;
|
use Illuminate\Support\Facades\Request;
|
||||||
use Illuminate\Support\Facades\Session;
|
use Illuminate\Support\Facades\Session;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
@ -35,7 +36,7 @@ class ImportController extends Controller
|
||||||
* Process and store a CSV upload file.
|
* Process and store a CSV upload file.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
* @param \Illuminate\Http\Request $request
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\JsonResponse
|
||||||
*/
|
*/
|
||||||
public function store()
|
public function store()
|
||||||
{
|
{
|
||||||
|
@ -56,7 +57,7 @@ class ImportController extends Controller
|
||||||
'text/tsv', ])) {
|
'text/tsv', ])) {
|
||||||
$results['error'] = 'File type must be CSV. Uploaded file is '.$file->getMimeType();
|
$results['error'] = 'File type must be CSV. Uploaded file is '.$file->getMimeType();
|
||||||
|
|
||||||
return response()->json(Helper::formatStandardApiResponse('error', null, $results['error']), 500);
|
return response()->json(Helper::formatStandardApiResponse('error', null, $results['error']), 422);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: is there a lighter way to do this?
|
//TODO: is there a lighter way to do this?
|
||||||
|
@ -64,7 +65,19 @@ class ImportController extends Controller
|
||||||
ini_set('auto_detect_line_endings', '1');
|
ini_set('auto_detect_line_endings', '1');
|
||||||
}
|
}
|
||||||
$reader = Reader::createFromFileObject($file->openFile('r')); //file pointer leak?
|
$reader = Reader::createFromFileObject($file->openFile('r')); //file pointer leak?
|
||||||
$import->header_row = $reader->fetchOne(0);
|
|
||||||
|
try {
|
||||||
|
$import->header_row = $reader->fetchOne(0);
|
||||||
|
} catch (JsonEncodingException $e) {
|
||||||
|
return response()->json(
|
||||||
|
Helper::formatStandardApiResponse(
|
||||||
|
'error',
|
||||||
|
null,
|
||||||
|
'One or more attributes in the header row contain malformed UTF-8 characters'
|
||||||
|
),
|
||||||
|
422
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
//duplicate headers check
|
//duplicate headers check
|
||||||
$duplicate_headers = [];
|
$duplicate_headers = [];
|
||||||
|
@ -82,11 +95,22 @@ class ImportController extends Controller
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (count($duplicate_headers) > 0) {
|
if (count($duplicate_headers) > 0) {
|
||||||
return response()->json(Helper::formatStandardApiResponse('error', null, implode('; ', $duplicate_headers)), 500); //should this be '4xx'?
|
return response()->json(Helper::formatStandardApiResponse('error', null, implode('; ', $duplicate_headers)),422);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grab the first row to display via ajax as the user picks fields
|
try {
|
||||||
$import->first_row = $reader->fetchOne(1);
|
// Grab the first row to display via ajax as the user picks fields
|
||||||
|
$import->first_row = $reader->fetchOne(1);
|
||||||
|
} catch (JsonEncodingException $e) {
|
||||||
|
return response()->json(
|
||||||
|
Helper::formatStandardApiResponse(
|
||||||
|
'error',
|
||||||
|
null,
|
||||||
|
'One or more attributes in row 2 contain malformed UTF-8 characters'
|
||||||
|
),
|
||||||
|
422
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$date = date('Y-m-d-his');
|
$date = date('Y-m-d-his');
|
||||||
$fixed_filename = str_slug($file->getClientOriginalName());
|
$fixed_filename = str_slug($file->getClientOriginalName());
|
||||||
|
@ -108,12 +132,12 @@ class ImportController extends Controller
|
||||||
}
|
}
|
||||||
$results = (new ImportsTransformer)->transformImports($results);
|
$results = (new ImportsTransformer)->transformImports($results);
|
||||||
|
|
||||||
return [
|
return response()->json([
|
||||||
'files' => $results,
|
'files' => $results,
|
||||||
];
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.feature_disabled')), 500);
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.feature_disabled')), 422);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue