Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve error handling and output #2164

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/bin/vip-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -41,7 +42,19 @@ command( { requiredArgs: 1, format: true } )
error: `App ${ arg[ 0 ] } does not exist`,
} );

console.log( `App ${ chalk.blueBright( arg[ 0 ] ) } does not exist` );
// Get error message, if available.
const apiErrorMsg = parseApiError( err );

let errorMsg = `Unable to locate app ${ chalk.blueBright( arg[ 0 ] ) }: `;

if ( apiErrorMsg ) {
errorMsg += apiErrorMsg;
} else {
// 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 );
return;
}

Expand All @@ -50,7 +63,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;
}

Expand Down
17 changes: 15 additions & 2 deletions src/lib/cli/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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' );
Expand All @@ -247,7 +248,19 @@ args.argv = async function ( argv, cb ) {
error: 'App lookup failed',
} );

exit.withError( `App ${ chalk.blueBright( options.app ) } does not exist` );
// Get error message, if available.
const apiErrorMsg = parseApiError( err );

let errorMsg = `Unable to find app ${ chalk.blueBright( options.app ) }: `;

if ( apiErrorMsg ) {
errorMsg += apiErrorMsg;
} else {
// 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 );
}

if ( ! appLookup?.id ) {
Expand Down
27 changes: 27 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,30 @@ 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?: { message?: string }[];
} ): string | null {
if ( err?.networkError?.message ) {
return err?.networkError?.message;
}

if ( err?.graphQLErrors?.[ 0 ]?.message ) {
return err.graphQLErrors[ 0 ].message;
}

if ( err?.message ) {
return err?.message;
}

return null;
}
Loading