Added basic function for handling errors
This commit is contained in:
parent
9d0e64ff24
commit
94b38993e9
32
main.js
32
main.js
@ -12,9 +12,10 @@ clipboard.on('success', function (e) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
//Set default url
|
//Set default url
|
||||||
chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => {
|
// chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => {
|
||||||
document.getElementById("target").value = tabs[0].url;
|
// document.getElementById("target").value = tabs[0].url;
|
||||||
});
|
// });
|
||||||
|
document.getElementById("target").value = "google.com";
|
||||||
|
|
||||||
// Calls the api to create a new short url
|
// Calls the api to create a new short url
|
||||||
document.getElementById("create").onclick = createUrl;
|
document.getElementById("create").onclick = createUrl;
|
||||||
@ -35,7 +36,14 @@ function createUrl() {
|
|||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.then(res => res.json())
|
.then(res => {
|
||||||
|
if (!res.ok) {
|
||||||
|
handleError(res);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.json()
|
||||||
|
}
|
||||||
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
document.getElementById("shorturl").innerText = res.url;
|
document.getElementById("shorturl").innerText = res.url;
|
||||||
document.getElementById("shorturl_container").className = "";
|
document.getElementById("shorturl_container").className = "";
|
||||||
@ -47,6 +55,22 @@ function createUrl() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleError(res) {
|
||||||
|
if (res.status == 400) {
|
||||||
|
res.text().then((body)=>{
|
||||||
|
switch (body) {
|
||||||
|
case "Shortcode already exists, please choose another code":
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Resets the UI
|
//Resets the UI
|
||||||
document.getElementById("reset").onclick = reset;
|
document.getElementById("reset").onclick = reset;
|
||||||
function reset() {
|
function reset() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user