Skip to content

Commit

Permalink
Add error class for config file issues (evcc-io#14495)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Jul 10, 2024
1 parent 71ac9e2 commit bcb29b3
Show file tree
Hide file tree
Showing 24 changed files with 235 additions and 137 deletions.
18 changes: 18 additions & 0 deletions assets/js/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { reactive, watch } from "vue";
import api from "./api";
import store from "./store";
import Modal from "bootstrap/js/dist/modal";
import { isSystemError } from "./utils/fatal";

const auth = reactive({
configured: true,
Expand All @@ -9,6 +11,11 @@ const auth = reactive({
});

export async function updateAuthStatus() {
if (store.state.offline || isSystemError(store.state.fatal)) {
// system not ready, skip auth check
return;
}

try {
const res = await api.get("/auth/status", {
validateStatus: (code) => [200, 501, 500].includes(code),
Expand Down Expand Up @@ -72,4 +79,15 @@ watch(
}
);

let timeoutId = null;
function debounedUpdateAuthStatus() {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
updateAuthStatus();
}, 500);
}

watch(() => store.state.offline, debounedUpdateAuthStatus);
watch(() => store.state.fatal, debounedUpdateAuthStatus);

export default auth;
4 changes: 3 additions & 1 deletion assets/js/components/TopNavigation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ import collector from "../mixins/collector";
import { logout, isLoggedIn, openLoginModal } from "../auth";
import baseAPI from "../baseapi";
import { isApp, sendToApp } from "../utils/native";
import { isUserConfigError } from "../utils/fatal";
export default {
name: "TopNavigation",
Expand Down Expand Up @@ -168,7 +169,8 @@ export default {
return this.logoutCount > 0;
},
showBadge() {
return this.loginRequired || this.sponsor.expiresSoon || this.fatal?.error;
const userConfigError = isUserConfigError(this.fatal);
return this.loginRequired || this.sponsor.expiresSoon || userConfigError;
},
badgeClass() {
if (this.fatal?.error) {
Expand Down
5 changes: 4 additions & 1 deletion assets/js/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createRouter, createWebHashHistory } from "vue-router";
import Modal from "bootstrap/js/dist/modal";
import Main from "./views/Main.vue";
import { ensureCurrentLocaleMessages } from "./i18n";
import { openLoginModal, statusUnknown, updateAuthStatus, isLoggedIn } from "./auth";
import { openLoginModal, statusUnknown, updateAuthStatus, isLoggedIn, isConfigured } from "./auth";

function hideAllModals() {
[...document.querySelectorAll(".modal.show")].forEach((modal) => {
Expand All @@ -18,6 +18,9 @@ function hideAllModals() {

async function ensureAuth(to) {
await updateAuthStatus();
if (!isConfigured()) {
return false;
}
if (!isLoggedIn() && !statusUnknown()) {
openLoginModal(to.path);
return false;
Expand Down
4 changes: 4 additions & 0 deletions assets/js/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ function setProperty(obj, props, value) {
}

const state = reactive({
offline: false,
loadpoints: [], // ensure array type
});

const store = {
state,
offline: function (value) {
state.offline = value;
},
update: function (msg) {
Object.keys(msg).forEach(function (k) {
if (k === "log") {
Expand Down
21 changes: 21 additions & 0 deletions assets/js/utils/fatal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const FATALS = ["configfile", "database"];

function isError(fatal) {
return !!fatal?.error;
}

export function isUserConfigError(fatal) {
if (!isError(fatal)) {
return false;
}

const errorClass = fatal?.class;
if (FATALS.includes(errorClass)) {
return false;
}
return true;
}

export function isSystemError(fatal) {
return isError(fatal) && !isUserConfigError(fatal);
}
14 changes: 5 additions & 9 deletions assets/js/views/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import PasswordModal from "../components/PasswordModal.vue";
import LoginModal from "../components/LoginModal.vue";
import HelpModal from "../components/HelpModal.vue";
import collector from "../mixins/collector";
import { updateAuthStatus } from "../auth";
// assume offline if not data received for 5 minutes
let lastDataReceived = new Date();
Expand Down Expand Up @@ -61,9 +60,9 @@ export default {
this.reload();
}
},
offline: function (now, prev) {
updateAuthStatus();
if (now && !prev) {
offline: function (offline) {
store.offline(offline);
if (offline) {
this.reconnect();
}
},
Expand All @@ -88,7 +87,6 @@ export default {
mounted: function () {
this.connect();
document.addEventListener("visibilitychange", this.pageVisibilityChanged, false);
updateAuthStatus();
},
unmounted: function () {
this.disconnect();
Expand All @@ -102,7 +100,6 @@ export default {
this.disconnect();
} else {
this.connect();
updateAuthStatus();
}
},
reconnect: function () {
Expand All @@ -113,7 +110,6 @@ export default {
}, 2500);
},
disconnect: function () {
console.log("websocket disconnecting");
if (this.ws) {
this.ws.onerror = null;
this.ws.onopen = null;
Expand Down Expand Up @@ -150,16 +146,16 @@ export default {
this.ws = new WebSocket(uri);
this.ws.onerror = () => {
console.error({ message: "Websocket error. Trying to reconnect." });
console.log({ message: "Websocket error. Trying to reconnect." });
this.ws.close();
};
this.ws.onopen = () => {
console.log("websocket connected");
window.app.setOnline();
};
this.ws.onclose = () => {
console.log("websocket disconnected");
window.app.setOffline();
this.reconnect();
};
this.ws.onmessage = (evt) => {
try {
Expand Down
140 changes: 72 additions & 68 deletions cmd/class_enumer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Class int
//go:generate enumer -type Class -trimprefix Class -transform=lower -text
const (
_ Class = iota
ClassConfigFile
ClassMeter
ClassCharger
ClassVehicle
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func runRoot(cmd *cobra.Command, args []string) {
log.FATAL.Fatal(err)
}
} else {
err = cfgErr
err = wrapErrorWithClass(ClassConfigFile, cfgErr)
}

// setup environment
Expand Down
Loading

0 comments on commit bcb29b3

Please sign in to comment.