Skip to content

Commit

Permalink
top-level error handling (#865)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfoyle authored Jan 4, 2024
1 parent c885ff2 commit 74846bd
Show file tree
Hide file tree
Showing 22 changed files with 304 additions and 208 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-seals-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-cli': patch
---

gracefully handle errors that occur during a process event handler
4 changes: 3 additions & 1 deletion packages/cli/src/amplify.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env node

import { hideBin } from 'yargs/helpers';
import * as process from 'process';
import { createMainParser } from './main_parser_factory.js';
import { attachUnhandledExceptionListeners } from './error_handler.js';

attachUnhandledExceptionListeners();

const parser = createMainParser();

Expand Down
70 changes: 0 additions & 70 deletions packages/cli/src/command_failure_handler.test.ts

This file was deleted.

33 changes: 0 additions & 33 deletions packages/cli/src/command_failure_handler.ts

This file was deleted.

10 changes: 1 addition & 9 deletions packages/cli/src/commands/configure/configure_command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Argv, CommandModule } from 'yargs';
import { handleCommandFailure } from '../../command_failure_handler.js';

/**
* Root command to configure Amplify.
Expand Down Expand Up @@ -35,13 +34,6 @@ export class ConfigureCommand implements CommandModule<object> {
* @inheritDoc
*/
builder = (yargs: Argv): Argv => {
return yargs
.version(false)
.command(this.configureSubCommands)
.help()
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
});
return yargs.version(false).command(this.configureSubCommands).help();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
USAGE_DATA_TRACKING_ENABLED,
} from '@aws-amplify/platform-core';
import { Argv, CommandModule } from 'yargs';
import { handleCommandFailure } from '../../../command_failure_handler.js';
/**
* Command to configure AWS Amplify profile.
*/
Expand Down Expand Up @@ -54,10 +53,6 @@ export class ConfigureTelemetryCommand implements CommandModule<object> {
})
.demandCommand()
.strictCommands()
.recommendCommands()
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
});
.recommendCommands();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ClientConfigFormat } from '@aws-amplify/client-config';
import { BackendIdentifierResolver } from '../../../backend-identifier/backend_identifier_resolver.js';
import { ClientConfigGeneratorAdapter } from '../../../client-config/client_config_generator_adapter.js';
import { ArgumentsKebabCase } from '../../../kebab_case.js';
import { handleCommandFailure } from '../../../command_failure_handler.js';

export type GenerateConfigCommandOptions =
ArgumentsKebabCase<GenerateConfigCommandOptionsCamelCase>;
Expand Down Expand Up @@ -100,10 +99,6 @@ export class GenerateConfigCommand
'A path to directory where config is written. If not provided defaults to current process working directory.',
type: 'string',
array: false,
})
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { BackendIdentifierResolver } from '../../../backend-identifier/backend_i
import { DEFAULT_UI_PATH } from '../../../form-generation/default_form_generation_output_paths.js';
import { FormGenerationHandler } from '../../../form-generation/form_generation_handler.js';
import { ArgumentsKebabCase } from '../../../kebab_case.js';
import { handleCommandFailure } from '../../../command_failure_handler.js';

export type GenerateFormsCommandOptions =
ArgumentsKebabCase<GenerateFormsCommandOptionsCamelCase>;
Expand Down Expand Up @@ -128,10 +127,6 @@ export class GenerateFormsCommand
type: 'string',
array: true,
group: 'Form Generation',
})
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
});
};
}
5 changes: 0 additions & 5 deletions packages/cli/src/commands/generate/generate_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Argv, CommandModule } from 'yargs';
import { GenerateConfigCommand } from './config/generate_config_command.js';
import { GenerateFormsCommand } from './forms/generate_forms_command.js';
import { GenerateGraphqlClientCodeCommand } from './graphql-client-code/generate_graphql_client_code_command.js';
import { handleCommandFailure } from '../../command_failure_handler.js';
import { CommandMiddleware } from '../../command_middleware.js';

/**
Expand Down Expand Up @@ -61,10 +60,6 @@ export class GenerateCommand implements CommandModule {
array: false,
})
.middleware([this.commandMiddleware.ensureAwsCredentialAndRegion])
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
})
);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
GenerateModelsOptions,
} from '@aws-amplify/model-generator';
import { ArgumentsKebabCase } from '../../../kebab_case.js';
import { handleCommandFailure } from '../../../command_failure_handler.js';

type GenerateOptions =
| GenerateGraphqlCodegenOptions
Expand Down Expand Up @@ -321,10 +320,6 @@ export class GenerateGraphqlClientCodeCommand
array: false,
hidden: true,
})
.showHidden('all')
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
});
.showHidden('all');
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Argv, CommandModule } from 'yargs';
import { BackendDeployer } from '@aws-amplify/backend-deployer';
import { ClientConfigGeneratorAdapter } from '../../client-config/client_config_generator_adapter.js';
import { ArgumentsKebabCase } from '../../kebab_case.js';
import { handleCommandFailure } from '../../command_failure_handler.js';
import { BackendIdentifier } from '@aws-amplify/plugin-types';

export type PipelineDeployCommandOptions =
Expand Down Expand Up @@ -88,10 +87,6 @@ export class PipelineDeployCommand
'A path to directory where config is written. If not provided defaults to current process working directory.',
type: 'string',
array: false,
})
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
});
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ArgumentsCamelCase, Argv, CommandModule } from 'yargs';
import { SandboxSingletonFactory } from '@aws-amplify/sandbox';
import { AmplifyPrompter } from '@aws-amplify/cli-core';
import { handleCommandFailure } from '../../../command_failure_handler.js';

/**
* Command that deletes the sandbox environment.
Expand Down Expand Up @@ -76,10 +75,6 @@ export class SandboxDeleteCommand
}
}
return true;
})
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
});
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Argv, CommandModule } from 'yargs';
import { handleCommandFailure } from '../../../command_failure_handler.js';

/**
* Root command to manage sandbox secret.
Expand Down Expand Up @@ -35,12 +34,6 @@ export class SandboxSecretCommand implements CommandModule<object> {
* @inheritDoc
*/
builder = (yargs: Argv): Argv => {
return yargs
.command(this.secretSubCommands)
.help()
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
});
return yargs.command(this.secretSubCommands).help();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { SecretClient } from '@aws-amplify/backend-secret';
import { SandboxBackendIdResolver } from '../sandbox_id_resolver.js';
import { Printer } from '@aws-amplify/cli-core';
import { ArgumentsKebabCase } from '../../../kebab_case.js';
import { handleCommandFailure } from '../../../command_failure_handler.js';

/**
* Command to get sandbox secret.
Expand Down Expand Up @@ -53,11 +52,7 @@ export class SandboxSecretGetCommand
type: 'string',
demandOption: true,
})
.help()
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
});
.help();
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Argv, CommandModule } from 'yargs';
import { SecretClient } from '@aws-amplify/backend-secret';
import { SandboxBackendIdResolver } from '../sandbox_id_resolver.js';
import { ArgumentsKebabCase } from '../../../kebab_case.js';
import { handleCommandFailure } from '../../../command_failure_handler.js';

/**
* Command to remove sandbox secret.
Expand Down Expand Up @@ -46,16 +45,11 @@ export class SandboxSecretRemoveCommand
* @inheritDoc
*/
builder = (yargs: Argv): Argv<SecretRemoveCommandOptions> => {
return yargs
.positional('secret-name', {
describe: 'Name of the secret to remove',
type: 'string',
demandOption: true,
})
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
});
return yargs.positional('secret-name', {
describe: 'Name of the secret to remove',
type: 'string',
demandOption: true,
});
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { SandboxBackendIdResolver } from '../sandbox_id_resolver.js';
import { AmplifyPrompter } from '@aws-amplify/cli-core';

import { ArgumentsKebabCase } from '../../../kebab_case.js';
import { handleCommandFailure } from '../../../command_failure_handler.js';

/**
* Command to set sandbox secret.
Expand Down Expand Up @@ -49,16 +48,11 @@ export class SandboxSecretSetCommand
* @inheritDoc
*/
builder = (yargs: Argv): Argv<SecretSetCommandOptions> => {
return yargs
.positional('secret-name', {
describe: 'Name of the secret to set',
type: 'string',
demandOption: true,
})
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
});
return yargs.positional('secret-name', {
describe: 'Name of the secret to set',
type: 'string',
demandOption: true,
});
};
}

Expand Down
5 changes: 0 additions & 5 deletions packages/cli/src/commands/sandbox/sandbox_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
getClientConfigPath,
} from '@aws-amplify/client-config';
import { ArgumentsKebabCase } from '../../kebab_case.js';
import { handleCommandFailure } from '../../command_failure_handler.js';
import { ClientConfigLifecycleHandler } from '../../client-config/client_config_lifecycle_handler.js';
import { ClientConfigGeneratorAdapter } from '../../client-config/client_config_generator_adapter.js';
import { CommandMiddleware } from '../../command_middleware.js';
Expand Down Expand Up @@ -180,10 +179,6 @@ export class SandboxCommand
return true;
})
.middleware([this.commandMiddleware.ensureAwsCredentialAndRegion])
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
})
);
};

Expand Down
Loading

0 comments on commit 74846bd

Please sign in to comment.