Skip to content

Commit a2eb4ad

Browse files
authored
Merge pull request #1999 from contentstack/fix/DX-3205
fix: error messages in auth plugin
2 parents 0bd26ce + 7df69aa commit a2eb4ad

File tree

3 files changed

+65
-30
lines changed
  • packages

3 files changed

+65
-30
lines changed

packages/contentstack-auth/src/utils/auth-handler.ts

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cliux, CLIError, handleAndLogError, log } from '@contentstack/cli-utilities';
1+
import { cliux, CLIError, log, cliErrorHandler } from '@contentstack/cli-utilities';
22
import { User } from '../interfaces';
33
import { askOTPChannel, askOTP } from './interactive';
44

@@ -28,8 +28,15 @@ class AuthHandler {
2828
* TBD: take out the otp implementation from login and create a new method/function to handle otp
2929
*/
3030
async login(email: string, password: string, tfaToken?: string): Promise<User> {
31-
log.debug('Starting login process', { module: 'auth-handler', email, hasPassword: !!password, hasTfaToken: !!tfaToken });
32-
31+
const hasCredentials = !!password;
32+
const hasTfaToken = !!tfaToken;
33+
log.debug('Starting login process', {
34+
module: 'auth-handler',
35+
email,
36+
hasCredentials,
37+
hasTfaToken,
38+
});
39+
3340
return new Promise((resolve, reject) => {
3441
if (email && password) {
3542
const loginPayload: {
@@ -41,22 +48,31 @@ class AuthHandler {
4148
loginPayload.tfa_token = tfaToken;
4249
log.debug('Adding TFA token to login payload', { module: 'auth-handler' });
4350
}
44-
45-
log.debug('Making login API call', { module: 'auth-handler', payload: { email, hasPassword: !!password, hasTfaToken: !!tfaToken } });
46-
51+
52+
const hasCredentials = !!password;
53+
const hasTfaTokenPresent = !!tfaToken;
54+
log.debug('Making login API call', {
55+
module: 'auth-handler',
56+
payload: { email, hasCredentials, hasTfaTokenPresent },
57+
});
58+
4759
this._client
4860
.login(loginPayload)
4961
.then(async (result: any) => {
50-
log.debug('Login API response received', { module: 'auth-handler', hasUser: !!result.user, errorCode: result.error_code });
51-
62+
log.debug('Login API response received', {
63+
module: 'auth-handler',
64+
hasUser: !!result.user,
65+
errorCode: result.error_code,
66+
});
67+
5268
if (result.user) {
5369
log.debug('Login successful, user found', { module: 'auth-handler', userEmail: result.user.email });
5470
resolve(result.user as User);
5571
} else if (result.error_code === 294) {
5672
log.debug('TFA required, requesting OTP channel', { module: 'auth-handler' });
5773
const otpChannel = await askOTPChannel();
5874
log.debug(`OTP channel selected: ${otpChannel}`, { module: 'auth-handler' });
59-
75+
6076
// need to send sms to the mobile
6177
if (otpChannel === 'sms') {
6278
log.debug('Sending SMS OTP request', { module: 'auth-handler' });
@@ -66,22 +82,23 @@ class AuthHandler {
6682
cliux.print('CLI_AUTH_LOGIN_SECURITY_CODE_SEND_SUCCESS');
6783
} catch (error) {
6884
log.debug('SMS OTP request failed', { module: 'auth-handler', error });
69-
handleAndLogError(error, {}, 'Failed to send the security code')
70-
reject(new CLIError({ message: 'Failed to login - failed to send the security code' }));
85+
const err = cliErrorHandler.classifyError(error);
86+
reject(err);
7187
return;
7288
}
7389
}
74-
90+
7591
log.debug('Requesting OTP input from user', { module: 'auth-handler' });
7692
const tfToken = await askOTP();
7793
log.debug('OTP received, retrying login', { module: 'auth-handler' });
78-
94+
7995
try {
8096
resolve(await this.login(email, password, tfToken));
8197
} catch (error) {
8298
log.debug('Login with TFA token failed', { module: 'auth-handler', error });
83-
handleAndLogError(error, {}, 'Failed to login with tfa token')
84-
reject(new CLIError({ message: 'Failed to login with the tf token' }));
99+
const err = cliErrorHandler.classifyError(error);
100+
reject(err);
101+
return;
85102
}
86103
} else {
87104
log.debug('Login failed - no user found', { module: 'auth-handler', result });
@@ -90,11 +107,17 @@ class AuthHandler {
90107
})
91108
.catch((error: any) => {
92109
log.debug('Login API call failed', { module: 'auth-handler', error: error.message || error });
93-
handleAndLogError(error, {}, 'Failed to login with the credentials');
94-
reject(new CLIError({ message: error.errorMessage }));
110+
const err = cliErrorHandler.classifyError(error);
111+
reject(err);
95112
});
96113
} else {
97-
log.debug('Login failed - missing credentials', { module: 'auth-handler', hasEmail: !!email, hasPassword: !!password });
114+
const hasEmail = !!email;
115+
const hasCredentials = !!password;
116+
log.debug('Login failed - missing credentials', {
117+
module: 'auth-handler',
118+
hasEmail,
119+
hasCredentials,
120+
});
98121
reject(new CLIError({ message: 'No credential found to login' }));
99122
}
100123
});
@@ -107,11 +130,11 @@ class AuthHandler {
107130
*/
108131
async logout(authtoken: string): Promise<object> {
109132
log.debug('Starting logout process', { module: 'auth-handler', hasAuthToken: !!authtoken });
110-
133+
111134
return new Promise((resolve, reject) => {
112135
if (authtoken) {
113136
log.debug('Making logout API call', { module: 'auth-handler' });
114-
137+
115138
this._client
116139
.logout(authtoken)
117140
.then(function (response: object) {
@@ -120,8 +143,8 @@ class AuthHandler {
120143
})
121144
.catch((error: Error) => {
122145
log.debug('Logout API call failed', { module: 'auth-handler', error: error.message });
123-
handleAndLogError(error, {}, 'Failed to logout');
124-
return reject(new CLIError({ message: 'Failed to logout - ' + error.message }));
146+
const err = cliErrorHandler.classifyError(error);
147+
reject(err);
125148
});
126149
} else {
127150
log.debug('Logout failed - no auth token provided', { module: 'auth-handler' });
@@ -137,11 +160,11 @@ class AuthHandler {
137160
*/
138161
async validateAuthtoken(authtoken: string): Promise<object> {
139162
log.debug('Starting token validation', { module: 'auth-handler', hasAuthToken: !!authtoken });
140-
163+
141164
return new Promise((resolve, reject) => {
142165
if (authtoken) {
143166
log.debug('Making token validation API call', { module: 'auth-handler' });
144-
167+
145168
this._client
146169
.getUser()
147170
.then((user: object) => {
@@ -150,8 +173,8 @@ class AuthHandler {
150173
})
151174
.catch((error: Error) => {
152175
log.debug('Token validation failed', { module: 'auth-handler', error: error.message });
153-
handleAndLogError(error, {}, 'Failed to validate token');
154-
reject(new CLIError({ message: 'Failed to validate token - ' + error.message }));
176+
const err = cliErrorHandler.classifyError(error);
177+
reject(err);
155178
});
156179
} else {
157180
log.debug('Token validation failed - no auth token provided', { module: 'auth-handler' });

packages/contentstack-config/src/commands/config/set/log.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { Command } from '@contentstack/cli-command';
2-
import { cliux, flags, configHandler, FlagInput, messageHandler } from '@contentstack/cli-utilities';
2+
import { cliux, flags, configHandler, FlagInput, messageHandler, CLIError } from '@contentstack/cli-utilities';
33
import { interactive } from '../../../utils';
4+
import { existsSync, statSync } from 'fs';
5+
import { dirname } from 'path';
46

57
export default class LogSetCommand extends Command {
68
static description = 'Set logging configuration for CLI';
79

810
static flags: FlagInput = {
9-
'level': flags.string({
11+
level: flags.string({
1012
description: 'Set the log level for the CLI.',
1113
options: ['debug', 'info', 'warn', 'error'],
1214
}),
13-
'path': flags.string({
15+
path: flags.string({
1416
description: ' Specify the file path where logs should be saved.',
1517
}),
1618
};
@@ -33,6 +35,16 @@ export default class LogSetCommand extends Command {
3335
logPath = await interactive.askLogPath();
3436
}
3537

38+
if (logPath) {
39+
const logDir = dirname(logPath);
40+
// Check if the directory part of the path exists and is actually a file
41+
if (existsSync(logDir) && statSync(logDir).isFile()) {
42+
throw new CLIError({
43+
message: `The directory path '${logDir}' is a file, not a directory. Please provide a valid directory path for the log file.`,
44+
});
45+
}
46+
}
47+
3648
const currentLoggingConfig = configHandler.get('log') || {};
3749
if (logLevel) currentLoggingConfig.level = logLevel;
3850
if (logPath) currentLoggingConfig.path = logPath;

packages/contentstack-utilities/src/logger/log.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function handleAndLogError(error: unknown, context?: ErrorContext, errorMessage?
2929
// Always log the error
3030
v2Logger.logError({
3131
type: classified.type,
32-
message: errorMessage || classified.message,
32+
message: errorMessage || classified.error?.message || classified.message,
3333
error: classified.error,
3434
context: typeof classified.context === 'string' ? { message: classified.context } : classified.context,
3535
hidden: classified.hidden,

0 commit comments

Comments
 (0)