Skip to content

Commit

Permalink
Use status code as fallback to status message for error responses (#1045
Browse files Browse the repository at this point in the history
)

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 <leon.k97@gmx.de>
  • Loading branch information
Legion2 committed May 11, 2021
1 parent 1b002cf commit 4e402ef
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions bundles/org.openhab.ui/web/src/components/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions bundles/org.openhab.ui/web/src/js/openhab/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}

Expand Down
2 changes: 1 addition & 1 deletion bundles/org.openhab.ui/web/src/js/openhab/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default {
})
.catch((err) => {
console.log('Error while loading model: ' + err)
if (err === 'Unauthorized') {
if (err === 'Unauthorized' || err === 401) {
authorize()
}
})
Expand Down

0 comments on commit 4e402ef

Please sign in to comment.