Skip to content

Commit

Permalink
add better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv committed Feb 6, 2024
1 parent 2e4bf2a commit 3432bf8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '16'

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Pages
uses: actions/configure-pages@v3
uses: actions/configure-pages@v4
- name: Build
run: make build && make pages
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
uses: actions/upload-pages-artifact@v3
with:
path: 'pages'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
uses: actions/deploy-pages@v4
64 changes: 54 additions & 10 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,63 @@ class MeetingDataProcessor {
this.isProcessingKML = false;
}

// D ynamically load JSONP data
// Load JSONP data
fetchMeetings(query, isCSV, isKML) {
MeetingDataProcessor.clearError();
MeetingDataProcessor.hideLinks();
return new Promise((resolve, reject) => {
const script = document.createElement("script");
const callbackName = `jsonpCallback_${Date.now()}`;

window[callbackName] = (data) => {
document.body.removeChild(script);
delete window[callbackName];
this.handleMeetingsData(data, isCSV, isKML);
resolve(data);

// check for empty array
if (Array.isArray(data) && data.length === 0) {
const errorMsg = "No data found";
MeetingDataProcessor.displayError(errorMsg);
reject(new Error(errorMsg));
} else {
this.handleMeetingsData(data, isCSV, isKML);
resolve(data);
}
};

script.src = `${query}&callback=${callbackName}`;
script.onerror = () => reject(new Error("Script loading failed"));
script.onerror = () => {
const errorMsg = "Error loading data";
MeetingDataProcessor.displayError(errorMsg);
reject(new Error(errorMsg));
};
document.body.appendChild(script);

this.isProcessingCSV = isCSV;
this.isProcessingKML = isKML;
});
}

// handle data once it's fetched
static displayError(message) {
const errorContainer = document.getElementById("errorMessages");
if (errorContainer) {
errorContainer.textContent = message;
errorContainer.style.display = "block";
} else {
console.error("Error container not found in the document.");
}
}

static clearError() {
const errorContainer = document.getElementById("errorMessages");
if (errorContainer) {
errorContainer.style.display = "none";
errorContainer.textContent = "";
} else {
console.error("Error container not found in the document.");
}
}

// Handle data once it's fetched
handleMeetingsData(meetings, isCSV, isKML) {
if (isCSV) {
this.exportCSV(meetings);
Expand All @@ -49,13 +83,20 @@ class MeetingDataProcessor {
// KML export functionality
exportKML(meetings) {
const kmlContent = `data:text/xml;charset=utf-8,${encodeURIComponent(
this.convertToKML(meetings)
this.constructor.convertToKML(meetings)
)}`;
const kmlDownloadLink = document.getElementById("kmlDownloadLink");
kmlDownloadLink.href = kmlContent;
kmlDownloadLink.style.display = "block";
}

static hideLinks() {
const downloadLink = document.getElementById("downloadLink");
downloadLink.style.display = "none";
const kmlDownloadLink = document.getElementById("kmlDownloadLink");
kmlDownloadLink.style.display = "none";
}

// Convert data to CSV
static convertToCSV(data) {
const csvRows = [];
Expand All @@ -80,7 +121,7 @@ class MeetingDataProcessor {
}

// Convert data to KML
convertToKML(data) {
static convertToKML(data) {
let kmlContent = `<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>`;
Expand All @@ -92,8 +133,8 @@ class MeetingDataProcessor {
const lat = parseFloat(meeting["latitude"]);
if (!lng || !lat) return "";

const description = this.constructor.prepareSimpleLine(meeting);
const address = this.constructor.prepareSimpleLine(meeting, false);
const description = MeetingDataProcessor.prepareSimpleLine(meeting);
const address = MeetingDataProcessor.prepareSimpleLine(meeting, false);

return ` <Placemark>
<name>${name}</name>
Expand Down Expand Up @@ -190,7 +231,9 @@ class MeetingDataProcessor {
// start the export process
exportData(query) {
if (!query.includes("/client_interface/jsonp")) {
alert("Invalid BMLT query URL, must use jsonp endpoint.");
MeetingDataProcessor.displayError(
"Invalid BMLT query URL, must use jsonp endpoint."
);
return;
}
const isCSV = true;
Expand All @@ -201,6 +244,7 @@ class MeetingDataProcessor {
}
}

// Triggers data export process, bound to button click event
function exportData() {
const query = document.getElementById("query").value;
const processor = new MeetingDataProcessor();
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ <h1>BMLT Data Converter</h1>
<button onclick="exportData()">Generate Export Data</button>
<a id="downloadLink" style="display: none;" download="BMLT_data.csv">Download CSV</a>
<a id="kmlDownloadLink" style="display: none;" download="BMLT_data.kml">Download KML</a>
<div id="errorMessages" style="color: red; display: none; text-align: center; padding-top: 15px"></div>
</div>
<script src="assets/js/main.js"></script>
</body>
Expand Down

0 comments on commit 3432bf8

Please sign in to comment.