From 4e402ef697e3dabb519b7f6ef37eb546bd236535 Mon Sep 17 00:00:00 2001 From: Leon Kiefer Date: Tue, 11 May 2021 16:26:53 +0200 Subject: [PATCH] Use status code as fallback to status message for error responses (#1045) Use status code as fallback to the status message for error responses to detect whether the initial requests are unauthorized (HTTP 401). (With HTTP2 there is no status message and even in HTTP1 it is optional.) Fix #1018. Signed-off-by: Leon Kiefer --- bundles/org.openhab.ui/web/src/components/app.vue | 4 ++-- bundles/org.openhab.ui/web/src/js/openhab/api.js | 4 ++-- bundles/org.openhab.ui/web/src/js/openhab/auth.js | 2 +- bundles/org.openhab.ui/web/src/pages/home/homecards-mixin.js | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.ui/web/src/components/app.vue b/bundles/org.openhab.ui/web/src/components/app.vue index 14c880d43a..af75efaa2d 100644 --- a/bundles/org.openhab.ui/web/src/components/app.vue +++ b/bundles/org.openhab.ui/web/src/components/app.vue @@ -378,7 +378,7 @@ export default { return useCredentialsPromise .then(() => { return this.$oh.api.get('/rest/') }) .catch((err) => { - if (err === 'Unauthorized') { + if (err === 'Unauthorized' || err === 401) { if (!useCredentials) { // try again with credentials this.loadData(true) @@ -395,7 +395,7 @@ export default { this.storeBasicCredentials() this.loadData() }).catch((err) => { - if (err === 'Unauthorized') { + if (err === 'Unauthorized' || err === 401) { this.clearBasicCredentials() this.loadData() return Promise.reject() diff --git a/bundles/org.openhab.ui/web/src/js/openhab/api.js b/bundles/org.openhab.ui/web/src/js/openhab/api.js index c91d956d7d..c7a803c240 100644 --- a/bundles/org.openhab.ui/web/src/js/openhab/api.js +++ b/bundles/org.openhab.ui/web/src/js/openhab/api.js @@ -4,8 +4,8 @@ import { getAccessToken, getTokenInCustomHeader, getBasicCredentials } from './a function wrapPromise (f7promise) { return new Promise((resolve, reject) => { f7promise - .then((data) => resolve(data.data, data.status, data.xhr)) - .catch((err) => reject(err.message, err.status, err.xhr)) + .then((data) => resolve(data.data)) + .catch((err) => reject(err.message || err.status)) }) } diff --git a/bundles/org.openhab.ui/web/src/js/openhab/auth.js b/bundles/org.openhab.ui/web/src/js/openhab/auth.js index a638976e57..7ac37cc881 100644 --- a/bundles/org.openhab.ui/web/src/js/openhab/auth.js +++ b/bundles/org.openhab.ui/web/src/js/openhab/auth.js @@ -92,7 +92,7 @@ export function setAccessToken (token, api) { requireToken = false return Promise.resolve() }).catch((err) => { - if (err === 'Unauthorized') requireToken = true + if (err === 'Unauthorized' || err === 401) requireToken = true return Promise.resolve() }) } diff --git a/bundles/org.openhab.ui/web/src/pages/home/homecards-mixin.js b/bundles/org.openhab.ui/web/src/pages/home/homecards-mixin.js index b95ec55b97..88d514db7a 100644 --- a/bundles/org.openhab.ui/web/src/pages/home/homecards-mixin.js +++ b/bundles/org.openhab.ui/web/src/pages/home/homecards-mixin.js @@ -124,7 +124,7 @@ export default { }) .catch((err) => { console.log('Error while loading model: ' + err) - if (err === 'Unauthorized') { + if (err === 'Unauthorized' || err === 401) { authorize() } })