From dd0bd2dde7c9e9294d785425d11ca536364df6de Mon Sep 17 00:00:00 2001 From: Roman Nikitenko Date: Wed, 25 Oct 2023 18:46:37 +0300 Subject: [PATCH] Handle errors at the Device Authentication flow Signed-off-by: Roman Nikitenko --- .../che-api/src/impl/github-service-impl.ts | 2 +- .../src/device-authentication.ts | 22 ++++++++++++++----- .../che-github-authentication/src/logger.ts | 4 +++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/code/extensions/che-api/src/impl/github-service-impl.ts b/code/extensions/che-api/src/impl/github-service-impl.ts index ce90054ff4d..8d548e63a90 100644 --- a/code/extensions/che-api/src/impl/github-service-impl.ts +++ b/code/extensions/che-api/src/impl/github-service-impl.ts @@ -21,7 +21,7 @@ import { Logger } from '../logger'; import { K8SServiceImpl } from './k8s-service-impl'; import { base64Decode, base64Encode, createLabelsSelector, randomString } from './utils'; -export const GIT_CREDENTIALS_PATH = path.resolve('/.git-credentials', 'credentials'); +const GIT_CREDENTIALS_PATH = path.resolve('/.git-credentials', 'credentials'); const GIT_CREDENTIAL_LABEL = { 'controller.devfile.io/git-credential': 'true' }; diff --git a/code/extensions/che-github-authentication/src/device-authentication.ts b/code/extensions/che-github-authentication/src/device-authentication.ts index 47b32ee4a22..5f15056af39 100644 --- a/code/extensions/che-github-authentication/src/device-authentication.ts +++ b/code/extensions/che-github-authentication/src/device-authentication.ts @@ -14,7 +14,7 @@ import { inject, injectable } from 'inversify'; import * as vscode from 'vscode'; import { ExtensionContext } from './extension-context'; import { GitHubAuthProvider, GithubService } from './github'; -import { Logger } from './logger'; +import { CHANNEL_NAME, Logger } from './logger'; @injectable() export class DeviceAuthentication { @@ -35,7 +35,7 @@ export class DeviceAuthentication { this.logger.info('Remove Device Authentication Token command has been registered'); } - async trigger(scopes = 'user:email'): Promise { + async trigger(scopes = 'user:email'): Promise { this.logger.info(`Device Authentication is triggered for scopes: ${scopes}`); const sessionsToRemove = await this.gitHubAuthProvider.getSessions([scopes]); @@ -57,10 +57,20 @@ export class DeviceAuthentication { const token = await vscode.commands.executeCommand('github-authentication.device-code-flow'); this.logger.info(`Device Authentication: token for scopes: ${scopes} has been generated successfully`); - await this.githubService.persistDeviceAuthToken(token); - await this.gitHubAuthProvider.createSession([scopes]); - this.onTokenGenerated(scopes); - return token; + try { + await this.githubService.persistDeviceAuthToken(token); + await this.gitHubAuthProvider.createSession([scopes]); + this.onTokenGenerated(scopes); + + return token; + } catch (error) { + const message = 'An error has occurred at the Device Authentication flow'; + + this.logger.error(`${message}: ${error.message}`); + vscode.window.showErrorMessage(`${message}, details are available in the ${CHANNEL_NAME} output channel.`); + + return undefined; + } } async removeDeviceAuthToken(): Promise { diff --git a/code/extensions/che-github-authentication/src/logger.ts b/code/extensions/che-github-authentication/src/logger.ts index 76c8c06df46..07f0574fffe 100644 --- a/code/extensions/che-github-authentication/src/logger.ts +++ b/code/extensions/che-github-authentication/src/logger.ts @@ -13,12 +13,14 @@ import { injectable } from 'inversify'; import * as vscode from 'vscode'; +export const CHANNEL_NAME = 'Che GitHub Authentication'; + @injectable() export class Logger { private outputChannel: vscode.LogOutputChannel; constructor() { - this.outputChannel = vscode.window.createOutputChannel('Che GitHub Authentication', { log: true }); + this.outputChannel = vscode.window.createOutputChannel(CHANNEL_NAME, { log: true }); } info(message: string): void {