improvement(auth): verbose error message when GitHub repo unavailable (#1831)

This commit is contained in:
Shawn Erquhart
2018-11-02 10:29:11 -04:00
committed by GitHub
parent a0cfa1a92f
commit b60c94aa8f
8 changed files with 81 additions and 30 deletions

View File

@ -1,5 +1,6 @@
import trimStart from 'lodash/trimStart';
import semaphore from 'semaphore';
import { stripIndent } from 'common-tags';
import { CURSOR_COMPATIBILITY_SYMBOL } from 'netlify-cms-lib-util';
import AuthenticationPage from './AuthenticationPage';
import API from './API';
@ -39,7 +40,7 @@ export default class GitLab {
return this.authenticate(user);
}
authenticate(state) {
async authenticate(state) {
this.token = state.token;
this.api = new API({
token: this.token,
@ -47,15 +48,25 @@ export default class GitLab {
repo: this.repo,
api_root: this.api_root,
});
return this.api.user().then(user =>
this.api.hasWriteAccess(user).then(isCollab => {
// Unauthorized user
if (!isCollab)
throw new Error('Your GitLab user account does not have access to this repo.');
// Authorized user
return Object.assign({}, user, { token: state.token });
}),
);
const user = await this.api.user();
const isCollab = await this.api.hasWriteAccess(user).catch(error => {
error.message = stripIndent`
Repo "${this.repo}" not found.
Please ensure the repo information is spelled correctly.
If the repo is private, make sure you're logged into a GitLab account with access.
`;
throw error;
});
// Unauthorized user
if (!isCollab) {
throw new Error('Your GitLab user account does not have access to this repo.');
}
// Authorized user
return { ...user, token: state.token };
}
logout() {