Skip to content
Snippets Groups Projects
Commit f7ccf9fb authored by michael.minelli's avatar michael.minelli
Browse files

HttpManager => Add support for client version errors from server

parent 00d45704
No related branches found
No related tags found
No related merge requests found
Pipeline #26572 passed
import axios, { AxiosRequestHeaders } from 'axios';
import SessionManager from './SessionManager';
import FormData from 'form-data';
import logger from '../shared/logging/WinstonLogger';
import GitlabManager from './GitlabManager';
import { StatusCodes } from 'http-status-codes';
import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig';
import { version } from '../config/Version';
import DojoBackendResponse from '../shared/types/Dojo/DojoBackendResponse';
import DojoStatusCode from '../shared/types/Dojo/DojoStatusCode';
import boxen from 'boxen';
import Config from '../config/Config';
class HttpManager {
public handleCommandErrors: boolean = true;
public handleAuthorizationCommandErrors: boolean = true;
registerAxiosInterceptor() {
this.registerRequestInterceptor();
......@@ -32,6 +36,9 @@ class HttpManager {
if ( SessionManager.isLogged ) {
config.headers.Authorization = `Bearer ${ SessionManager.token }`;
}
config.headers['client'] = 'DojoCLI';
config.headers['client-version'] = version;
}
if ( GitlabManager.isLogged && config.url && config.url.indexOf(ClientsSharedConfig.gitlab.apiURL) !== -1 ) {
......@@ -42,6 +49,19 @@ class HttpManager {
});
}
private requestError(message: string) {
console.log(boxen(message, {
title : 'Request error',
titleAlignment: 'center',
borderColor : 'red',
borderStyle : 'bold',
margin : 1,
padding : 1,
textAlignment : 'left'
}));
process.exit(1);
}
private registerResponseInterceptor() {
axios.interceptors.response.use((response) => {
if ( response.data && response.data.sessionToken ) {
......@@ -51,28 +71,39 @@ class HttpManager {
return response;
}, (error) => {
if ( error.response ) {
if ( this.handleCommandErrors ) {
if ( error.response.status === StatusCodes.METHOD_NOT_ALLOWED && error.response.data ) {
const data: DojoBackendResponse<{}> = error.response.data;
switch ( data.code ) {
case DojoStatusCode.CLIENT_NOT_SUPPORTED:
this.requestError('Client not recognized by the server. Please contact the administrator.');
break;
case DojoStatusCode.CLIENT_VERSION_NOT_SUPPORTED:
this.requestError(`CLI version not anymore supported by the server. Please update the CLI.\nYou can download the latest stable version (latest release without "-dev" suffix) on this page:\n${ Config.gitlab.cliReleasePage }`);
break;
default:
break;
}
}
if ( this.handleAuthorizationCommandErrors ) {
if ( error.response.url && error.response.url.indexOf(ClientsSharedConfig.apiURL) !== -1 ) {
switch ( error.response.status ) {
case StatusCodes.UNAUTHORIZED: // Unauthorized
logger.error('Session expired or inexistent. Please login again.');
process.exit(1);
case StatusCodes.UNAUTHORIZED:
this.requestError('Session expired or does not exist. Please login again.');
break;
case StatusCodes.FORBIDDEN: // Forbidden
logger.error('Forbidden access.');
process.exit(1);
case StatusCodes.FORBIDDEN:
this.requestError('Forbidden access.');
break;
}
}
} else {
this.handleCommandErrors = true;
this.handleAuthorizationCommandErrors = true;
}
} else {
logger.error('Error connecting to the server.');
process.exit(1);
this.requestError('Error connecting to the server. Please check your internet connection. If the problem persists, please contact the administrator.');
}
return Promise.reject(error);
});
}
......
......@@ -97,7 +97,7 @@ class SessionManager {
ora('Checking Dojo session: ').start().info();
}
HttpManager.handleCommandErrors = false;
HttpManager.handleAuthorizationCommandErrors = false;
const spinner: ora.Ora = ora({
text : `Testing Dojo session`,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment