From 2412ea0107f188411f818f6909466dacca0ee41b Mon Sep 17 00:00:00 2001 From: "Gudmundur D. Haraldsson" Date: Mon, 16 Dec 2024 17:29:11 +0000 Subject: [PATCH 1/5] Improve error handling and output --- src/bin/vip-app.js | 15 +++++++++++++-- src/lib/cli/command.js | 15 +++++++++++++-- src/lib/utils.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/bin/vip-app.js b/src/bin/vip-app.js index 65d5e7e49..291464d34 100755 --- a/src/bin/vip-app.js +++ b/src/bin/vip-app.js @@ -5,6 +5,7 @@ import chalk from 'chalk'; import app from '../lib/api/app'; import command, { getEnvIdentifier } from '../lib/cli/command'; import { trackEvent } from '../lib/tracker'; +import { parseApiError } from '../lib/utils'; command( { requiredArgs: 1, format: true } ) .example( @@ -41,7 +42,17 @@ command( { requiredArgs: 1, format: true } ) error: `App ${ arg[ 0 ] } does not exist`, } ); - console.log( `App ${ chalk.blueBright( arg[ 0 ] ) } does not exist` ); + const apiErrorMsg = parseApiError( err ); + + let errorMsg = `Unable to locate app ${ chalk.blueBright( arg[ 0 ] ) }`; + + if ( apiErrorMsg ) { + errorMsg += ': ' + apiErrorMsg; + } else { + errorMsg += ': Invalid application or connection issue?'; + } + + console.log( errorMsg ); return; } @@ -50,7 +61,7 @@ command( { requiredArgs: 1, format: true } ) error: `App ${ arg[ 0 ] } does not exist`, } ); - console.log( `App ${ chalk.blueBright( arg[ 0 ] ) } does not exist` ); + console.log( `App ${ chalk.blueBright( arg[ 0 ] ) } was not found` ); return; } diff --git a/src/lib/cli/command.js b/src/lib/cli/command.js index 875911ab1..d07770950 100644 --- a/src/lib/cli/command.js +++ b/src/lib/cli/command.js @@ -12,6 +12,7 @@ import pkg from '../../../package.json'; import API from '../../lib/api'; import app from '../../lib/api/app'; import { trackEvent } from '../../lib/tracker'; +import { parseApiError } from '../../lib/utils'; import UserError from '../user-error'; function uncaughtError( err ) { @@ -232,7 +233,7 @@ args.argv = async function ( argv, cb ) { error: 'Invalid app selected', } ); - exit.withError( `App ${ chalk.blueBright( appSelection.app.name ) } does not exist` ); + exit.withError( `App ${ chalk.blueBright( appSelection.app.name ) } could not be located` ); } await trackEvent( 'command_appcontext_list_select_success' ); @@ -247,7 +248,17 @@ args.argv = async function ( argv, cb ) { error: 'App lookup failed', } ); - exit.withError( `App ${ chalk.blueBright( options.app ) } does not exist` ); + const apiErrorMsg = parseApiError( err ); + + let errorMsg = `Unable to find app ${ chalk.blueBright( options.app ) }`; + + if ( apiErrorMsg ) { + errorMsg += ': ' + apiErrorMsg; + } else { + errorMsg += ': Invalid application or connection issue?'; + } + + exit.withError( errorMsg ); } if ( ! appLookup?.id ) { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 66c5661c2..d199ee3a0 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -75,3 +75,31 @@ export function getAbsolutePath( filePath: string ): string { return filePath; } + +/** + * Parse error object and return probable error. + * + * @param {Error} Error object + * + * @return {string|null} Error string when error was found, otherwise null. + */ + +export function parseApiError( err: { + networkError?: { message?: string }; + message?: string; + graphQLErrors: unknown[]; +} ): string | null { + if ( err?.networkError?.message ) { + return err?.networkError?.message; + } + + if ( err?.graphQLErrors && err?.graphQLErrors?.length > 0 && err?.graphQLErrors[ 0 ]?.message ) { + return err?.graphQLErrors[ 0 ]?.message; + } + + if ( err?.message ) { + return err?.message; + } + + return null; +} From c21ace4451e8605dd048712da275d8908196c318 Mon Sep 17 00:00:00 2001 From: "Gudmundur D. Haraldsson" Date: Mon, 16 Dec 2024 17:44:34 +0000 Subject: [PATCH 2/5] Improve typing --- src/lib/utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index d199ee3a0..79602fbaf 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -87,14 +87,14 @@ export function getAbsolutePath( filePath: string ): string { export function parseApiError( err: { networkError?: { message?: string }; message?: string; - graphQLErrors: unknown[]; + graphQLErrors?: { message?: string }[]; } ): string | null { if ( err?.networkError?.message ) { return err?.networkError?.message; } if ( err?.graphQLErrors && err?.graphQLErrors?.length > 0 && err?.graphQLErrors[ 0 ]?.message ) { - return err?.graphQLErrors[ 0 ]?.message; + return err?.graphQLErrors[ 0 ]?.message; } if ( err?.message ) { From 44a8c07c8e3a37c532f2eaade8bbb91d7a165f47 Mon Sep 17 00:00:00 2001 From: "Gudmundur D. Haraldsson" Date: Mon, 16 Dec 2024 17:59:36 +0000 Subject: [PATCH 3/5] Remove linebreak not needed --- src/lib/utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 79602fbaf..9e77fc845 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -83,7 +83,6 @@ export function getAbsolutePath( filePath: string ): string { * * @return {string|null} Error string when error was found, otherwise null. */ - export function parseApiError( err: { networkError?: { message?: string }; message?: string; From c0dc35252faee0dfc5ee298ec8ab550352b03052 Mon Sep 17 00:00:00 2001 From: "Gudmundur D. Haraldsson" Date: Tue, 17 Dec 2024 13:30:18 +0000 Subject: [PATCH 4/5] Update error message, include stack trace if no reliable error message is found --- src/bin/vip-app.js | 8 +++++--- src/lib/cli/command.js | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/bin/vip-app.js b/src/bin/vip-app.js index 291464d34..323f2ed74 100755 --- a/src/bin/vip-app.js +++ b/src/bin/vip-app.js @@ -42,14 +42,16 @@ command( { requiredArgs: 1, format: true } ) error: `App ${ arg[ 0 ] } does not exist`, } ); + // Get error message, if available. const apiErrorMsg = parseApiError( err ); - let errorMsg = `Unable to locate app ${ chalk.blueBright( arg[ 0 ] ) }`; + let errorMsg = `Unable to locate app ${ chalk.blueBright( arg[ 0 ] ) }: `; if ( apiErrorMsg ) { - errorMsg += ': ' + apiErrorMsg; + errorMsg += apiErrorMsg; } else { - errorMsg += ': Invalid application or connection issue?'; + // No error message, so let's include stack trace for debugging. + errorMsg += 'Unknown error. Please contact VIP support if this persists.\n' + err.stack; } console.log( errorMsg ); diff --git a/src/lib/cli/command.js b/src/lib/cli/command.js index d07770950..8a49341a5 100644 --- a/src/lib/cli/command.js +++ b/src/lib/cli/command.js @@ -248,14 +248,16 @@ args.argv = async function ( argv, cb ) { error: 'App lookup failed', } ); + // Get error message, if available. const apiErrorMsg = parseApiError( err ); - let errorMsg = `Unable to find app ${ chalk.blueBright( options.app ) }`; + let errorMsg = `Unable to find app ${ chalk.blueBright( options.app ) }: `; if ( apiErrorMsg ) { - errorMsg += ': ' + apiErrorMsg; + errorMsg += apiErrorMsg; } else { - errorMsg += ': Invalid application or connection issue?'; + // Should happen rarely, if ever. Let's include stack trace for debugging. + errorMsg += 'Unknown error. If this persists, please contact VIP support.\n' + err.stack; } exit.withError( errorMsg ); From f9119a59f56546f01ffd40f3efe9f7ec79fd9fbb Mon Sep 17 00:00:00 2001 From: "Gudmundur D. Haraldsson" Date: Tue, 17 Dec 2024 13:30:27 +0000 Subject: [PATCH 5/5] Update src/lib/utils.ts Co-authored-by: Roberson Gomes --- src/lib/utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 9e77fc845..d2f7a32ee 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -92,8 +92,8 @@ export function parseApiError( err: { return err?.networkError?.message; } - if ( err?.graphQLErrors && err?.graphQLErrors?.length > 0 && err?.graphQLErrors[ 0 ]?.message ) { - return err?.graphQLErrors[ 0 ]?.message; + if ( err?.graphQLErrors?.[ 0 ]?.message ) { + return err.graphQLErrors[ 0 ].message; } if ( err?.message ) {