1616 * limitations under the License.
1717 */
1818
19- import axios from 'axios' ;
19+ import axios , { isAxiosError } from 'axios' ;
2020import yargs from 'yargs/yargs' ;
2121import { hideBin } from 'yargs/helpers' ;
2222import fs from 'fs' ;
2323import util from 'util' ;
2424
2525import { PushQuery } from '@finos/git-proxy/db' ;
2626import { Action } from '@finos/git-proxy/proxy/actions' ;
27+ import { handleAndLogError } from '@finos/git-proxy/utils/errors' ;
2728
2829const GIT_PROXY_COOKIE_FILE = 'git-proxy-cookie' ;
2930// GitProxy UI HOST and PORT (configurable via environment variable)
@@ -64,12 +65,12 @@ async function login(username: string, password: string) {
6465 const user = `"${ response . data . username } " <${ response . data . email } >` ;
6566 const isAdmin = response . data . admin ? ' (admin)' : '' ;
6667 console . log ( `Login ${ user } ${ isAdmin } : OK` ) ;
67- } catch ( error : any ) {
68- if ( error . response ) {
68+ } catch ( error : unknown ) {
69+ if ( isAxiosError ( error ) && error . response ) {
6970 console . error ( `Error: Login '${ username } ': '${ error . response . status } '` ) ;
7071 process . exitCode = 1 ;
7172 } else {
72- console . error ( `Error: Login '${ username } ': ' ${ error . message } '` ) ;
73+ handleAndLogError ( error , `Error: Login '${ username } '` ) ;
7374 process . exitCode = 2 ;
7475 }
7576 }
@@ -182,8 +183,8 @@ async function getGitPushes(filters: Partial<PushQuery>) {
182183 } ) ;
183184
184185 console . log ( util . inspect ( records , false , null , false ) ) ;
185- } catch ( error : any ) {
186- console . error ( ` Error: List: ' ${ error . message } '` ) ;
186+ } catch ( error : unknown ) {
187+ handleAndLogError ( error , ' Error: List' ) ;
187188 process . exitCode = 2 ;
188189 }
189190}
@@ -224,23 +225,21 @@ async function authoriseGitPush(id: string) {
224225 ) ;
225226
226227 console . log ( `Authorise: ID: '${ id } ': OK` ) ;
227- } catch ( error : any ) {
228- // default error
229- let errorMessage = `Error: Authorise: '${ error . message } '` ;
230- process . exitCode = 2 ;
231-
232- if ( error . response ) {
228+ } catch ( error : unknown ) {
229+ if ( isAxiosError ( error ) && error . response ) {
233230 switch ( error . response . status ) {
234231 case 401 :
235- errorMessage = 'Error: Authorise: Authentication required' ;
232+ console . error ( 'Error: Authorise: Authentication required' ) ;
236233 process . exitCode = 3 ;
237234 break ;
238235 case 404 :
239- errorMessage = `Error: Authorise: ID: '${ id } ': Not Found` ;
236+ console . error ( `Error: Authorise: ID: '${ id } ': Not Found` ) ;
240237 process . exitCode = 4 ;
241238 }
239+ } else {
240+ handleAndLogError ( error , `Error: Authorise: '${ id } '` ) ;
241+ process . exitCode = 2 ;
242242 }
243- console . error ( errorMessage ) ;
244243 }
245244}
246245
@@ -271,23 +270,21 @@ async function rejectGitPush(id: string) {
271270 ) ;
272271
273272 console . log ( `Reject: ID: '${ id } ': OK` ) ;
274- } catch ( error : any ) {
275- // default error
276- let errorMessage = `Error: Reject: '${ error . message } '` ;
277- process . exitCode = 2 ;
278-
279- if ( error . response ) {
273+ } catch ( error : unknown ) {
274+ if ( isAxiosError ( error ) && error . response ) {
280275 switch ( error . response . status ) {
281276 case 401 :
282- errorMessage = 'Error: Reject: Authentication required' ;
277+ console . error ( 'Error: Reject: Authentication required' ) ;
283278 process . exitCode = 3 ;
284279 break ;
285280 case 404 :
286- errorMessage = `Error: Reject: ID: '${ id } ': Not Found` ;
281+ console . error ( `Error: Reject: ID: '${ id } ': Not Found` ) ;
287282 process . exitCode = 4 ;
288283 }
284+ } else {
285+ handleAndLogError ( error , `Error: Reject: '${ id } '` ) ;
286+ process . exitCode = 2 ;
289287 }
290- console . error ( errorMessage ) ;
291288 }
292289}
293290
@@ -318,23 +315,21 @@ async function cancelGitPush(id: string) {
318315 ) ;
319316
320317 console . log ( `Cancel: ID: '${ id } ': OK` ) ;
321- } catch ( error : any ) {
322- // default error
323- let errorMessage = `Error: Cancel: '${ error . message } '` ;
324- process . exitCode = 2 ;
325-
326- if ( error . response ) {
318+ } catch ( error : unknown ) {
319+ if ( isAxiosError ( error ) && error . response ) {
327320 switch ( error . response . status ) {
328321 case 401 :
329- errorMessage = 'Error: Cancel: Authentication required' ;
322+ console . error ( 'Error: Cancel: Authentication required' ) ;
330323 process . exitCode = 3 ;
331324 break ;
332325 case 404 :
333- errorMessage = `Error: Cancel: ID: '${ id } ': Not Found` ;
326+ console . error ( `Error: Cancel: ID: '${ id } ': Not Found` ) ;
334327 process . exitCode = 4 ;
335328 }
329+ } else {
330+ handleAndLogError ( error , `Error: Cancel: '${ id } '` ) ;
331+ process . exitCode = 2 ;
336332 }
337- console . error ( errorMessage ) ;
338333 }
339334}
340335
@@ -355,8 +350,8 @@ async function logout() {
355350 headers : { Cookie : cookies } ,
356351 } ,
357352 ) ;
358- } catch ( error : any ) {
359- console . log ( ` Warning: Logout: ' ${ error . message } '` ) ;
353+ } catch ( error : unknown ) {
354+ handleAndLogError ( error , ' Warning: Logout' ) ;
360355 }
361356 }
362357
@@ -379,10 +374,9 @@ async function reloadConfig() {
379374 await axios . post ( `${ baseUrl } /api/v1/admin/reload-config` , { } , { headers : { Cookie : cookies } } ) ;
380375
381376 console . log ( 'Configuration reloaded successfully' ) ;
382- } catch ( error : any ) {
383- const errorMessage = ` Error: Reload config: ' ${ error . message } '` ;
377+ } catch ( error : unknown ) {
378+ handleAndLogError ( error , ' Error: Reload config' ) ;
384379 process . exitCode = 2 ;
385- console . error ( errorMessage ) ;
386380 }
387381}
388382
@@ -425,23 +419,22 @@ async function createUser(
425419 ) ;
426420
427421 console . log ( `User '${ username } ' created successfully` ) ;
428- } catch ( error : any ) {
429- let errorMessage = `Error: Create User: '${ error . message } '` ;
430- process . exitCode = 2 ;
431-
432- if ( error . response ) {
422+ } catch ( error : unknown ) {
423+ if ( isAxiosError ( error ) && error . response ) {
433424 switch ( error . response . status ) {
434425 case 401 :
435- errorMessage = 'Error: Create User: Authentication required' ;
426+ console . error ( 'Error: Create User: Authentication required' ) ;
436427 process . exitCode = 3 ;
437428 break ;
438429 case 400 :
439- errorMessage = `Error: Create User: ${ error . response . data . message } ` ;
430+ console . error ( `Error: Create User: ${ error . response . data . message } ` ) ;
440431 process . exitCode = 4 ;
441432 break ;
442433 }
434+ } else {
435+ handleAndLogError ( error , `Error: Create User: '${ username } '` ) ;
436+ process . exitCode = 2 ;
443437 }
444- console . error ( errorMessage ) ;
445438 }
446439}
447440
0 commit comments