open-consul/ui-v2/app/utils/acls-status.js
John Cowen 864ffdf79b ui: Adds better error passthrough, disable/unauthorize properly on error (#5041)
1. Ensure any unexpected developer errors are passed through/shown
2. Previously when errors where returns/resolved the special
isEnabled/isAuthorized would never get resolved. This was fine as they
were set to false to start with anyway, but this resolves them again to
false for completeness
3. Improved unit testing coverage
2019-05-01 18:21:53 +00:00

65 lines
1.7 KiB
JavaScript

// This is used by all acl routes to check whether
// acls are enabled on the server, and whether the user
// has a valid token
// Right now this is very acl specific, but is likely to be
// made a bit more less specific
export default function(isValidServerError, P = Promise) {
return function(obj) {
const propName = Object.keys(obj)[0];
const p = obj[propName];
let authorize;
let enable;
return {
isAuthorized: new P(function(resolve) {
authorize = function(bool) {
resolve(bool);
};
}),
isEnabled: new P(function(resolve) {
enable = function(bool) {
resolve(bool);
};
}),
[propName]: p
.catch(function(e) {
if (e.errors && e.errors[0]) {
switch (e.errors[0].status) {
case '500':
if (isValidServerError(e)) {
enable(true);
authorize(false);
} else {
enable(false);
authorize(false);
return P.reject(e);
}
break;
case '403':
enable(true);
authorize(false);
break;
case '401':
enable(false);
authorize(false);
break;
default:
enable(false);
authorize(false);
throw e;
}
return [];
}
enable(false);
authorize(false);
throw e;
})
.then(function(res) {
enable(true);
authorize(true);
return res;
}),
};
};
}