Skip to content

Commit dd69771

Browse files
Merge pull request #462 from contactashish13/issue-459
Better error messages for invalid CSVs/URLs
2 parents dbcfade + 20e6ca8 commit dd69771

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function getJsonRoots() {
131131

132132
$roots = $source->fetchRoots();
133133
if ( empty( $roots ) ) {
134-
wp_send_json_error();
134+
wp_send_json_error( array( 'msg' => $source->get_error() ) );
135135
}
136136

137137
wp_send_json_success( array( 'url' => $params['url'], 'roots' => $roots ) );
@@ -159,7 +159,7 @@ public function getJsonData() {
159159

160160
$data = $source->parse();
161161
if ( empty( $data ) ) {
162-
wp_send_json_error();
162+
wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
163163
}
164164

165165
$data = Visualizer_Render_Layout::show( 'editor-table', $data, $chart_id, 'viz-json-table', false, false );
@@ -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/Render/Page/Data.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ class="visualizer-select">
182182
</li>
183183

184184
<li class="viz-subsection">
185-
<span class="viz-section-title visualizer_source_json"><?php _e( 'Import from JSON/REST', 'visualizer' ); ?>
185+
<span class="viz-section-title visualizer_source_json"><?php _e( 'Import from JSON', 'visualizer' ); ?>
186186
<span class="dashicons dashicons-lock"></span></span>
187187
<div class="viz-section-items section-items">
188-
<p class="viz-group-description"><?php _e( 'You can choose here to import/synchronize your chart data with a remote JSON/REST source. For more info check <a href="https://docs.themeisle.com/article/1052-how-to-generate-charts-from-json-data-rest-endpoints" target="_blank" >this</a> tutorial', 'visualizer' ); ?></p>
188+
<p class="viz-group-description"><?php _e( 'You can choose here to import/synchronize your chart data with a remote JSON source. For more info check <a href="https://docs.themeisle.com/article/1052-how-to-generate-charts-from-json-data-rest-endpoints" target="_blank" >this</a> tutorial', 'visualizer' ); ?></p>
189189
<form id="vz-import-json" action="<?php echo $upload_link; ?>" method="post" target="thehole" enctype="multipart/form-data">
190190
<div class="remote-file-section">
191191
<?php

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

classes/Visualizer/Source/Json.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ public function fetchRoots() {
107107
if ( false !== $roots ) {
108108
return $roots;
109109
}
110-
return $this->getRootElements( 'root', '', array(), $this->getJSON() );
110+
$roots = $this->getRootElements( 'root', '', array(), $this->getJSON() );
111+
if ( empty( $roots ) ) {
112+
$this->_error = esc_html__( 'This does not appear to be a valid JSON feed. Please try again.', 'visualizer' );
113+
return false;
114+
}
115+
return $roots;
111116
}
112117

113118
/**

js/frame.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@
390390
$('#json-root-form').fadeIn('medium');
391391
json_accordion_activate(1, true);
392392
}else{
393-
alert(visualizer.l10n.json_error);
393+
alert(data.data.msg);
394394
}
395395
},
396396
complete: function(){
@@ -435,7 +435,7 @@
435435
json_accordion_activate(2, false);
436436
$table.columns.adjust().draw();
437437
}else{
438-
alert(visualizer.l10n.json_error);
438+
alert(data.data.msg);
439439
}
440440
},
441441
complete: function(){

0 commit comments

Comments
 (0)