Skip to content

Commit 8a249c0

Browse files
Better error messages for invalid CSVs
1 parent 60d538c commit 8a249c0

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ public function uploadData() {
964964
} elseif ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ) {
965965
$source = $this->handleTabularData();
966966
} else {
967-
$render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' );
967+
$render->message = esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' );
968968
}
969969
if ( $source ) {
970970
if ( $source->fetch() ) {
@@ -996,7 +996,10 @@ public function uploadData() {
996996
$render->series = json_encode( $source->getSeries() );
997997
$render->settings = json_encode( $settings );
998998
} else {
999-
$render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' );
999+
$render->message = $source->get_error();
1000+
if ( empty( $render->message ) ) {
1001+
$render->message = esc_html__( 'CSV file is broken or invalid. Please try again.', 'visualizer' );
1002+
}
10001003
}
10011004
}
10021005
$render->render();

classes/Visualizer/Source/Csv.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,18 @@ private function _fetchSeries( &$handle ) {
7777
$types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
7878
}
7979

80+
$labels = array_filter( $labels );
81+
$types = array_filter( $types );
82+
8083
if ( ! $labels || ! $types ) {
84+
$this->_error = esc_html__( 'File should have a heading row (1st row) and a data type row (2nd row). Please try again.', 'visualizer' );
8185
return false;
8286
}
8387

84-
// if no types were setup, re read labels and empty types array
8588
$types = array_map( 'trim', $types );
8689
if ( ! self::_validateTypes( $types ) ) {
87-
// re open the file
88-
fclose( $handle );
89-
$handle = $this->_get_file_handle();
90-
91-
// re read the labels and empty types array
92-
$labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
93-
$types = array();
90+
$this->_error = esc_html__( 'Invalid data types detected in the data type row (2nd row). Please try again.', 'visualizer' );
91+
return false;
9492
}
9593

9694
for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) {
@@ -134,6 +132,7 @@ protected function _get_file_handle( $filename = false ) {
134132
public function fetch() {
135133
// if filename is empty return false
136134
if ( empty( $this->_filename ) ) {
135+
$this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' );
137136
return false;
138137
}
139138

0 commit comments

Comments
 (0)