better error handling and user interaction

This commit is contained in:
hippoz 2020-10-13 22:17:09 +03:00
parent d3c153dc26
commit 53952adcc4

View file

@ -54,7 +54,14 @@
}, },
methods: { methods: {
sendMessage: async function(webhook, message) { sendMessage: async function(webhook, message) {
if (!webhook.url || !webhook.name || !message) return; if (!webhook.url || !webhook.name) {
alert('Invalid webhook does not have url or name property');
return;
}
if (!message) {
alert('Message is required');
return
}
const res = await fetch(webhook.url, { const res = await fetch(webhook.url, {
method: 'POST', method: 'POST',
@ -66,7 +73,10 @@
}) })
}); });
console.log(await res.text()); if (res.status !== 200) {
alert(`Request failed with status code ${res.status} and content "${await res.text()}" (POST ${webhook.url})`);
return;
}
}, },
createWebhook: function() { createWebhook: function() {
const name = prompt('Webhook name'); const name = prompt('Webhook name');
@ -78,14 +88,19 @@
this.webhooks.push({ name, url }); this.webhooks.push({ name, url });
this.saveToLocalstorage(); this.saveToLocalstorage();
alert(`Webhook "${name}" successfully created`);
}, },
deleteWebhookByIndex: function(index) { deleteWebhookByIndex: function(index) {
this.webhooks.splice(index, 1); this.webhooks.splice(index, 1);
}, },
deleteCurrentWebhook: function() { deleteCurrentWebhook: function() {
this.selectedWebhook = -1; const yn = confirm(`Delete webhook ${this.selectedWebhookObject.name}?`);
this.deleteWebhookByIndex(this.selectedWebhook); if (yn) {
this.saveToLocalstorage(); this.selectedWebhook = -1;
this.deleteWebhookByIndex(this.selectedWebhook);
this.saveToLocalstorage();
alert(`Webhook successfully deleted`);
}
}, },
saveToLocalstorage: function() { saveToLocalstorage: function() {
localStorage.setItem('webhooks', JSON.stringify(this.webhooks)); localStorage.setItem('webhooks', JSON.stringify(this.webhooks));
@ -99,14 +114,14 @@
try { try {
const webhooksJSON = JSON.parse(savedWebhooks); const webhooksJSON = JSON.parse(savedWebhooks);
if (!webhooksJSON) { if (!webhooksJSON) {
console.error('Parsing localstorage data returned undefined, clearing localstorage...', e); alert('Parsing localstorage data returned undefined, clearing localstorage...', e);
this.webhooks = []; this.webhooks = [];
localStorage.clear(); localStorage.clear();
return; return;
} }
this.webhooks = webhooksJSON; this.webhooks = webhooksJSON;
} catch(e) { } catch(e) {
console.error('Error while parsing saved localstorage data, clearing localstorage...', e); alert('Error while parsing saved localstorage data, clearing localstorage...', e);
this.webhooks = []; this.webhooks = [];
localStorage.clear(); localStorage.clear();
return; return;