Skip to content

Commit 8cd2fe9

Browse files
fix(cli): add some context to error throwing in add command (botpress#14547)
1 parent 1e6ef30 commit 8cd2fe9

3 files changed

Lines changed: 34 additions & 26 deletions

File tree

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@botpress/cli",
3-
"version": "4.25.0",
3+
"version": "4.25.1",
44
"description": "Botpress CLI",
55
"scripts": {
66
"build": "pnpm run build:types && pnpm run bundle && pnpm run template:gen",

packages/cli/src/command-implementations/add-command.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ export class AddCommand extends GlobalCommand<AddCommandDefinition> {
4141
if (ref) {
4242
return await this._addNewSinglePackage({ ...ref, alias: this.argv.alias })
4343
}
44-
const pkgJson = await utils.pkgJson.readPackageJson(this.argv.installPath)
44+
45+
const pkgJson = await utils.pkgJson.readPackageJson(this.argv.installPath).catch((thrown) => {
46+
throw errors.BotpressCLIError.wrap(thrown, 'failed to read package.json file')
47+
})
48+
4549
if (!pkgJson) {
4650
this.logger.warn('No package.json found in the install path')
4751
return

packages/cli/src/command-implementations/global-command.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,39 @@ export abstract class GlobalCommand<C extends GlobalCommandDefinition> extends B
8181
}
8282

8383
protected async getAuthenticatedClient(credentials: Partial<YargsConfig<typeof config.schemas.credentials>>) {
84-
const cache = this.globalCache
85-
86-
let token: string | undefined
87-
let workspaceId: string | undefined
88-
let apiUrl: string | undefined
84+
try {
85+
const cache = this.globalCache
86+
87+
let token: string | undefined
88+
let workspaceId: string | undefined
89+
let apiUrl: string | undefined
90+
91+
if (this.argv.profile) {
92+
if (credentials.token || credentials.workspaceId || credentials.apiUrl) {
93+
this.logger.warn(
94+
'You are currently using credential command line arguments or environment variables as well as a profile. Your profile has overwritten the variables'
95+
)
96+
}
97+
;({ token, workspaceId, apiUrl } = await this.readProfileFromFS(this.argv.profile))
98+
this.logger.log(`Using profile "${this.argv.profile}"`, { prefix: '👤' })
99+
} else {
100+
token = credentials.token ?? (await cache.get('token'))
101+
workspaceId = credentials.workspaceId ?? (await cache.get('workspaceId'))
102+
apiUrl = credentials.apiUrl ?? (await cache.get('apiUrl'))
103+
}
89104

90-
if (this.argv.profile) {
91-
if (credentials.token || credentials.workspaceId || credentials.apiUrl) {
92-
this.logger.warn(
93-
'You are currently using credential command line arguments or environment variables as well as a profile. Your profile has overwritten the variables'
94-
)
105+
if (!(token && workspaceId && apiUrl)) {
106+
return null
95107
}
96-
;({ token, workspaceId, apiUrl } = await this.readProfileFromFS(this.argv.profile))
97-
this.logger.log(`Using profile "${this.argv.profile}"`, { prefix: '👤' })
98-
} else {
99-
token = credentials.token ?? (await cache.get('token'))
100-
workspaceId = credentials.workspaceId ?? (await cache.get('workspaceId'))
101-
apiUrl = credentials.apiUrl ?? (await cache.get('apiUrl'))
102-
}
103108

104-
if (!(token && workspaceId && apiUrl)) {
105-
return null
106-
}
109+
if (apiUrl !== consts.defaultBotpressApiUrl) {
110+
this.logger.log(`Using custom url ${apiUrl}`, { prefix: '🔗' })
111+
}
107112

108-
if (apiUrl !== consts.defaultBotpressApiUrl) {
109-
this.logger.log(`Using custom url ${apiUrl}`, { prefix: '🔗' })
113+
return this.api.newClient({ apiUrl, token, workspaceId }, this.logger)
114+
} catch (thrown) {
115+
throw errors.BotpressCLIError.wrap(thrown, 'failed to create authenticated client')
110116
}
111-
112-
return this.api.newClient({ apiUrl, token, workspaceId }, this.logger)
113117
}
114118

115119
protected async readProfileFromFS(profile: string): Promise<ProfileCredentials> {

0 commit comments

Comments
 (0)