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(mock): handle stack trace & produce meaningful error, resolution messages for mocking API & Func category #12537

Merged
merged 7 commits into from
May 1, 2023

Conversation

manaswi223
Copy link
Contributor

@manaswi223 manaswi223 commented Apr 27, 2023

Description of changes

This PR allows mocking API category & Function category to produce meaningful Error: Resolution: and Link: to documentation wherever applicable while failing on scenarios like:

  1. Amplify Environment is not initialized in the project and mocking API
    amplify mock api in an uninitialized project throws with a stack trace amplify-category-api#1324
  2. No AppSync API is added to the project and mocking API
  3. blank schema in the schema.graphql file and mocking API
  4. When Lambdas are not configured in the project and mocking Function, etc.
    amplify mock api throws an error with a stack trace if @function specifies a function name that does not exist amplify-category-api#1390

Elaborate examples of above scenarios and outputs produced before & after the PR:

  1. Amplify Environment is not initialized in the project and mocking API

steps:

  1. amplify init -y
  2. amplify add api > GraphQL > accept defaults
  3. simulate a fresh git clone git init; git clean -fXd
  4. amplify mock api
  5. observe Error

Before the PR:

➜  amplify mock api
AmplifyException [EnvironmentNotInitializedError]: Current environment cannot be determined.
    at AmplifyToolkit.getEnvInfo [as _getEnvInfo] (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/extensions/amplify-helpers/get-env-info.js:9:11)
    at getMockDataDirectory (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/utils/mock-directory.js:51:23)
    at addMockDataToGitIgnore (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/utils/git-ignore.js:53:82)
    at start (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/api/index.js:16:40)
    at Object.run (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/commands/mock/api.js:21:27)
    at Object.executeAmplifyCommand (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/amplify-plugin-index.js:55:23)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async executePluginModuleCommand (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:136:5)
    at async executeCommand (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:34:9)
    at async Object.run (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/index.js:116:5) {
  classification: 'ERROR',
  options: {
    message: 'Current environment cannot be determined.',
    resolution: "Use 'amplify init' in the root of your app directory to create a new environment."
  },
  downstreamException: undefined,
  toObject: [Function (anonymous)],
  details: undefined,
  resolution: "Use 'amplify init' in the root of your app directory to create a new environment.",
  code: undefined,
  link: 'https://docs.amplify.aws/cli/project/troubleshooting/'
}

after the PR:
Screenshot 2023-04-25 at 3 19 24 PM

  1. No AppSync API is added to the project and mocking API

steps:

  1. amplify init
  2. amplify mock api

before the PR:

Failed to start API Mocking. Running cleanup tasks.
MockProcessFault: Failed to start API Mocking.. Reason: No AppSync API is added to the project
    at APITest.start (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/api/api.js:157:13)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async start (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/api/index.js:18:5)
    at async Object.run (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/commands/mock/api.js:21:5)
    at async Object.executeAmplifyCommand (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/amplify-plugin-index.js:55:3)
    at async executePluginModuleCommand (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:135:5)
    at async executeCommand (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:33:9)
    at async Object.run (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/index.js:117:5) {
  classification: 'FAULT',
  options: {
    message: 'Failed to start API Mocking.. Reason: No AppSync API is added to the project',
    link: 'https://docs.amplify.aws/cli/graphql/troubleshooting/'
  },
  downstreamException: undefined,
  toObject: [Function (anonymous)],
  details: undefined,
  resolution: undefined,
  code: undefined,
  link: 'https://docs.amplify.aws/cli/graphql/troubleshooting/'
}

after the PR:
Screenshot 2023-04-24 at 10 19 49 PM

  1. adding blank schema in the schema.graphql file and mocking API

steps:

  1. amplify init -y
  2. amplify add api > GraphQL > choose blank schema
  3. amplify mock api

before the PR:

 amplify mock api
Failed to start API Mocking. Running cleanup tasks.
MockProcessFault: Failed to start API Mocking.. Reason: Syntax Error: Unexpected <EOF>.
    at APITest.start (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/api/api.js:157:13)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async start (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/api/index.js:18:5)
    at async Object.run (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/commands/mock/api.js:21:5)
    at async Object.executeAmplifyCommand (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/amplify-plugin-index.js:55:3)
    at async executePluginModuleCommand (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:135:5)
    at async executeCommand (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:33:9)
    at async Object.run (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/index.js:117:5) {
  classification: 'FAULT',
  options: {
    message: 'Failed to start API Mocking.. Reason: Syntax Error: Unexpected <EOF>.',
    link: 'https://docs.amplify.aws/cli/graphql/troubleshooting/'
  },
  downstreamException: undefined,
  toObject: [Function (anonymous)],
  details: undefined,
  resolution: undefined,
  code: undefined,
  link: 'https://docs.amplify.aws/cli/graphql/troubleshooting/'
}

after the PR:
Screenshot 2023-04-24 at 11 21 36 PM

  1. When Lambdas are not configured in the project and mocking Function

schema:

type Query {
  oops: String @function(name: "doesnotexist-${env}")
}

steps:

  1. amplify init -y

  2. amplify add api > GraphQL

copy the schema from the description above, where the function doesn’t exist

  1. run amplify mock api

observe error

before the PR:

➜  amplify mock api

⚠️  WARNING: your GraphQL API currently allows public create, read, update, and delete access to all models via an API Key. To configure PRODUCTION-READY authorization rules, review: https://docs.amplify.aws/cli/graphql/authorization-rules

✅ GraphQL schema compiled successfully.

Edit your schema at /Users/josefai/Documents/projects/aws-amplify/reproductions/a12357/amplify/backend/api/a12357/schema.graphql or place .graphql files in a directory at /Users/josefai/Documents/projects/aws-amplify/reproductions/a12357/amplify/backend/api/a12357/schema
Failed to start API Mocking. Running cleanup tasks.
MockProcessFault: Failed to start API Mocking.. Reason: Did not find a Lambda matching ARN ["arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:doesnotexist-${env}"] in the project. Local mocking only supports Lambdas that are configured in the project.
See https://docs.amplify.aws/cli/graphql/custom-business-logic/#lambda-function-resolver for information on how to configure Lambda resolvers.
    at APITest.start (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/api/api.js:157:13)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async start (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/api/index.js:18:5)
    at async Object.run (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/commands/mock/api.js:21:5)
    at async Object.executeAmplifyCommand (/snapshot/repo/build/node_modules/@aws-amplify/amplify-util-mock/lib/amplify-plugin-index.js:55:3)
    at async executePluginModuleCommand (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:135:5)
    at async executeCommand (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:33:9)
    at async Object.run (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/index.js:117:5) {
  classification: 'FAULT',
  options: {
    message: 'Failed to start API Mocking.. Reason: Did not find a Lambda matching ARN ["arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:doesnotexist-${env}"] in the project. Local mocking only supports Lambdas that are configured in the project.\n' +
      'See https://docs.amplify.aws/cli/graphql/custom-business-logic/#lambda-function-resolver for information on how to configure Lambda resolvers.',
    link: 'https://docs.amplify.aws/cli/graphql/troubleshooting/'
  },
  downstreamException: undefined,
  toObject: [Function (anonymous)],
  details: undefined,
  resolution: undefined,
  code: undefined,
  link: 'https://docs.amplify.aws/cli/graphql/troubleshooting/'
}

after the PR:
Screenshot 2023-04-24 at 10 55 41 PM

Issue #, if available

Description of how you validated changes

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)
  • New AWS SDK calls or CloudFormation actions have been added to relevant test and service IAM policies
  • Pull request labels are added

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@manaswi223 manaswi223 requested a review from a team as a code owner April 27, 2023 21:22
AaronZyLee
AaronZyLee previously approved these changes Apr 28, 2023
@danielleadams danielleadams changed the title fix(mock)-handle stack trace & produce meaningful error, resolution messages for mocking API & Func category fix(mock): handle stack trace & produce meaningful error, resolution messages for mocking API & Func category Apr 28, 2023
@aws-eddy
Copy link
Contributor

aws-eddy commented May 1, 2023

For the second case, "No AppSync API is added to the project and mocking API" error messaging, it doesn't seem like the resolution of amplify init api is the right resolution. I would suggest changing the error resolution to say something along the lines of amplify add api.

Update: Disregard my comment, your code does not represent the screenshots in the PR description 😄

@aws-eddy
Copy link
Contributor

aws-eddy commented May 1, 2023

Good to merge once E2E tests pass.

@aws-eddy aws-eddy merged commit 0f20518 into dev May 1, 2023
@aws-eddy aws-eddy deleted the fix/stack-trace-mockAPI branch May 1, 2023 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants