Skip to content

Commit 60fb404

Browse files
committed
Added error feedback to the demo
1 parent a9dc28f commit 60fb404

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

docs/demos/leaflet-graphql/index.html

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ <h3>Edit GraphQL Query</h3>
9999
<button id="loadComplexExample">Load Complex Example</button>
100100
<a id="endpointLink" href="" target="_blank">View JSON on Endpoint</a>
101101
</div>
102+
<textarea id="output" readonly placeholder="Messages appear here"></textarea>
102103
</div>
103104
<div id="map"></div>
104105

@@ -195,6 +196,8 @@ <h3>Edit GraphQL Query</h3>
195196

196197
const initialQuery = simpleQuery;
197198

199+
const output = document.getElementById('output');
200+
198201
// Initialize the Leaflet map
199202
const map = L.map('map').setView([0, 0], 2);
200203

@@ -205,17 +208,29 @@ <h3>Edit GraphQL Query</h3>
205208

206209
// Fetch data from the GraphQL API
207210
async function fetchData(query) {
208-
const response = await fetch(GRAPHQL_URL, {
209-
method: 'POST',
210-
headers: {
211-
'Content-Type': 'application/json',
212-
},
213-
body: JSON.stringify({ query })
214-
});
211+
output.value = '[LOADING...]';
212+
try {
213+
const response = await fetch(GRAPHQL_URL, {
214+
method: 'POST',
215+
headers: {
216+
'Content-Type': 'application/json',
217+
},
218+
body: JSON.stringify({ query })
219+
});
215220

216-
const json = await response.json();
217-
const locations = json.data.locations;
218-
return locations;
221+
if (!response.ok) {
222+
const msg = await response.text();
223+
throw new Error(`[ERROR] Server message: ${msg}`);
224+
}
225+
226+
const json = await response.json();
227+
output.value = '[SUCCESS]';
228+
const locations = json.data.locations;
229+
return locations;
230+
} catch (error) {
231+
output.value = error.message;
232+
throw new Error(error);
233+
}
219234
}
220235

221236
// Function to create HTML for the popup with all JSON keys and values

0 commit comments

Comments
 (0)