Skip to content

Commit

Permalink
Better error handle in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
RadhiFadlillah committed Aug 20, 2019
1 parent f62fd30 commit f50d0cc
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 44 deletions.
4 changes: 2 additions & 2 deletions internal/view/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
})
.catch(err => {
this.dialog.loading = false;
err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
});
}
Expand Down
17 changes: 14 additions & 3 deletions internal/view/js/page/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,29 @@ export default {
if (cfg.escPressed) base.escPressed = cfg.escPressed;
this.dialog = base;
},
async getErrorMessage(err) {
switch (err.constructor) {
case Error:
return err.message;
case Response:
var text = await err.text();
return `${text} (${err.status})`;
default:
return err;
}
},
isSessionError(err) {
switch (err.trim().toLowerCase()) {
switch (err.replace(/\(\d+\)/g, "").trim().toLowerCase()) {
case "session is not exist":
case "session has been expired":
return true
default:
return false;
}
},
showErrorDialog(msg, status) {
showErrorDialog(msg) {
var sessionError = this.isSessionError(msg),
dialogContent = sessionError ? "Session has expired, please login again." : `${msg} (${status})`;
dialogContent = sessionError ? "Session has expired, please login again." : msg;

this.showDialog({
visible: true,
Expand Down
28 changes: 14 additions & 14 deletions internal/view/js/page/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ export default {
this.loading = false;

if (err !== skipFetchTags) {
err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
}
});
Expand Down Expand Up @@ -419,8 +419,8 @@ export default {
})
.catch(err => {
this.dialog.loading = false;
err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
});
}
Expand Down Expand Up @@ -513,8 +513,8 @@ export default {
})
.catch(err => {
this.dialog.loading = false;
err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
});
}
Expand Down Expand Up @@ -582,8 +582,8 @@ export default {
this.editMode = false;
this.dialog.loading = false;

err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
});
}
Expand Down Expand Up @@ -657,8 +657,8 @@ export default {
this.editMode = false;
this.dialog.loading = false;

err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
});
}
Expand Down Expand Up @@ -740,8 +740,8 @@ export default {
this.editMode = false;
this.dialog.loading = false;

err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
});
}
Expand Down Expand Up @@ -820,8 +820,8 @@ export default {
this.dialog.loading = false;
this.dialogTags.visible = false;
this.dialogTags.editMode = false;
err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
});
},
Expand Down
16 changes: 8 additions & 8 deletions internal/view/js/page/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export default {
})
.catch(err => {
this.loading = false;
err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
});
},
Expand Down Expand Up @@ -186,8 +186,8 @@ export default {
})
.catch(err => {
this.dialog.loading = false;
err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
});
}
Expand Down Expand Up @@ -256,8 +256,8 @@ export default {
})
.catch(err => {
this.dialog.loading = false;
err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
});
}
Expand Down Expand Up @@ -289,8 +289,8 @@ export default {
})
.catch(err => {
this.dialog.loading = false;
err.text().then(msg => {
this.showErrorDialog(msg, err.status);
this.getErrorMessage(err).then(msg => {
this.showErrorDialog(msg);
})
});
}
Expand Down
15 changes: 13 additions & 2 deletions internal/view/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@
nightMode: false,
},
methods: {
async getErrorMessage(err) {
switch (err.constructor) {
case Error:
return err.message;
case Response:
var text = await err.text();
return `${text} (${err.status})`;
default:
return err;
}
},
login() {
// Validate input
if (this.username === "") {
Expand Down Expand Up @@ -96,8 +107,8 @@
})
.catch(err => {
this.loading = false;
err.text().then(msg => {
this.error = `${msg} (${err.status})`;
this.getErrorMessage(err).then(msg => {
this.error = msg;
})
});
},
Expand Down
Loading

0 comments on commit f50d0cc

Please sign in to comment.