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

Prefer amplify errors in generators #2035

Merged
merged 1 commit into from
Sep 20, 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
7 changes: 7 additions & 0 deletions .changeset/forty-squids-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@aws-amplify/schema-generator': patch
'@aws-amplify/model-generator': patch
'@aws-amplify/form-generator': patch
---

Prefer amplify errors in generators
6 changes: 3 additions & 3 deletions packages/eslint-rules/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export const configs = {
'packages/backend-auth/src/**',
'packages/backend-deployer/src/**',
'packages/create-amplify/src/**',
'packages/form-generator/src/**',
'packages/model-generator/src/**',
'packages/schema-generator/src/**',
],
excludedFiles: ['**/*.test.ts'],
rules: {
Expand All @@ -39,9 +42,6 @@ export const configs = {
'packages/ai-constructs/src/**',
'packages/backend-output-storage/src/**',
'packages/deployed-backend-client/src/**',
'packages/form-generator/src/**',
'packages/model-generator/src/**',
'packages/schema-generator/src/**',
],
rules: {
'amplify-backend-rules/no-amplify-errors': 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,13 @@ export class LocalGraphqlFormGenerator implements GraphqlFormGenerator {
([key]) => key.toLowerCase() === model.toLowerCase()
);
if (!entry) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error(`Could not find specified model ${model}`);
}
prev.push(entry);
return prev;
}
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error(`Could not find specified model ${model}`);
},
[]
Expand Down
1 change: 1 addition & 0 deletions packages/form-generator/src/s3_string_object_fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class S3StringObjectFetcher {
);
const schema = await getSchemaCommandResult.Body?.transformToString();
if (!schema) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('Error on parsing output schema');
}
return schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ export const createGraphqlDocumentGenerator = ({
awsClientProvider,
}: GraphqlDocumentGeneratorFactoryParams): GraphqlDocumentGenerator => {
if (!backendIdentifier) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('`backendIdentifier` must be defined');
}
if (!awsClientProvider) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('`awsClientProvider` must be defined');
}

Expand All @@ -44,6 +46,7 @@ export const createGraphqlDocumentGenerator = ({
);
const apiId = output[graphqlOutputKey]?.payload.awsAppsyncApiId;
if (!apiId) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error(`Unable to determine AppSync API ID.`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ const createGraphqlModelsGeneratorFromBackendIdentifier = ({
awsClientProvider,
}: GraphqlModelsFromBackendIdentifierParams): GraphqlModelsGenerator => {
if (!backendIdentifier) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('`backendIdentifier` must be defined');
}
if (!awsClientProvider) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('`awsClientProvider` must be defined');
}

Expand All @@ -90,9 +92,11 @@ export const createGraphqlModelsFromS3UriGenerator = ({
awsClientProvider,
}: GraphqlModelsFromS3UriGeneratorFactoryParams): GraphqlModelsGenerator => {
if (!modelSchemaS3Uri) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('`modelSchemaS3Uri` must be defined');
}
if (!awsClientProvider) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('`awsClientProvider` must be defined');
}
return new StackMetadataGraphqlModelsGenerator(
Expand All @@ -118,6 +122,7 @@ const getModelSchema = async (
const modelSchemaS3Uri =
output[graphqlOutputKey]?.payload.amplifyApiModelSchemaS3Uri;
if (!modelSchemaS3Uri) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error(`Cannot find model schema at amplifyApiModelSchemaS3Uri`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ export const createGraphqlTypesGenerator = ({
awsClientProvider,
}: GraphqlTypesGeneratorFactoryParams): GraphqlTypesGenerator => {
if (!backendIdentifier) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('`backendIdentifier` must be defined');
}
if (!awsClientProvider) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('`awsClientProvider` must be defined');
}

Expand All @@ -44,6 +46,7 @@ export const createGraphqlTypesGenerator = ({
);
const apiId = output[graphqlOutputKey]?.payload.awsAppsyncApiId;
if (!apiId) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error(`Unable to determine AppSync API ID.`);
}

Expand Down
1 change: 1 addition & 0 deletions packages/model-generator/src/generate_api_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export class ApiCodeGenerator {
return this.generateIntrospectionApiCode();
}
default:
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error(
`${
(props as GenerateApiCodeProps).format as string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const getBackendOutputWithErrorHandling = async (
error instanceof BackendOutputClientError &&
error.code === BackendOutputClientErrorType.DEPLOYMENT_IN_PROGRESS
) {
// eslint-disable-next-line amplify-backend-rules/no-amplify-errors
throw new AmplifyUserError(
'DeploymentInProgressError',
{
Expand All @@ -34,7 +33,6 @@ export const getBackendOutputWithErrorHandling = async (
error instanceof BackendOutputClientError &&
error.code === BackendOutputClientErrorType.NO_STACK_FOUND
) {
// eslint-disable-next-line amplify-backend-rules/no-amplify-errors
throw new AmplifyUserError(
'StackDoesNotExistError',
{
Expand All @@ -49,7 +47,6 @@ export const getBackendOutputWithErrorHandling = async (
error instanceof BackendOutputClientError &&
error.code === BackendOutputClientErrorType.CREDENTIALS_ERROR
) {
// eslint-disable-next-line amplify-backend-rules/no-amplify-errors
throw new AmplifyUserError(
'CredentialsError',
{
Expand All @@ -64,7 +61,6 @@ export const getBackendOutputWithErrorHandling = async (
error instanceof BackendOutputClientError &&
error.code === BackendOutputClientErrorType.ACCESS_DENIED
) {
// eslint-disable-next-line amplify-backend-rules/no-amplify-errors
throw new AmplifyUserError(
'AccessDeniedError',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class AppSyncGraphqlDocumentGenerator
const schema = await this.fetchSchema();

if (!schema) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('Invalid schema');
}

Expand Down
1 change: 1 addition & 0 deletions packages/model-generator/src/graphql_models_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class StackMetadataGraphqlModelsGenerator
const schema = await this.fetchSchema();

if (!schema) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('Invalid schema');
}

Expand Down
1 change: 1 addition & 0 deletions packages/model-generator/src/graphql_types_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class AppSyncGraphqlTypesGenerator implements GraphqlTypesGenerator {
const schema = await this.fetchSchema();

if (!schema) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('Invalid schema');
}

Expand Down
1 change: 1 addition & 0 deletions packages/model-generator/src/s3_string_object_fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class S3StringObjectFetcher {
);
const schema = await getSchemaCommandResult.Body?.transformToString();
if (!schema) {
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
throw new Error('Error on parsing output schema');
}
return schema;
Expand Down
10 changes: 10 additions & 0 deletions packages/schema-generator/src/generate_schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,14 @@ void describe('SchemaGenerator', () => {
'Unable to parse the database URL. One or more parts of the database URL is missing. Missing [username, password].',
});
});

void it('should throw error if database engine is incorrect', async () => {
const parse = () =>
parseDatabaseUrl('incorrect://user:password@test-host-name/db');
assert.throws(parse, {
name: 'DatabaseUrlParseError',
message:
'Unable to parse the database URL. Unsupported database engine: incorrect',
});
});
});
13 changes: 9 additions & 4 deletions packages/schema-generator/src/generate_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type SchemaGeneratorConfig = {

type AmplifyGenerateSchemaError =
| 'DatabaseConnectionError'
| 'DatabaseUnsupportedEngineError'
| 'DatabaseUrlParseError';

/**
Expand All @@ -39,7 +40,6 @@ export class SchemaGenerator {
} catch (err) {
const databaseError = err as DatabaseConnectError;
if (databaseError.code === 'ETIMEDOUT') {
// eslint-disable-next-line amplify-backend-rules/no-amplify-errors
throw new AmplifyUserError<AmplifyGenerateSchemaError>(
'DatabaseConnectionError',
{
Expand Down Expand Up @@ -118,7 +118,6 @@ export const parseDatabaseUrl = (databaseUrl: string): SQLDataSourceConfig => {
).filter((part) => !config[part]);

if (missingParts.length > 0) {
// eslint-disable-next-line amplify-backend-rules/no-amplify-errors
throw new AmplifyUserError<AmplifyGenerateSchemaError>(
'DatabaseUrlParseError',
{
Expand All @@ -134,7 +133,6 @@ export const parseDatabaseUrl = (databaseUrl: string): SQLDataSourceConfig => {
return config;
} catch (err) {
const error = err as Error;
// eslint-disable-next-line amplify-backend-rules/no-amplify-errors
throw new AmplifyUserError<AmplifyGenerateSchemaError>(
'DatabaseUrlParseError',
{
Expand All @@ -153,7 +151,14 @@ const constructDBEngine = (engine: string): SQLEngine => {
case 'postgres':
return 'postgresql';
default:
throw new Error(`Unsupported database engine: ${engine}`);
throw new AmplifyUserError<AmplifyGenerateSchemaError>(
'DatabaseUnsupportedEngineError',
{
message: `Unsupported database engine: ${engine}`,
resolution:
'Ensure that database URL specifies supported engine. Supported engines are "mysql", "postgresql", "postgres".',
}
);
}
};

Expand Down