diff --git a/NodeApp/src/managers/HttpManager.ts b/NodeApp/src/managers/HttpManager.ts index 2be4d248355e51f317c718e4657f481a7a0e7cbd..b95922e2293eadc197a7ac2c5e3d50df66ac974e 100644 --- a/NodeApp/src/managers/HttpManager.ts +++ b/NodeApp/src/managers/HttpManager.ts @@ -1,14 +1,18 @@ 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); }); } diff --git a/NodeApp/src/managers/SessionManager.ts b/NodeApp/src/managers/SessionManager.ts index 6e492fa8a103e1f0904778931d18e3499782ef8f..28082f59a9345997c53ddb8cb4a04eeab60e43eb 100644 --- a/NodeApp/src/managers/SessionManager.ts +++ b/NodeApp/src/managers/SessionManager.ts @@ -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`,