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

feat: show detailed error information in debug mode #1636

Merged
merged 5 commits into from
Jan 9, 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
29 changes: 23 additions & 6 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"@types/ejs": "^3.1.5",
"@types/jest": "^29.5.8",
"@types/js-yaml": "^4.0.9",
"@types/node": "^18.19.5",
"@types/node-fetch": "^2.6.9",
"@types/proxy-from-env": "^1.0.4",
"@types/semver": "^7.5.5",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cli/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function uncaughtError( err ) {
return;
}
if ( err instanceof UserError ) {
exit.withError( err.message );
exit.withError( err );
}

console.log( chalk.red( '✕' ), 'Please contact VIP Support with the following information:' );
Expand Down
10 changes: 8 additions & 2 deletions src/lib/cli/exit.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { red, yellow } from 'chalk';
import debug from 'debug';

import env from '../../lib/env';

export function withError( message: Error | string ): never {
console.error( `${ red( 'Error: ' ) } ${ message.toString().replace( /^Error:\s*/, '' ) }` );
const msg = message instanceof Error ? message.message : message;
console.error( `${ red( 'Error: ' ) } ${ msg.replace( /^Error:\s*/, '' ) }` );

// Debug ouput is printed below error output both for information
// hierarchy and to make it more likely that the user copies it to their
// clipboard when dragging across output.
console.log(
`${ yellow( 'Debug: ' ) } VIP-CLI v${ env.app.version }, Node ${ env.node.version }, ${
env.os.name
} ${ env.os.version }`
} ${ env.os.version } ${ env.os.arch }`
);

if ( debug.names.length > 0 && message instanceof Error ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug.enabled('*') always returns true :-(

console.error( yellow( 'Debug: ' ), message );
}

process.exit( 1 );
}
2 changes: 1 addition & 1 deletion src/lib/cli/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface StepFromServer {
export class ProgressTracker {
hasFailure: boolean;
hasPrinted: boolean;
printInterval: NodeJS.Timer | undefined;
printInterval: NodeJS.Timeout | undefined;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a side-effect of installing @types/node.


// Track the state of each step
stepsFromCaller: Map< string, Step >;
Expand Down
13 changes: 9 additions & 4 deletions src/lib/client-file-uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export const getFileHash = async (
await pipeline( src, dst );
return dst.digest().toString( 'hex' );
} catch ( err ) {
throw new Error( `could not generate file hash: ${ ( err as Error ).message }` );
throw new Error( `Could not generate file hash: ${ ( err as Error ).message }`, {
cause: err,
} );
} finally {
src.close();
}
Expand All @@ -104,7 +106,7 @@ const gzipFile = async ( uncompressedFileName: string, compressedFileName: strin
createWriteStream( compressedFileName )
);
} catch ( err ) {
throw new Error( `could not compress file: ${ ( err as Error ).message }` );
throw new Error( `Could not compress file: ${ ( err as Error ).message }`, { cause: err } );
}
};

Expand Down Expand Up @@ -173,7 +175,8 @@ export async function uploadImportSqlFileToS3( {
tmpDir = await getWorkingTempDir();
} catch ( err ) {
throw new Error(
`Unable to create temporary working directory: ${ ( err as Error ).message }`
`Unable to create temporary working directory: ${ ( err as Error ).message }`,
{ cause: err }
);
}

Expand Down Expand Up @@ -305,7 +308,9 @@ async function uploadUsingPutObject( {
try {
parsedResponse = ( await parser.parseStringPromise( result ) ) as UploadErrorResponse;
} catch ( err ) {
throw new Error( `Invalid response from cloud service. ${ ( err as Error ).message }` );
throw new Error( `Invalid response from cloud service. ${ ( err as Error ).message }`, {
cause: err,
} );
}

const { Code, Message } = parsedResponse.Error;
Expand Down
4 changes: 3 additions & 1 deletion src/lib/dev-environment/dev-environment-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ const verifyDNSResolution = async ( slug: string ): Promise< void > => {
address = await lookup( testDomain, 4 );
debug( `Got DNS response ${ address.address }` );
} catch ( error ) {
throw new UserError( `DNS resolution for ${ testDomain } failed. ${ advice }` );
throw new UserError( `DNS resolution for ${ testDomain } failed. ${ advice }`, {
cause: error,
} );
}

if ( address.address !== expectedIP ) {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/dev-environment/dev-environment-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ export function readEnvironmentData( slug: string ): InstanceData {
} catch ( error: unknown ) {
const err = error as Error;
throw new UserError(
`There was an error reading file "${ instanceDataTargetPath }": ${ err.message }.`
`There was an error reading file "${ instanceDataTargetPath }": ${ err.message }.`,
{ cause: error }
);
}

Expand All @@ -417,7 +418,8 @@ export function readEnvironmentData( slug: string ): InstanceData {
} catch ( error: unknown ) {
const err = error as Error;
throw new UserError(
`There was an error parsing file "${ instanceDataTargetPath }": ${ err.message }. You may need to recreate the environment.`
`There was an error parsing file "${ instanceDataTargetPath }": ${ err.message }. You may need to recreate the environment.`,
{ cause: error }
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/lib/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { platform, release } from 'node:os';
import { arch, platform, release } from 'node:os';

import pkg from '../../package.json';

Expand All @@ -10,6 +10,7 @@ interface AppInfo {
interface OSInfo {
name: string;
version: string;
arch: string;
}

interface NodeInfo {
Expand All @@ -31,6 +32,7 @@ const app: AppInfo = {
const os: OSInfo = {
name: platform(),
version: release(),
arch: arch(),
};

const node: NodeInfo = {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/media-import/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type MediaImportStatus = Pick<
export class MediaImportProgressTracker {
hasFailure: boolean;
hasPrinted: boolean;
printInterval: NodeJS.Timer | undefined;
printInterval: NodeJS.Timeout | undefined;
status: MediaImportStatus;

// Spinnerz go brrrr
Expand Down
4 changes: 2 additions & 2 deletions src/lib/user-error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default class UserError extends Error {
constructor( message: string ) {
super( message );
constructor( message: string, options?: ErrorOptions ) {
super( message, options );
Comment on lines +2 to +3
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.name = 'UserError';
}
}
2 changes: 1 addition & 1 deletion src/lib/validations/line-by-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function fileLineValidations(
} );

readInterface.on( 'error', ( err: Error ) => {
throw new Error( ` Error validating input file: ${ err.toString() }` );
throw new Error( `Error validating input file: ${ err.toString() }`, { cause: err } );
} );

// Block until the processing completes
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"lib": [ "es2021" ],
"lib": [ "ES2022" ],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ErrorOptions is an ES2022 feature. However, because the minimum supported version is Node 18, ES2022 is fine; see https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping#node-18

"module": "node16",
"strict": true,
"esModuleInterop": true,
Expand All @@ -9,7 +9,7 @@
"moduleResolution": "node16",

// Target latest version of ECMAScript.
"target": "esnext",
"target": "ES2022",
// Don't parse types from JS as TS doesn't play well with Flow-ish JS.
"allowJs": false,
// Don't emit; allow Babel to transform files.
Expand Down
Loading