Skip to content

Commit 3ce7977

Browse files
descriptive error messages for JSON
1 parent 8a249c0 commit 3ce7977

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 2 additions & 2 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 );

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/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)