1- import { cliux , CLIError , log , cliErrorHandler } from '@contentstack/cli-utilities' ;
1+ import { cliux , log , handleAndLogError , messageHandler } from '@contentstack/cli-utilities' ;
22import { User } from '../interfaces' ;
33import { askOTPChannel , askOTP } from './interactive' ;
44
@@ -48,19 +48,18 @@ class AuthHandler {
4848 try {
4949 await this . requestSMSOTP ( loginPayload ) ;
5050 } catch ( error ) {
51- log . debug ( 'SMS OTP request failed' , { module : 'auth-handler' , error } ) ;
52- throw new CLIError ( 'Failed to send SMS OTP. Please try again or use a different 2FA method.' ) ;
51+ log . error ( 'SMS OTP request failed' , { module : 'auth-handler' , error } ) ;
52+ cliux . print ( 'CLI_AUTH_SMS_OTP_FAILED' , { color : 'yellow' } ) ;
53+ handleAndLogError ( error , { module : 'auth-handler' } ) ;
5354 }
5455 }
5556
5657 log . debug ( 'Requesting OTP input' , { module : 'auth-handler' , channel : otpChannel } ) ;
5758 return await askOTP ( ) ;
5859 } catch ( error ) {
59- log . debug ( '2FA flow failed' , { module : 'auth-handler' , error } ) ;
60- if ( error instanceof CLIError ) {
61- throw error ;
62- }
63- throw new CLIError ( 'Failed to complete 2FA authentication. Please try again.' ) ;
60+ log . error ( '2FA flow failed' , { module : 'auth-handler' , error } ) ;
61+ cliux . print ( 'CLI_AUTH_2FA_FAILED' , { color : 'yellow' } ) ;
62+ handleAndLogError ( error , { module : 'auth-handler' } ) ;
6463 }
6564 }
6665
@@ -76,9 +75,9 @@ class AuthHandler {
7675 log . debug ( 'SMS OTP request successful' , { module : 'auth-handler' } ) ;
7776 cliux . print ( 'CLI_AUTH_LOGIN_SECURITY_CODE_SEND_SUCCESS' ) ;
7877 } catch ( error ) {
79- log . debug ( 'SMS OTP request failed' , { module : 'auth-handler' , error } ) ;
80- const err = cliErrorHandler . classifyError ( error ) ;
81- throw new CLIError ( err ) ;
78+ log . error ( 'SMS OTP request failed' , { module : 'auth-handler' , error } ) ;
79+ handleAndLogError ( error , { module : 'auth-handler' } ) ;
80+ throw error ;
8281 }
8382 }
8483
@@ -130,19 +129,23 @@ class AuthHandler {
130129 resolve ( await this . login ( email , password , tfToken ) ) ;
131130 } catch ( error ) {
132131 log . debug ( 'Login with TFA token failed' , { module : 'auth-handler' , error } ) ;
133- const err = cliErrorHandler . classifyError ( error ) ;
134- reject ( new CLIError ( err ) ) ;
132+ handleAndLogError ( error , { module : 'auth-handler' } ) ;
133+ log . debug ( '2FA authentication failed' , { module : 'auth-handler' , error } ) ;
134+ cliux . print ( 'CLI_AUTH_2FA_FAILED' , { color : 'yellow' } ) ;
135+ handleAndLogError ( error , { module : 'auth-handler' } ) ;
135136 return ;
136137 }
137138 } else {
138139 log . debug ( 'Login failed - no user found' , { module : 'auth-handler' , result } ) ;
139- reject ( new CLIError ( { message : 'No user found with the credentials' } ) ) ;
140+ log . debug ( 'Login failed - no user found' , { module : 'auth-handler' , result } ) ;
141+ cliux . print ( 'CLI_AUTH_LOGIN_NO_USER' , { color : 'yellow' } ) ;
142+ handleAndLogError ( new Error ( messageHandler . parse ( 'CLI_AUTH_LOGIN_NO_USER' ) ) , { module : 'auth-handler' } ) ;
140143 }
141144 } )
142145 . catch ( ( error : any ) => {
143146 log . debug ( 'Login API call failed' , { module : 'auth-handler' , error : error . message || error } ) ;
144- const err = cliErrorHandler . classifyError ( error ) ;
145- reject ( new CLIError ( err ) ) ;
147+ cliux . print ( 'CLI_AUTH_LOGIN_FAILED' , { color : 'yellow' } ) ;
148+ handleAndLogError ( error , { module : 'auth-handler' } ) ;
146149 } ) ;
147150 } else {
148151 const hasEmail = ! ! email ;
@@ -152,7 +155,9 @@ class AuthHandler {
152155 hasEmail,
153156 hasCredentials,
154157 } ) ;
155- reject ( new CLIError ( 'No credential found to login' ) ) ;
158+ log . debug ( 'Login failed - missing credentials' , { module : 'auth-handler' , hasEmail, hasCredentials } ) ;
159+ cliux . print ( 'CLI_AUTH_LOGIN_NO_CREDENTIALS' , { color : 'yellow' } ) ;
160+ handleAndLogError ( new Error ( messageHandler . parse ( 'CLI_AUTH_LOGIN_NO_CREDENTIALS' ) ) , { module : 'auth-handler' } ) ;
156161 }
157162 } ) ;
158163 }
@@ -177,12 +182,13 @@ class AuthHandler {
177182 } )
178183 . catch ( ( error : Error ) => {
179184 log . debug ( 'Logout API call failed' , { module : 'auth-handler' , error : error . message } ) ;
180- const err = cliErrorHandler . classifyError ( error ) ;
181- reject ( new CLIError ( err ) ) ;
185+ cliux . print ( 'CLI_AUTH_LOGOUT_FAILED' , { color : 'yellow' } ) ;
186+ handleAndLogError ( error , { module : 'auth-handler' } ) ;
182187 } ) ;
183188 } else {
184189 log . debug ( 'Logout failed - no auth token provided' , { module : 'auth-handler' } ) ;
185- reject ( new CLIError ( 'No auth token found to logout' ) ) ;
190+ cliux . print ( 'CLI_AUTH_LOGOUT_NO_TOKEN' , { color : 'yellow' } ) ;
191+ handleAndLogError ( new Error ( messageHandler . parse ( 'CLI_AUTH_LOGOUT_NO_TOKEN' ) ) , { module : 'auth-handler' } ) ;
186192 }
187193 } ) ;
188194 }
@@ -207,12 +213,15 @@ class AuthHandler {
207213 } )
208214 . catch ( ( error : Error ) => {
209215 log . debug ( 'Token validation failed' , { module : 'auth-handler' , error : error . message } ) ;
210- const err = cliErrorHandler . classifyError ( error ) ;
211- reject ( new CLIError ( err ) ) ;
216+ cliux . print ( 'CLI_AUTH_TOKEN_VALIDATION_FAILED' , { color : 'yellow' } ) ;
217+ handleAndLogError ( error , { module : 'auth-handler' } ) ;
212218 } ) ;
213219 } else {
214220 log . debug ( 'Token validation failed - no auth token provided' , { module : 'auth-handler' } ) ;
215- reject ( new CLIError ( 'No auth token found to validate' ) ) ;
221+ cliux . print ( 'CLI_AUTH_TOKEN_VALIDATION_NO_TOKEN' , { color : 'yellow' } ) ;
222+ handleAndLogError ( new Error ( messageHandler . parse ( 'CLI_AUTH_TOKEN_VALIDATION_NO_TOKEN' ) ) , {
223+ module : 'auth-handler' ,
224+ } ) ;
216225 }
217226 } ) ;
218227 }
0 commit comments