From 8cd2fe963cce61a35fb286c879224480a20f8f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Levasseur?= Date: Tue, 18 Nov 2025 11:21:38 -0500 Subject: [PATCH] fix(cli): add some context to error throwing in add command (#14547) --- packages/cli/package.json | 2 +- .../command-implementations/add-command.ts | 6 ++- .../command-implementations/global-command.ts | 52 ++++++++++--------- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index 5ce9c87156f..3d9e2acabc9 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@botpress/cli", - "version": "4.25.0", + "version": "4.25.1", "description": "Botpress CLI", "scripts": { "build": "pnpm run build:types && pnpm run bundle && pnpm run template:gen", diff --git a/packages/cli/src/command-implementations/add-command.ts b/packages/cli/src/command-implementations/add-command.ts index 1b5338458b8..8c7120df293 100644 --- a/packages/cli/src/command-implementations/add-command.ts +++ b/packages/cli/src/command-implementations/add-command.ts @@ -41,7 +41,11 @@ export class AddCommand extends GlobalCommand { if (ref) { return await this._addNewSinglePackage({ ...ref, alias: this.argv.alias }) } - const pkgJson = await utils.pkgJson.readPackageJson(this.argv.installPath) + + const pkgJson = await utils.pkgJson.readPackageJson(this.argv.installPath).catch((thrown) => { + throw errors.BotpressCLIError.wrap(thrown, 'failed to read package.json file') + }) + if (!pkgJson) { this.logger.warn('No package.json found in the install path') return diff --git a/packages/cli/src/command-implementations/global-command.ts b/packages/cli/src/command-implementations/global-command.ts index 39a966856a8..c090a24b7ac 100644 --- a/packages/cli/src/command-implementations/global-command.ts +++ b/packages/cli/src/command-implementations/global-command.ts @@ -81,35 +81,39 @@ export abstract class GlobalCommand extends B } protected async getAuthenticatedClient(credentials: Partial>) { - const cache = this.globalCache - - let token: string | undefined - let workspaceId: string | undefined - let apiUrl: string | undefined + try { + const cache = this.globalCache + + let token: string | undefined + let workspaceId: string | undefined + let apiUrl: string | undefined + + if (this.argv.profile) { + if (credentials.token || credentials.workspaceId || credentials.apiUrl) { + this.logger.warn( + 'You are currently using credential command line arguments or environment variables as well as a profile. Your profile has overwritten the variables' + ) + } + ;({ token, workspaceId, apiUrl } = await this.readProfileFromFS(this.argv.profile)) + this.logger.log(`Using profile "${this.argv.profile}"`, { prefix: '👤' }) + } else { + token = credentials.token ?? (await cache.get('token')) + workspaceId = credentials.workspaceId ?? (await cache.get('workspaceId')) + apiUrl = credentials.apiUrl ?? (await cache.get('apiUrl')) + } - if (this.argv.profile) { - if (credentials.token || credentials.workspaceId || credentials.apiUrl) { - this.logger.warn( - 'You are currently using credential command line arguments or environment variables as well as a profile. Your profile has overwritten the variables' - ) + if (!(token && workspaceId && apiUrl)) { + return null } - ;({ token, workspaceId, apiUrl } = await this.readProfileFromFS(this.argv.profile)) - this.logger.log(`Using profile "${this.argv.profile}"`, { prefix: '👤' }) - } else { - token = credentials.token ?? (await cache.get('token')) - workspaceId = credentials.workspaceId ?? (await cache.get('workspaceId')) - apiUrl = credentials.apiUrl ?? (await cache.get('apiUrl')) - } - if (!(token && workspaceId && apiUrl)) { - return null - } + if (apiUrl !== consts.defaultBotpressApiUrl) { + this.logger.log(`Using custom url ${apiUrl}`, { prefix: '🔗' }) + } - if (apiUrl !== consts.defaultBotpressApiUrl) { - this.logger.log(`Using custom url ${apiUrl}`, { prefix: '🔗' }) + return this.api.newClient({ apiUrl, token, workspaceId }, this.logger) + } catch (thrown) { + throw errors.BotpressCLIError.wrap(thrown, 'failed to create authenticated client') } - - return this.api.newClient({ apiUrl, token, workspaceId }, this.logger) } protected async readProfileFromFS(profile: string): Promise {